Commit 28eccff0 authored by pku2402010330's avatar pku2402010330
Browse files

sys_open

parent 530efc4b
No related merge requests found
Showing with 9 additions and 64 deletions
+9 -64
...@@ -195,62 +195,6 @@ create(char *path, short type, int mode) ...@@ -195,62 +195,6 @@ create(char *path, short type, int mode)
return ep; return ep;
} }
// uint64
// sys_open(void)
// {
// char path[FAT32_MAX_PATH];
// int fd, omode;
// struct file *f;
// struct dirent *ep;
// if(argstr(0, path, FAT32_MAX_PATH) < 0 || argint(1, &omode) < 0){
// printf("Error: Invalid arguments\n");
// return -1;
// }
// printf("Opening file: %s with mode: %d\n", path, omode);
// if(omode & O_CREATE){
// ep = create(path, T_FILE, omode);
// if(ep == NULL){
// return -1; // File exists error
// }
// } else {
// if((ep = ename(path)) == NULL){
// return -1; // File not found error
// }
// elock(ep);
// if((ep->attribute & ATTR_DIRECTORY) && omode != O_RDONLY){
// eunlock(ep);
// eput(ep);
// return -1; // Is a directory error
// }
// }
// if((f = filealloc()) == NULL || (fd = fdalloc(f)) < 0){
// if (f) {
// fileclose(f);
// }
// eunlock(ep);
// eput(ep);
// return -1; // Too many open files error
// }
// if(!(ep->attribute & ATTR_DIRECTORY) && (omode & O_TRUNC)){
// etrunc(ep);
// }
// f->type = FD_ENTRY;
// f->off = (omode & O_APPEND) ? ep->file_size : 0;
// f->ep = ep;
// f->readable = !(omode & O_WRONLY);
// f->writable = (omode & O_WRONLY) || (omode & O_RDWR);
// eunlock(ep);
// return fd;
// }
uint64 uint64
sys_open(void) sys_open(void)
{ {
...@@ -260,38 +204,39 @@ sys_open(void) ...@@ -260,38 +204,39 @@ sys_open(void)
struct dirent *ep; struct dirent *ep;
if(argint(0, &fd) < 0 || argstr(1, path, FAT32_MAX_PATH) < 0 || argint(2, &flags) < 0 || argint(3, &omode) < 0) { if(argint(0, &fd) < 0 || argstr(1, path, FAT32_MAX_PATH) < 0 || argint(2, &flags) < 0 || argint(3, &omode) < 0) {
return -1; return -1; // Argument error
} }
if(flags & O_CREATE){ if(flags & O_CREATE){
ep = create(path, T_FILE, flags); ep = create(path, T_FILE, flags);
if(ep == NULL){ if(ep == NULL){
return -1; return -1; // File creation failed
} }
} else { } else {
if((ep = ename(path)) == NULL){ if((ep = ename(path)) == NULL){
return -1; return -1; // File not found
} }
elock(ep); elock(ep);
if((ep->attribute & ATTR_DIRECTORY) && (flags & O_RDONLY) && (flags & O_DIRECTORY)){ if((ep->attribute & ATTR_DIRECTORY) && (flags & O_DIRECTORY)){
eunlock(ep); eunlock(ep);
eput(ep); eput(ep);
return -1; return -1; // Directory cannot be opened as a file
} }
} }
if((f = filealloc()) == NULL || (fd = fdalloc(f)) < 0){ if((f = filealloc()) == NULL || (fd = fdalloc(f)) < 0){
printf("Error: Could not allocate file descriptor\n");
if (f) { if (f) {
fileclose(f); fileclose(f);
} }
eunlock(ep); eunlock(ep);
eput(ep); eput(ep);
printf("unable to open: %d\n", flags); printf("unable to open: %d\n", flags);
return -1; return -1; // File descriptor allocation failed
} }
if(!(ep->attribute & ATTR_DIRECTORY) && (flags & O_TRUNC)){ if(!(ep->attribute & ATTR_DIRECTORY) && (flags & O_TRUNC)){
etrunc(ep); etrunc(ep); // Truncate file if not directory and O_TRUNC flag is set
} }
f->type = FD_ENTRY; f->type = FD_ENTRY;
...@@ -300,7 +245,7 @@ sys_open(void) ...@@ -300,7 +245,7 @@ sys_open(void)
f->readable = !(flags & O_WRONLY); f->readable = !(flags & O_WRONLY);
f->writable = (flags & O_WRONLY) || (flags & O_RDWR); f->writable = (flags & O_WRONLY) || (flags & O_RDWR);
eunlock(ep); eunlock(ep); // Unlock after setting the file structure
return fd; return fd;
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment