void chdir (struct intr_frame *f) { const char * dirname = *(char **)value_stack(f->esp,4); //if empty or root return if(strlen(dirname) == 0 || strcmp( dirname, "/")) f->eax = 0; /* CLOSE FILE ? */ struct file * file = filesys_open(dirname); if ( file == NULL ) { f->eax = 0; return; } struct inode * myinode = file_get_inode (file); enum file_type type = inode_type (myinode); //if the file is a dir open it and set pwd to it if(type == FILE_DIR) { f->eax = 1; dir_close(thread_current()->pwd); thread_current()->pwd = dir_open(inode_reopen(myinode)); } else f->eax = 0; file_close(file); }
static int sys_open(const char* file_name) { struct thread *curr=thread_current(); struct opened_file_elem *opened_elem = (struct opened_file_elem *) malloc(sizeof(struct opened_file_elem)); struct file *opened_file; opened_file = filesys_open(file_name); if(opened_file==NULL) { free(opened_elem); return -1; } else { // char *name = (char*)malloc(strlen(file_name)); // strlcpy(name,file_name,strlen(file_name)+1); opened_elem->opened_file=opened_file; opened_elem->fd=curr->fd; // printf("%d open\n",curr->fd); // opened_elem->name=name; // printf("open file fd :%d\n",opened_elem->fd); curr->fd++; list_push_back(&curr->openfile_list , &opened_elem->elem_f); // printf(" initial file size %d\n",file_length(opened_file)); return opened_elem->fd; } }
/* Creates a file named NAME with the given INITIAL_SIZE. Returns true if successful, false otherwise. Fails if a file named NAME already exists, or if internal memory allocation fails. */ bool filesys_create (const char *name, off_t initial_size, enum file_type type) { block_sector_t inode_sector = 0; char * parse = parse_filename (name); //get the correct dir struct dir *dir = dir_lookup_rec (parse); bool success = (dir != NULL && free_map_allocate (1, &inode_sector) && inode_create (inode_sector, initial_size, type) && dir_add (dir, parse, inode_sector)); if (!success && inode_sector != 0) free_map_release (inode_sector, 1); if( success == true && type == FILE_DIR ) { //we want to add . and .. as well if it is a dir //open the created directory struct file * created = filesys_open(parse); struct dir * mydir = dir_open(file_get_inode (created)); //add . to it dir_add (mydir, ".", inode_sector); struct inode * parent = dir_get_inode (dir); block_sector_t inode_sector_parent = inode_id(parent); //add .. to it dir_add (mydir, "..", inode_sector_parent); dir_close(mydir); file_close(created); } dir_close (dir); return success; }
/* Open system call. */ static int sys_open (const char *ufile) { char *kfile = copy_in_string (ufile); struct file_descriptor *fd; int handle = -1; fd = calloc (1, sizeof *fd); if (fd != NULL) { struct inode *inode = filesys_open (kfile); if (inode != NULL) { if (inode_get_type (inode) == FILE_INODE) fd->file = file_open (inode); else fd->dir = dir_open (inode); if (fd->file != NULL || fd->dir != NULL) { struct thread *cur = thread_current (); handle = fd->handle = cur->next_handle++; list_push_front (&cur->fds, &fd->elem); } else { free (fd); inode_close (inode); } } } palloc_free_page (kfile); return handle; }
/* Open a file. */ static int sys_open (const char *file) { struct file *sys_f; struct user_file *f; #if PRINT_DEBUG printf ("[SYSCALL] SYS_OPEN: file: %s\n", file); #endif if (file == NULL || !is_user_vaddr (file)) sys_exit (-1); lock_acquire (&file_lock); sys_f = filesys_open (file); lock_release (&file_lock); if (sys_f == NULL) return -1; f = (struct user_file *) malloc (sizeof (struct user_file)); if (f == NULL) { lock_acquire (&file_lock); file_close (sys_f); lock_release (&file_lock); return -1; } f->file = sys_f; f->fid = allocate_fid (); list_push_back (&thread_current ()->files, &f->thread_elem); return f->fid; }
/* Open system call. */ static int sys_open (const char *ufile) { char *kfile = copy_in_string (ufile); struct file_descriptor *fd; int handle = -1; fd = malloc (sizeof *fd); if (fd != NULL) { lock_acquire (&fs_lock); fd->file = filesys_open (kfile); if (fd->file != NULL) { struct thread *cur = thread_current (); handle = fd->handle = cur->next_handle++; list_push_front (&cur->fds, &fd->elem); } else free (fd); lock_release (&fs_lock); } palloc_free_page (kfile); return handle; }
int syscall_open(const char *file) { if(!is_valid_ptr(file)) syscall_exit(-1); // 0. Try to open it. struct file *fp = filesys_open(file); if (!fp) return -1; // 1. Get First Empty FD int fd = Get_First_Empty_FD(&(thread_current()->FDs)); if (fd == -1) // XXX Reached MAX_FD (* TEST: [multi-oom]) return -1; // 2. Make FD struct fd_list *new_fd = (struct fd_list *)calloc(1, sizeof(struct fd_list)); if(!new_fd) return -1; // 3. Assign That FD as got the first empty one. new_fd->fd = fd; new_fd->file = fp; // 4. Insert Ordered this FD at FDs List. list_insert_ordered( &(thread_current()->FDs), &(new_fd->elem), FD_List_Less_Func, NULL); return fd; }
/* Open specified file and return fd. */ static int syscall_open(const char *file_name){ int fd; struct fd_elem *fd_elem, **fdt = thread_current()->open_files; struct file *f; /* Get first available fd and ensure it's valid. */ for(fd = 2; fdt[fd] != NULL && fd < 128; fd++); if(fd == 128) return -1; /* Open file */ lock_acquire(&fs_lock); f = filesys_open(file_name); lock_release(&fs_lock); /* Check that file was opened. */ if(f == NULL) return -1; /* Create new fd_elem and add it to the file table. */ fd_elem = (struct fd_elem *) malloc(sizeof(struct fd_elem)); fd_elem->fd = fd; fd_elem->file = f; fdt[fd] = fd_elem; return fd; }
static int syscall_open (const char *file) { lock_acquire (&filesys_lock); struct file* fp = filesys_open (file); int retval = (fp) ? get_new_fd (fm, fp, FM_MODE_FD) : -1; lock_release (&filesys_lock); return retval; }
/* Opens the file with the given NAME. Returns the new file if successful or a null pointer otherwise. Fails if no file named NAME exists, or if an internal memory allocation fails. */ struct file * filesys_open (const char *name) { if (strnlen (name, FULLPATH_MAX_LEN) == 0) return NULL; if (strcmp (".", name) == 0) { block_sector_t cwd = thread_current ()->cwd_sector; struct inode *curr = NULL; curr = inode_open (cwd); struct dir *p; p = dir_open (inode_open (curr->data.parent_dir_sector)); struct dir_entry e; size_t ofs; ASSERT (p != NULL); for (ofs = 0; inode_read_at (p->inode, &e, sizeof e, ofs) == sizeof e; ofs += sizeof e) { if (e.inode_sector == cwd && e.in_use) { return filesys_open (e.name); } } return NULL; } struct inode *crr = NULL; crr = inode_open (thread_current ()->cwd_sector); struct dir *parent_dir = dir_reopen(dir_get_parent_dir (name)); //if (crr->data.is_dir) //parent_dir = dir_open (inode_open (crr->data.parent_dir_sector)); //else if (parent_dir == NULL) return NULL; struct inode *inode = NULL; char leaf_name[NAME_MAX + 1]; if (!dir_get_leaf_name (name, leaf_name) && strnlen(leaf_name, NAME_MAX) == 0) { inode = inode_reopen (dir_get_inode (parent_dir)); dir_close (parent_dir); return file_open (inode); } if (parent_dir != NULL) dir_lookup (parent_dir, leaf_name, &inode); dir_close (parent_dir); return file_open (inode); }
int open (const char *file) { struct file *f = filesys_open (file); if (!f) { return -1; } return process_add_file(f); }
bool sys_create(const char* file, unsigned initial_size) { if (filesys_create(file, initial_size) && initial_size >= 0) { map_insert(get_filemap(), filesys_open(file)); return true; } return false; }
/* Open file */ int open(const char* file) { int fd; struct file* f = filesys_open(file); if( f == NULL ) return (int)-1; fd = process_add_file(f); return (int)fd; }
static void sys_open(struct intr_frame *f_) { unsigned int *esp = f_->esp; char *file = *(esp + 1); struct file *f = filesys_open(file); if (f == NULL) f_->eax = -1; else { f_->eax = file_insert_in_fd(f); } }
int sys_open(const char* file) { struct file* temp = filesys_open(file); if(temp != NULL) { // Mata in pekaren till filen i mapen // filemap finns initierad i thread.h/c return map_insert(get_filemap(), temp); // returnerar värdet i mapen } return -1; // filen fanns inte }
/* change working directory */ bool sys_chdir(const char *dir) { bool result = false; struct file *directory = filesys_open(dir); if(directory != NULL) { /* close thread's directory */ dir_close(thread_current()->thread_dir); /* chage thread's directory to new directory */ thread_current()->thread_dir = dir_open(file_get_inode(directory)); result = true; } return result; }
/* Syscall handler for when a syscall open is invoked. */ static int open (const char *file_) { // DONE lock_acquire(&file_lock); struct file *file = filesys_open(file_); lock_release(&file_lock); if (!file) return -1; struct file_wrapper *f = (struct file_wrapper *) malloc(sizeof(struct file_wrapper)); f->file = file; list_push_back(&thread_current()->file_wrappers, &f->thread_elem); lock_acquire(&fd_lock); f->fd = curr_fd++; lock_release(&fd_lock); return f->fd; }
static int my_open(const char *file) { lock_acquire(&filesys_lock); if(file == NULL){ lock_release(&filesys_lock); return -1; } if(!address_valid(file)){ lock_release(&filesys_lock); my_exit(-1); } struct file *fp = filesys_open(file); int fd = thread_current()->fd; if(!fp){ lock_release(&filesys_lock); // printf("file open error\n"); return -1; } struct Elf32_Ehdr ehdr; if (!(file_read (fp, &ehdr, sizeof ehdr) != sizeof ehdr || memcmp (ehdr.e_ident, "\177ELF\1\1\1", 7) || ehdr.e_type != 2 || ehdr.e_machine != 3 || ehdr.e_version != 1 || ehdr.e_phentsize != sizeof (struct Elf32_Phdr) || ehdr.e_phnum > 1024)) { file_deny_write (fp); } file_seek(fp, 0); struct process_file *pf = malloc(sizeof(struct process_file)); pf->file = fp; pf->fd = fd; (thread_current()->fd)++; list_push_back(&thread_current()->file_list, &pf->elem); lock_release(&filesys_lock); return fd; }
/* * System Call: int open (const char *file) * Opens the file called file. Returns a nonnegative integer handle called * a "file descriptor" (fd), or -1 if the file could not be opened. * * File descriptors numbered 0 and 1 are reserved for the * console: fd 0 (STDIN_FILENO) is standard input, fd 1 (STDOUT_FILENO) * is standard output. The open system call will never return either of * these file descriptors, which are valid as system call arguments only * as explicitly described below. * * Each process has an independent set of file descriptors. * File descriptors are not inherited by child processes. * * When a single file is opened more than once, whether by a single * process or different processes, each open returns a new file * descriptor. Different file descriptors for a single file are closed * independently in separate calls to close and they do not share a file * position. */ int open_handler(const char *file) { //printf("opening...\n"); if (string_access_ok(file)) { lock_acquire (&fic_m); struct file *f; if ((f = filesys_open(file))==NULL){ lock_release (&fic_m); return -1; } int ret = addfic(f); lock_release (&fic_m); return ret; } return -1; }
int process_file (const char* filename) { //Fail if can't open struct file* file = filesys_open(filename); if (!file) return -1; //Allocate resources and add to file_list struct thread* current = thread_current(); struct file_info* f = malloc(sizeof(struct file_info)); f->file = file; f->fd = current->fd; ++current->fd; list_push_back(¤t->file_list, &f->felem); return f->fd; }
int open(const char *file) { lock_acquire(&filesys_lock); struct file *f = filesys_open(file); lock_release(&filesys_lock); int fd; if (f == NULL) { fd = -1; } else { fd = thread_add_fd(f); } return fd; }
/* open file and return fd */ int open(const char *file) { int fd; struct file *new_file; new_file=filesys_open(file); if(new_file != NULL) { fd = process_add_file(new_file); return fd; } else { return -1; } }
bool remove (const char *file) { if (not_valid(file)) exit (-1); lock_acquire(&file_lock); /* In case the file is opened. First check its existence. */ struct file *f = filesys_open (file); bool result; if (f == NULL) result = false; else { file_close (f); result = filesys_remove (file); } lock_release(&file_lock); return result; }
/* Opens the file with the name "file", and returns its file descriptor. Returns -1 if no such file exists, or if the open fails for any other reason. If the same file is opened multiple times by separate processes or the same process, different fds with separate file pointers will be returned */ static int sys_open (const char *file) { struct file *f; struct file_fd *ffd; check_ptr (file); lock_acquire (&fs_lock); f = filesys_open (file); lock_release (&fs_lock); if (f == NULL) return -1; ffd = malloc (sizeof(struct file_fd)); ffd->file = f; ffd->fd = thread_current ()->next_fd++; list_push_back (&thread_current ()->open_files, &ffd->elem); return ffd->fd; }
static int sys_open (void *file_, struct intr_frame *f) { struct file *file; file = filesys_open ((char*)file_); if (file == NULL){ f->eax = -1; return -1; } else{ struct thread *t = thread_current(); if (t->fd_num >= FD_MAX){ f->eax = -1; return -1; } f->eax = t->fd_num; (t->fd_list)[(t->fd_num)++] = file; return f->eax; } }
/* Opens specified file. Returns its file descriptor. Same file opened multiple times returns different file descriptors. A process has an independent set of file descriptors (files open in that process). fd = 0 is STDIN_FILENO, fd = 1 is STDOUT_FILENO - These are never returned here. If file could not be opened, -1 is returned. */ static int sys_open(const char *file) { check_mem_ptr(file); if (file == NULL) sys_exit(ERROR); lock_acquire(&secure_file); struct file *fl = filesys_open(file); if (fl == NULL) { lock_release(&secure_file); return FD_ERROR; } struct thread *t = thread_current(); /* Freed in sys_close(). */ struct proc_file *f = malloc(sizeof(struct proc_file)); if (f == NULL) sys_exit(FD_ERROR); list_push_front(&t->files, &f->file_elem); f->file = fl; /* If file is currently being run as an executable in this process, we must not be able to write to it. */ if (is_executable(file)) file_deny_write(f->file); int file_descriptor = t->next_file_descriptor; f->fd = file_descriptor; /* Increment next_file_descriptor so that the next file to be opened has a different file descriptor. */ t->next_file_descriptor++; lock_release(&secure_file); return file_descriptor; }
void open (struct intr_frame *f) { const char * filename = *(char **)value_stack(f->esp,4); int fd; struct file * file = filesys_open (filename); if (file == NULL) { fd = -1; } else { //if not a directory if(!file_direct(file)) { fd = add_file(file); } else { //if a directory open it and save it in the pointer in the fd table so we can open it again struct inode * myinode = file_get_inode (file); struct dir * mydir = dir_open (myinode); fd = add_filemmap(file,mydir); } } f->eax = fd; }
static bool chdir_handler (const char *dir) { if (dir == NULL) { return false; } struct dir *new_dir = dir_open(filesys_open(dir)); if (new_dir == NULL) { return false; } struct thread *t = thread_current(); if (t->parent_data != NULL && t->parent_data->cwd != NULL) { dir_close(t->parent_data->cwd); t->parent_data->cwd = NULL; } if (t->cwd != NULL) { dir_close(t->cwd); } t->cwd = new_dir; return true; }
int sys_open(const char* file) { // memory validation check_user((const uint8_t*) file); struct file* file_opened; struct file_desc* fd = palloc_get_page(0); if (!fd) { return -1; } lock_acquire (&filesys_lock); file_opened = filesys_open(file); if (!file_opened) { palloc_free_page (fd); lock_release (&filesys_lock); return -1; } fd->file = file_opened; //file save // directory handling struct inode *inode = file_get_inode(fd->file); if(inode != NULL && inode_is_directory(inode)) { fd->dir = dir_open( inode_reopen(inode) ); } else fd->dir = NULL; struct list* fd_list = &thread_current()->file_descriptors; if (list_empty(fd_list)) { // 0, 1, 2 are reserved for stdin, stdout, stderr fd->id = 3; } else { fd->id = (list_entry(list_back(fd_list), struct file_desc, elem)->id) + 1; } list_push_back(fd_list, &(fd->elem)); lock_release (&filesys_lock); return fd->id; }
static int open_handler (const char *file) { if (file == NULL) { return -1; } struct inode *inode = filesys_open(file); if (inode == NULL) { return -1; } struct dir *sys_dir = NULL; struct file *sys_file = NULL; if (inode_is_dir(inode)) { sys_dir = dir_open(inode); if (sys_dir == NULL) { return -1; } } else { sys_file = file_open(inode); if (sys_file == NULL) { return -1; } } struct file_struct *fstruct = malloc(sizeof(struct file_struct)); if (fstruct == NULL) { if (sys_file != NULL) { file_close(sys_file); } else { dir_close(sys_dir); } return -1; } list_push_back(&thread_current()->files, &fstruct->elem); fstruct->fd = create_fd(); fstruct->sys_file = sys_file; fstruct->sys_dir = sys_dir; return fstruct->fd; }