Example #1
0
int binfmt_load(struct proc *proc, const char *path, struct proc **ref)
{
    struct vnode vnode;
    struct inode *inode = NULL;
    int err = 0;

    struct uio uio;
    memset(&uio, 0, sizeof(struct uio));

    if (proc) {
        uio = PROC_UIO(proc);
    }

    if ((err = vfs_lookup(path, &uio, &vnode, NULL)))
        return err;

    if ((err = vfs_vget(&vnode, &inode)))
        return err;

    for (size_t i = 0; i < NR_BINFMT; ++i) {
        if (!binfmt_list[i].check(inode)) {
            binfmt_fmt_load(proc, path, inode, &binfmt_list[i], ref);
            vfs_close(inode);
            return 0;
        }
    }

    vfs_close(inode);
    return -ENOEXEC;
}
/////////////////////////////////////////////////////////////////////
// Mapper 20
bool
NES_mapper20::initialize (NES *parent, NES_PPU *ppu)
{
  hfile_t fp = NULL;
  uint8 header[16];
  
  if (!(NES_mapper::initialize (parent, ppu)))
    goto error;
  
  fp = vfs_open (parent->disksys_rom_filename, VFS_READ);
  if (!fp)
  {
    ERROR("no disksys.rom");
    ERROR(parent->disksys_rom_filename);
    goto error;
  }
  
  if (vfs_read(header, sizeof(header), 1, fp) != sizeof(header))
    goto error;
  
  if (!NES_MEMCMP (header, "NES", 3))
    vfs_seek(fp, 0x6010, VFS_SEEK_SET);
  else
    vfs_seek(fp, 0, VFS_SEEK_SET);
  
  if (vfs_read(bios, 0x2000, 1, fp) != 0x2000)
    goto error;
  
  vfs_close(fp);
  return true;
  
error:
  if (fp) vfs_close(fp);
  return false;
}
Example #3
0
void op_write(const char **params, int nparams) {
  assert(nparams > 1);
  inode_t *ino = vfs_open(params[0], &dummy_access);

  if (!ino) {
    /* Find parent. */
    char *str = NULL;
    int i;
    for (i = strlen(params[0]); i >= 0; --i) {
      if (params[0][i] == '/') {
        str = kmalloc(i+1);
        strncpy(str, params[0], i);
        str[i] = '\0';
        break;
      }
    }
    assert(str && "Parent directory not found!");

    ino = vfs_open(str, &dummy_access);
    assert(ino && "Parent directory not found!");

    vfs_mknod(ino, &params[0][i+1], it_file, 0755, 0, 0);
    vfs_close(ino);
    ino = vfs_open(params[0], &dummy_access);

    assert(ino && "File not found after having created it!");
  }

  assert(ino && "File not found!");
  assert(ino->type == it_file && "File is not a regular file!");

  vfs_write(ino, 0, (void*)params[1], strlen(params[1]));
  vfs_close(ino);
}
Example #4
0
int
runprogram(char *progname)
{
	struct addrspace *as;
	struct vnode *v;
	vaddr_t entrypoint, stackptr;
	int result;

	/* Open the file. */
	result = vfs_open(progname, O_RDONLY, 0, &v);
	if (result) {
		return result;
	}

	/* We should be a new process. */
	KASSERT(curproc_getas() == NULL);

	/* Create a new address space. */
	#if OPT_A3
	as = as_create(progname);
	#else
	as = as_create();
	#endif
	if (as ==NULL) {
		vfs_close(v);
		return ENOMEM;
	}

	/* Switch to it and activate it. */
	curproc_setas(as);
	as_activate();

	/* Load the executable. */
	result = load_elf(v, &entrypoint);
	if (result) {
		/* p_addrspace will go away when curproc is destroyed */
		vfs_close(v);
		return result;
	}

	/* Done with the file now. */
	vfs_close(v);

	/* Define the user stack in the address space */
	result = as_define_stack(as, &stackptr);
	if (result) {
		/* p_addrspace will go away when curproc is destroyed */
		return result;
	}

	/* Warp to user mode. */
	enter_new_process(0 /*argc*/, NULL /*userspace addr of argv*/,
			  stackptr, entrypoint);
	
	/* enter_new_process does not return. */
	panic("enter_new_process returned\n");
	return EINVAL;
}
BOOL ui_load_onmov_file(void)
{
    partition_t *p_partition = NULL;
    u32 partition_cnt = 0;
    u16 file_name[MAX_FILE_PATH] = {0};
    hfile_t file = NULL;
    u8 identify[3] = {0};
    u16 *p_line = NULL;

    partition_cnt = file_list_get_partition(&p_partition);
    if (partition_cnt > 0)
    {
        file_name[0] = p_partition[0].letter[0];
        str_asc2uni(":\\onmov.txt", file_name + 1);
    }
#ifdef WIN32
    str_asc2uni("C:\\onmov.txt", file_name);
#endif

    file = vfs_open(file_name, VFS_READ);
    if(file == NULL)
    {
        return FALSE;
    }

    vfs_read((void *)identify, 1, 2, file);


    if (strcmp(identify, "\xFF\xFE") && strcmp(identify, "\xFE\xFF"))
    {
        vfs_close(file);
        return FALSE;
    }

    p_line = freadline(file);
    if (p_line != NULL)
    {
        mtos_free(p_line);
        p_line = NULL;
    }

    while (TRUE)
    {
        p_line = freadline(file);
        if (p_line == NULL)
        {
            break;
        }

        parse(p_line);

        mtos_free(p_line);
        p_line = NULL;
    }

    vfs_close(file);
    return TRUE;
}
Example #6
0
static int cmd_vfs_sha256(struct vmm_chardev *cdev, const char *path)
{
	int fd, rc, i;
	u32 len;
	size_t buf_rd;
	u8 buf[VFS_LOAD_BUF_SZ];
	struct stat st;
	struct sha256_context sha256c;
	sha256_digest_t digest;

	fd = vfs_open(path, O_RDONLY, 0);
	if (fd < 0) {
		vmm_cprintf(cdev, "Failed to open %s\n", path);
		return fd;
	}

	rc = vfs_fstat(fd, &st);
	if (rc) {
		vfs_close(fd);
		vmm_cprintf(cdev, "Failed to stat %s\n", path);
		return rc;
	}

	if (!(st.st_mode & S_IFREG)) {
		vfs_close(fd);
		vmm_cprintf(cdev, "Cannot read %s\n", path);
		return VMM_EINVALID;
	}

	len = st.st_size;
	sha256_init(&sha256c);

	while (len) {
		memset(buf, 0, sizeof(buf));

		buf_rd = (len < VFS_LOAD_BUF_SZ) ? len : VFS_LOAD_BUF_SZ;
		buf_rd = vfs_read(fd, buf, buf_rd);
		if (buf_rd < 1) {
			break;
		}
		sha256_update(&sha256c, buf, buf_rd);
	}

	sha256_final(digest, &sha256c);

	vmm_cprintf(cdev, "SHA-256 Digest: ");
	for (i = 0; i < SHA256_DIGEST_LEN; i++)
		vmm_cprintf(cdev, "%x", digest[i]);
	vmm_cprintf(cdev, "\n");

	rc = vfs_close(fd);
	if (rc) {
		vmm_cprintf(cdev, "Failed to close %s\n", path);
		return rc;
	}

	return VMM_OK;
}
Example #7
0
int16_t
cron_write_error(struct vfs_file_handle_t * file)
{
  vfs_close(file);

  /* truncate file */
  vfs_close(vfs_open(CRON_FILENAME));
  return -1;
}
Example #8
0
static int cmd_vfs_load(struct vmm_chardev *cdev, physical_addr_t pa, 
			const char *path, u32 off, u32 len)
{
	int fd, rc;
	size_t buf_rd, rd_count;
	char buf[VFS_LOAD_BUF_SZ];
	struct stat st;

	fd = vfs_open(path, O_RDONLY, 0);
	if (fd < 0) {
		vmm_cprintf(cdev, "Failed to open %s\n", path);
		return fd;
	}

	rc = vfs_fstat(fd, &st);
	if (rc) {
		vfs_close(fd);
		vmm_cprintf(cdev, "Failed to stat %s\n", path);
		return rc;
	}

	if (!(st.st_mode & S_IFREG)) {
		vfs_close(fd);
		vmm_cprintf(cdev, "Cannot read %s\n", path);
		return VMM_EINVALID;
	}

	if (off >= st.st_size) {
		vfs_close(fd);
		vmm_cprintf(cdev, "Offset greater than file size\n", path);
		return VMM_EINVALID;
	}

	len = ((st.st_size - off) < len) ? (st.st_size - off) : len;

	rd_count = 0;
	while (len) {
		buf_rd = (len < VFS_LOAD_BUF_SZ) ? len : VFS_LOAD_BUF_SZ;
		buf_rd = vfs_read(fd, buf, buf_rd);
		if (buf_rd < 1) {
			break;
		}
		vmm_host_memory_write(pa, buf, buf_rd);
		len -= buf_rd;
		rd_count += buf_rd;
	}

	vmm_cprintf(cdev, "Loaded %d bytes\n", rd_count);

	rc = vfs_close(fd);
	if (rc) {
		vmm_cprintf(cdev, "Failed to close %s\n", path);
		return rc;
	}

	return VMM_OK;
}
Example #9
0
File: file.c Project: gapry/AOS
static void file_open_end(void *token, int err, struct vnode *vn) {
    dprintf(3, "file_open_end called\n");
    assert(token != NULL);

    cont_file_open_t *cont = (cont_file_open_t*)token;

    if (err) {
        cont->callback(cont->token, err, -1);
        free(cont);
        return;
    }

    // Compare openning permission with file permission
    int accmode = cont->flags & O_ACCMODE;
    bool file_readable = vn->sattr.st_mode & S_IRUSR;
    bool file_writable = vn->sattr.st_mode & S_IWUSR;
    dprintf(3, "file_readable = %d, file_writable = %d\n", (int)file_readable, (int)file_writable);
    if ((accmode == O_RDONLY && !file_readable) ||
        (accmode == O_WRONLY && !file_writable) ||
        (accmode == O_RDWR && !(file_readable && file_writable)))
    {
        vfs_close(vn, cont->flags);
        cont->callback(cont->token, EINVAL, -1);
        free(cont);
        return;
    }
    struct openfile *file;
    int fd;

    file = malloc(sizeof(struct openfile));
    dprintf(3, "created an openfile at %p\n", file);
    if (file == NULL) {
        vfs_close(vn, cont->flags);
        cont->callback(cont->token, ENOMEM, -1);
        free(cont);
        return;
    }

    file->of_offset = 0;
    file->of_accmode = accmode;
    file->of_refcount = 1;
    file->of_vnode = vn;

    /* place the file in the filetable, getting the file descriptor */
    err = filetable_placefile(file, &fd);
    if (err) {
        free(file);
        vfs_close(vn, cont->flags);
        cont->callback(cont->token, err, -1);
        free(cont);
        return;
    }

    cont->callback(cont->token, 0, fd);
    free(cont);
}
Example #10
0
/*
 * Load program "progname" and start running it in usermode.
 * Does not return except on error.
 *
 * Calls vfs_open on progname and thus may destroy it.
 */
int
runprogram(char *progname)
{
	struct vnode *v;
	vaddr_t entrypoint, stackptr;
	int result;

	/* Open the file. */
	result = vfs_open(progname, O_RDONLY, 0, &v);
	if (result) {
		return result;
	}

	/* We should be a new thread. */
	KASSERT(curthread->t_addrspace == NULL);

	/* Create a new address space. */
	curthread->t_addrspace = as_create();
	if (curthread->t_addrspace==NULL) {
		vfs_close(v);
		return ENOMEM;
	}
	curthread->t_filetable = filetable_create();
	if(curthread->t_filetable == NULL){
		return ENOMEM;
	}

	/* Activate it. */
	as_activate(curthread->t_addrspace);

	/* Load the executable. */
	result = load_elf(v, &entrypoint);
	if (result) {
		/* thread_exit destroys curthread->t_addrspace */
		vfs_close(v);
		return result;
	}

	/* Done with the file now. */
	vfs_close(v);

	/* Define the user stack in the address space */
	result = as_define_stack(curthread->t_addrspace, &stackptr);
	if (result) {
		/* thread_exit destroys curthread->t_addrspace */
		return result;
	}

	/* Warp to user mode. */
	enter_new_process(0 /*argc*/, NULL /*userspace addr of argv*/,
			  stackptr, entrypoint);
	
	/* enter_new_process does not return. */
	panic("enter_new_process returned\n");
	return EINVAL;
}
Example #11
0
File: elf.c Project: Masshat/almos
static error_t elf_header_read(char* pathname, struct vfs_file_s **file, Elf32_Ehdr **header)
{ 
	kmem_req_t req;
	Elf32_Ehdr *e_header;
	uint_t refcount;
	register error_t err;
	register size_t eh_size;
	register ssize_t count;

	if((err = vfs_open(current_task->vfs_cwd, pathname, VFS_O_RDONLY, 0, file)))
	{
		printk(ERROR, "\nERROR: elf_header_read: faild to open executable file, error %d\n", err);
		return err;
	}
  
	eh_size = sizeof(*e_header);

	req.type  = KMEM_GENERIC;
	req.size  = eh_size;
	req.flags = AF_USER;

	if((e_header = kmem_alloc(&req)) == NULL)
	{
		vfs_close(*file, &refcount);
		return -ENOMEM;
	}

	if((count= vfs_read(*file, (uint8_t*)e_header, eh_size)) != eh_size)
	{
		printk(ERROR, "\nERROR: elf_header_read: faild to read ELF header, got %d bytes, expected %d bytes\n", count, eh_size);
		err = (error_t)count;
		goto ELF_HEADER_READ_ERR;
	}

	if(!(err= elf_isElfHeader(e_header)))
	{
		printk(ERROR, "\nERROR: elf_header_read: executable is not in ELF format\n");
		goto ELF_HEADER_READ_ERR;
	}

	if(!(err= elf_isValidHeader(e_header)))
	{
		printk(ERROR, "\nERROR: elf_header_read: not supported Elf\n");
		goto ELF_HEADER_READ_ERR;
	}
 
	*header = e_header;
	return 0;
 
ELF_HEADER_READ_ERR:
	vfs_close(*file, &refcount);
	req.ptr = e_header;
	kmem_free(&req);
	return -1;
}
Example #12
0
/*
 * Load program "progname" and start running it in usermode.
 * Does not return except on error.
 *
 * Calls vfs_open on progname and thus may destroy it.
 */
int
runprogram(char *progname)
{
	struct vnode *v;
	vaddr_t entrypoint, stackptr;
	int result;

	/* Open the file. */
	result = vfs_open(progname, O_RDONLY, &v);
	if (result) {
		return result;
	}

	/* We should be a new thread. */
	assert(curthread->t_vmspace == NULL);

	/* Create a new address space. */
	curthread->t_vmspace = as_create();
	if (curthread->t_vmspace==NULL) {
		vfs_close(v);
		return ENOMEM;
	}

	//kprintf("\n in runprogram");
	assignPid();
	/* Activate it. */
	as_activate(curthread->t_vmspace);

	/* Load the executable. */
	result = load_elf(v, &entrypoint);
	if (result) {
		/* thread_exit destroys curthread->t_vmspace */
		vfs_close(v);
		return result;
	}

	/* Done with the file now. */
	vfs_close(v);

	/* Define the user stack in the address space */
	result = as_define_stack(curthread->t_vmspace, &stackptr);
	if (result) {
		/* thread_exit destroys curthread->t_vmspace */
		return result;
	}

	/* Warp to user mode. */
	md_usermode(0 /*argc*/, NULL /*userspace addr of argv*/,
		    stackptr, entrypoint);
	
	/* md_usermode does not return */
	panic("md_usermode returned\n");
	return EINVAL;
}
Example #13
0
/*
 * Cause the current thread to exit.
 *
 * We clean up the parts of the thread structure we don't actually
 * need to run right away. The rest has to wait until thread_destroy
 * gets called from exorcise().
 */
void
thread_exit(void)
{
	if (curthread->t_stack != NULL) {
		/*
		 * Check the magic number we put on the bottom end of
		 * the stack in thread_fork. If these assertions go
		 * off, it most likely means you overflowed your stack
		 * at some point, which can cause all kinds of
		 * mysterious other things to happen.
		 */
		assert(curthread->t_stack[0] == (char)0xae);
		assert(curthread->t_stack[1] == (char)0x11);
		assert(curthread->t_stack[2] == (char)0xda);
		assert(curthread->t_stack[3] == (char)0x33);
	}
        #if OPT_A2
        vfs_close(curthread->ft[0]->file);
        vfs_close(curthread->ft[1]->file);
        vfs_close(curthread->ft[2]->file);
        #endif
	splhigh();

	if (curthread->t_vmspace) {
		/*
		 * Do this carefully to avoid race condition with
		 * context switch code.
		 */
		struct addrspace *as = curthread->t_vmspace;
		curthread->t_vmspace = NULL;
		as_destroy(as);
	}

	if (curthread->t_cwd) {
		VOP_DECREF(curthread->t_cwd);
		curthread->t_cwd = NULL;
	}

	assert(numthreads>0);

	numthreads--;
       /* #if OPT_A2
        struct process* current = p_table[curthread->pid];
        if (current->ppid != -1){
           struct process* parent = p_table[current->ppid];
           if (parent->waiting) cv_broadcast(parent->exit,parent->exitlock);
        }         
        #endif
        */
	mi_switch(S_ZOMB);

	panic("Thread came back from the dead!\n");
}
Example #14
0
File: cp.c Project: Masshat/almos
error_t cp_func(void *param)
{
  char *src_name;
  char *dest_name;
  error_t err;
  uint32_t argc;
  uint32_t i;
  ssize_t size;
  struct vfs_file_s *src_file;
  struct vfs_file_s *dest_file;
  ms_args_t *args;
  uint_t count;			/* FIXME to be correctly used */

  args  = (ms_args_t *) param;
  argc = args->argc;
  dest_name = args->argv[argc - 1];
  err = 0;
  
  if((err=vfs_open(ms_n_cwd,dest_name,VFS_O_WRONLY | VFS_O_CREATE,0,&dest_file)))
    return err;
  
  argc --;

  for(i=1; i< argc; i++)
  {
    src_name = args->argv[i];
    
    if((err=vfs_open(ms_n_cwd,src_name,VFS_O_RDONLY,0,&src_file))){
      vfs_close(dest_file, &count);
      ksh_print("error while opinig %s\n",src_name);
      return err;
    }
    size = 0;
    while((size = vfs_read(src_file,buffer,BUFFER_SIZE)) > 0)
    {
      if((size=vfs_write(dest_file,buffer,size)) < 0)
	goto CP_FUNC_ERROR;
    }
    
    if(size < 0) goto CP_FUNC_ERROR;
    vfs_close(src_file, &count);
  }
  vfs_close(dest_file,&count);
  return 0;

 CP_FUNC_ERROR:
  vfs_close(src_file,&count);
  vfs_close(dest_file,&count);
  
 return size;
}
Example #15
0
static void xsync_handle(struct xsync_ctx_t * ctx)
{
	uint8_t buf[PACKET_DATA_MAX];
	size_t size;

	switch(ctx->packet.command)
	{
	case XSYNC_COMMAND_ALIVE:
		size = sprintf((char *)buf, "%s", machine_uniqueid());
		xsync_put(XSYNC_COMMAND_ALIVE, buf, size);
		break;

	case XSYNC_COMMAND_START:
		buf[0] = xsync_handle_start(ctx);
		xsync_put(XSYNC_COMMAND_START, buf, 1);
		break;

	case XSYNC_COMMAND_TRANSFER:
		vfs_write(ctx->fd, (void *)ctx->packet.data, packet_dsize(&ctx->packet));
		xsync_put(XSYNC_COMMAND_TRANSFER, 0, 0);
		break;

	case XSYNC_COMMAND_STOP:
		if(ctx->fd > 0)
		{
			vfs_close(ctx->fd);
			ctx->fd = -1;
		}
		xsync_put(XSYNC_COMMAND_STOP, 0, 0);
		break;

	case XSYNC_COMMAND_SYSTEM:
		xsync_put(XSYNC_COMMAND_SYSTEM, 0, 0);
		ctx->quit = 1;
		if(ctx->fd > 0)
		{
			vfs_close(ctx->fd);
			ctx->fd = -1;
		}
		memset(buf, 0, sizeof(buf));
		memcpy(buf, &ctx->packet.data[0], packet_dsize(&ctx->packet));
		shell_system((const char *)buf);
		break;

	default:
		xsync_put(XSYNC_COMMAND_UNKOWN, 0, 0);
		break;
	}
}
BOOL ui_find_usb_pgdata_file(void)
{
  hfile_t file = NULL;
  channel_data_t * p_channel_data = NULL;
  u32 channel_data_size = 0;
  u16 filename[MAX_DB_FILE_NAME_LENGTH] = {0};
  u32 len = 0;
  
  ui_dbase_mk_db_filename(filename, IW_CHANNEL_DATA_FILE_NAME);
  
  channel_data_size = sizeof(channel_data_t);
  p_channel_data = mtos_malloc(channel_data_size);
  if(NULL == p_channel_data)
  {
    OS_PRINTF("@@ %s, p_channel_data malloc failed, size = %d \n", __FUNCTION__, channel_data_size);
    return FALSE;
  }
  memset(p_channel_data, 0, channel_data_size);
  
  //open file --
  file = vfs_open(filename, VFS_READ);
  if(file != NULL)
  {
    len = vfs_read(p_channel_data, channel_data_size,  1, file);
    if(len != channel_data_size)
    {
      OS_PRINTF("@@ %s :%d \n", __FUNCTION__, __LINE__);  
      vfs_close(file);
      mtos_free(p_channel_data);
      return FALSE;
    }
  }
  else
  {
    OS_PRINTF("@@ %s :%d \n", __FUNCTION__, __LINE__);  
    vfs_close(file);
    mtos_free(p_channel_data);
    return FALSE;
  }
  
  if(p_channel_data->total_pg_num == 0 || p_channel_data->total_tp_num == 0)
  {
    mtos_free(p_channel_data);
    return FALSE;
  }
  mtos_free(p_channel_data);
  return TRUE;
}
Example #17
0
File: mad.c Project: wangeek/Egoboo
//--------------------------------------------------------------------------------------------
mad_t * action_check_copy_vfs( mad_t * pmad, const char* loadname )
{
    /// @details ZZ@> This function copies a model's actions

    vfs_FILE *fileread;
    int actiona, actionb;
    char szOne[16] = EMPTY_CSTR;
    char szTwo[16] = EMPTY_CSTR;

    if ( NULL == pmad ) return pmad;

    fileread = vfs_openRead( loadname );
    if ( NULL == fileread ) return pmad;

    while ( goto_colon( NULL, fileread, btrue ) )
    {
        fget_string( fileread, szOne, SDL_arraysize( szOne ) );
        actiona = action_which( szOne[0] );

        fget_string( fileread, szTwo, SDL_arraysize( szTwo ) );
        actionb = action_which( szTwo[0] );

        action_copy_correct( pmad, actiona + 0, actionb + 0 );
        action_copy_correct( pmad, actiona + 1, actionb + 1 );
        action_copy_correct( pmad, actiona + 2, actionb + 2 );
        action_copy_correct( pmad, actiona + 3, actionb + 3 );
    }

    vfs_close( fileread );

    return pmad;
}
Example #18
0
File: dup.c Project: Nakrez/zOS
int vfs_dup2(struct thread *t, int oldfd, int newfd)
{
    int ret;
    struct process *p = get_parent(t);

    if (!process_fd_exist(p, oldfd))
        return -EBADF;

    if (newfd < 0 || newfd >= PROCESS_MAX_OPEN_FD)
        return -EBADF;

    if (p->files[newfd].used) {
        ret = vfs_close(t, newfd);
        if (ret < 0)
            return ret;
    }

    spinlock_lock(&p->files_lock);

    p->files[newfd].used = 1;

    spinlock_unlock(&p->files_lock);

    ret = process_dup_file(&p->files[newfd], &p->files[oldfd]);
    if (ret < 0) {
        process_free_fd(p, newfd);
        return ret;
    }

    return newfd;
}
Example #19
0
int sys_close (uint_t fd)
{
	register struct task_s *task;
	register struct thread_s *this;
	struct vfs_file_s *file;
	error_t err;
  
	file = NULL;
	this = current_thread;
	task = current_task;

	if((fd >= CONFIG_TASK_FILE_MAX_NR) || (task_fd_lookup(task, fd, &file)))
	{
		this->info.errno = EBADFD;
		return -1;
	}

	if((err = vfs_close(file, NULL)))
	{
		this->info.errno = err;
		return -1;
	}

	task_fd_put(task, fd);
	cpu_wbflush();
	return 0;
}
Example #20
0
int 
sys_close(int fd) {
  int i;
  struct file_table *tmp;
  if (fd < 0 || fd >= MAX_FILE_DESCRIPTOR)
    return EBADF;
  if (curthread->fd[fd] == NULL)
    return EBADF;
  lock_acquire(curthread->fd[fd]->mutex);
  
  curthread->fd[fd]->refcnt--;
  // Returns void; prints for hard I/O errors so no way to return them
  if (curthread->fd[fd]->refcnt == 0)
    vfs_close(curthread->fd[fd]->file);
  
  // Free contents of struct (vnode should be freed by vfs_close)
  if (curthread->fd[fd]->refcnt == 0){
    lock_release(curthread->fd[fd]->mutex);
    lock_destroy(curthread->fd[fd]->mutex);
    kfree(curthread->fd[fd]);

    // Clear all references to this table
    tmp = curthread->fd[fd];
    for (i=0; i<MAX_FILE_DESCRIPTOR; i++){
      if (curthread->fd[i] == tmp)
        curthread->fd[fd] = NULL;
    }
  }
  else
    lock_release(curthread->fd[fd]->mutex);
    curthread->fd[fd] = NULL;
  return 0;
}
Example #21
0
static int touch(int argc, char *argv[])
{
    if(argc < 2) {
        printf("Usage: %s [file...]\n", argv[0]);
        return 1;
    }

    vfs_handle_t vh;
    errval_t err;
    int ret = 0;

    for (int i = 1; i < argc; i++) {
        char *path = vfs_path_mkabsolute(cwd, argv[i]);
        err = vfs_create(path, &vh);
        free(path);
        if (err_is_fail(err)) {
            printf("%s: %s\n", argv[i], err_getstring(err));
            DEBUG_ERR(err, "vfs_create failed");
            ret = 1;
            continue;
        }

        err = vfs_close(vh);
        if (err_is_fail(err)) {
            DEBUG_ERR(err, "in vfs_close");
        }
    }

    return ret;
}
Example #22
0
File: file.c Project: gapry/os161
/* 
 * file_close
 * Called when a process closes a file descriptor.  
 */
int
file_close(int fd)
{
    DEBUG(DB_VFS, "*** Closing fd %d\n", fd);
    
    struct filetable *ft = curthread->t_filetable;
    spinlock_acquire(&ft->ft_spinlock);
    
    /* if fd is not a valid file descriptor, return error */
    if ((fd < 0) || (fd >= __OPEN_MAX) || (ft->ft_entries[fd] == NULL) ||
            (ft->ft_entries[fd]->ft_vnode == NULL)) {
        spinlock_release(&ft->ft_spinlock);
        return EBADF;
    }
    
    /* If there is no other fd pointing to this entry, close the file. */
    ft->ft_entries[fd]->ft_count--;
    if (ft->ft_entries[fd]->ft_count == 0) {
        vfs_close(ft->ft_entries[fd]->ft_vnode);
        kfree(ft->ft_entries[fd]);
    }
    
    /*Remove entry from file table. */
    ft->ft_entries[fd] = NULL;
    spinlock_release(&ft->ft_spinlock);
    
    return 0;
}
Example #23
0
int access(const char *pathname, int mode)
{
    vfs_handle_t vh;
    errval_t err;
    int ret;

    char *path = vfs_path_mkabs(pathname);
    assert(path != NULL);

    err = vfs_open(path, &vh);
    if (err_is_fail(err)) {
        if(err_no(err) == FS_ERR_NOTFILE) {
            // Is it a directory?
            err = vfs_opendir(path, &vh);
            if(err_is_ok(err)) {
                vfs_closedir(vh);
                ret = 0;
                goto out;
            }
        }
        POSIXCOMPAT_DEBUG("access(%s) failed\n", pathname);
        ret = -1;
    } else {
        POSIXCOMPAT_DEBUG("access(%s): OK\n", pathname);
        vfs_close(vh);
        ret = 0;
    }

 out:
    free(path);
    return ret;
}
Example #24
0
/*********************************************************************************************************
** Function name:           vfs_dup2
** Descriptions:            复制文件描述符到指定的文件描述符
** input parameters:        fd                  文件描述符
**                          to                  指定的文件描述符
** output parameters:       NONE
** Returned value:          0 OR -1
*********************************************************************************************************/
int vfs_dup2(int fd, int to)
{
    int ret;
    pid_t pid = getpid();

    vfs_file_begin(pid);

    ret = vfs_close(to);
    if (ret < 0) {
        vfs_file_end(pid);
        return ret;
    }

    mutex_lock(&info_lock[pid], 0);

    info->files[to] = file;

    mutex_unlock(&info_lock[pid]);

    atomic_inc(&file->ref);

    atomic_inc(&point->ref);

    vfs_file_end(pid);

    seterrno(0);

    return 0;
}
Example #25
0
File: swap.c Project: Adam-Koza/A3
/*
 * swap_shutdown
 *
 * Destroys data structures and closes the swap vnode.
 */
void
swap_shutdown(void)
{
	lock_destroy(swaplock);
	bitmap_destroy(swapmap);
	vfs_close(swapstore);
}
Example #26
0
static void
vfs_load_file_to_memory (const char *file, void **data, size_t *size)
{
    assert(data != NULL);
    assert(size != NULL);

    errval_t err;
    vfs_handle_t vh;

    err = vfs_open(file, &vh);
    if (err_is_fail(err)) {
        USER_PANIC_ERR(err, "Error opening %s", file);
    }

    struct vfs_fileinfo info;
    err = vfs_stat(vh, &info);
    assert_err(err, "vfs_stat");

    *data = malloc(info.size);
    assert(*data != NULL);

    err = vfs_read(vh, *data, info.size, size);
    assert_err(err, "vfs_read");
    assert(*size == info.size);

    vfs_close(vh);
}
Example #27
0
File: file.c Project: gapry/AOS
static void
filetable_init_end(void *token, int err, struct vnode *vn) {
    cont_filetable_init_t *cont = (cont_filetable_init_t*)token;

    struct openfile *file;

    file = malloc(sizeof(struct openfile));
    if (file == NULL) {
        vfs_close(vn, O_WRONLY);
        cont->callback(cont->token, ENOMEM);
        free(cont);
        return;
    }

    file->of_offset = 0; // console doesn't use offset
    file->of_accmode = O_WRONLY;
    file->of_refcount = 2;
    file->of_vnode = vn;

    cont->filetable->ft_openfiles[STDOUT_FD] = file;
    cont->filetable->ft_openfiles[STDERR_FD] = file;

    cont->callback(cont->token, 0);
    free(cont);
}
Example #28
0
static void cmd_retr(const char *arg, struct tcp_pcb *pcb, struct ftpd_msgstate *fsm)
{
	vfs_file_t *vfs_file;
	vfs_stat_t st;

	vfs_stat(fsm->vfs, arg, &st);
	if (!VFS_ISREG(st.st_mode)) {
		send_msg(pcb, fsm, msg550);
		return;
	}
	vfs_file = vfs_open(fsm->vfs, arg, "rb");
	if (!vfs_file) {
		send_msg(pcb, fsm, msg550);
		return;
	}

	send_msg(pcb, fsm, msg150recv, arg, st.st_size);

	if (open_dataconnection(pcb, fsm) != 0) {
		vfs_close(vfs_file);
		return;
	}

	fsm->datafs->vfs_file = vfs_file;
	fsm->state = FTPD_RETR;
}
Example #29
0
void
httpd_cleanup(void)
{
#ifdef VFS_SUPPORT
  if (STATE->handler == httpd_handle_vfs && STATE->u.vfs.fd)
  {
    printf("httpd: cleaning left-over vfs-handle at %p.\n", STATE->u.vfs.fd);

    vfs_close(STATE->u.vfs.fd);
    STATE->u.vfs.fd = NULL;
  }
#endif /* VFS_SUPPORT */

#ifdef HTTP_SD_DIR_SUPPORT
  if (STATE->handler == httpd_handle_sd_dir && STATE->u.dir.handle)
  {
    fat_close_dir(STATE->u.dir.handle);
    STATE->u.dir.handle = NULL;
  }
#endif /* HTTP_SD_DIR_SUPPORT */

#ifdef HTTPD_SOAP_SUPPORT
  if (STATE->handler == httpd_handle_soap)
    soap_deallocate_context(&STATE->u.soap);
#endif /* HTTPD_SOAP_SUPPORT */

  STATE->handler = NULL;
}
Example #30
0
void op_mkdir(const char **params, int nparams) {
  inode_t *ino = vfs_open(params[0], &dummy_access);

  assert(!ino && "Directory exists!");
    
  /* Find parent. */
  char *str = NULL;
  int i;
  for (i = strlen(params[0]); i >= 0; --i) {
    if (params[0][i] == '/') {
      str = kmalloc(i+1);
      strncpy(str, params[0], i);
      str[i] = '\0';
      break;
    }
  }
  assert(str && "Parent directory not found!");

  ino = vfs_open(str, &dummy_access);
  assert(ino && "Parent directory not found!");

  vfs_mknod(ino, &params[0][i+1], it_dir, 0777, 0, 0);

  ino = vfs_open(params[0], &dummy_access);
  assert(ino && "Directory not found after having created it!");
  
  vfs_close(ino);
}