Exemplo n.º 1
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;
}
Exemplo n.º 2
0
/*
 * swap_bootstrap: Initializes swap information and finishes
 * bootstrapping the VM so that processes can use it.
 *
 * Synchronization: none (runs during boot before anyone else uses VM)
 */
void
swap_bootstrap(size_t pmemsize)
{
	int rv;
	struct stat st;
	char path[sizeof(swapfilename)];
	off_t minsize;

	strcpy(path, swapfilename);
	rv = vfs_open(path, O_RDWR, &swapstore);
	if (rv) {
		kprintf("swap: Error %d opening swapfile %s\n", rv, 
			swapfilename);
		kprintf("swap: Please create swapfile/swapdisk.\n");
		panic("swap: Unable to continue.\n");
	}

	minsize = pmemsize*10;

	VOP_STAT(swapstore, &st);
	if (st.st_size < minsize) {
		kprintf("swap: swapfile %s is only %lu bytes.\n", swapfilename,
			(unsigned long) st.st_size);
		kprintf("swap: with %lu bytes of physical memory it should "
			"be at least\n", (unsigned long) pmemsize);
		kprintf("      %lu bytes (%lu blocks).\n", 
			(unsigned long) minsize, 
			(unsigned long) minsize / 512);
		kprintf("swap: Please extend it.\n");
		panic("swap: Unable to continue.\n");
	}

	kprintf("swap: swapping to %s (%lu bytes; %lu pages)\n", swapfilename,
		(unsigned long) st.st_size, 
		(unsigned long) st.st_size / PAGE_SIZE);

	swap_total_pages = st.st_size / PAGE_SIZE;
	swap_free_pages = swap_total_pages;
	swap_reserved_pages = 0;

	swapmap = bitmap_create(st.st_size/PAGE_SIZE);
	DEBUG(DB_VM, "creating swap map with %d entries\n",
			st.st_size/PAGE_SIZE);
	if (swapmap == NULL) {
		panic("swap: No memory for swap bitmap\n");
	}

	swaplock = lock_create("swaplock");
	if (swaplock == NULL) {
		panic("swap: No memory for swap lock\n");
	}

	/* mark the first page of swap used so we can check for errors */
	bitmap_mark(swapmap, 0);
	swap_free_pages--;
}
Exemplo n.º 3
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;
}
Exemplo n.º 4
0
/* open a file */
void msg_open(message_t msg)
{
	int ret;

	ret = vfs_open(msg->tid, (char *)msg->snd_ptr, (int *)&msg->w4, 0);
	msg->w1 = ret;
//	printf("reply %d ", msg->tid);
//	for(;;);
	reply_message(msg->tid, msg);
}
Exemplo n.º 5
0
void init(void) {
    int8_t *buffer[1024] = { 0 };
    if (vfs_open("/ramdisk/boot/kernel.sym")) {
	vfs_read("/ramdisk/boot/kernel.sym", 0, 0x100, buffer);
	vfs_close("/ramdisk/boot/kernel.sym");
    }
    buffer[1023] = 0;

    log_write("Testing module loading...\n%s\n", buffer);
}
Exemplo n.º 6
0
static int fuse_open(const char *path, struct fuse_file_info *fi)
{
    int fd;

    if((fd = vfs_open(path, fi->flags, 0)) < 0)
        return fd;

    fi->fh = fd;
    return 0;
}
Exemplo n.º 7
0
Arquivo: elf.c Projeto: 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;
}
Exemplo n.º 8
0
void
swapspace_init(){

	for(int itr = 0; itr < MAX_VAL; itr++){
		sw_space[itr].sw_vaddr = 0;
	}
	
	int ret = vfs_open((char*)"lhd0raw:", O_RDWR, 0, &sw_vn);
	KASSERT(ret == 0);
	KASSERT(sw_vn != NULL);
}
Exemplo n.º 9
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;
}
Exemplo n.º 10
0
struct vfs_file_handle_t *
vfs_open_or_creat(const char* filename)
{
    struct vfs_file_handle_t * fd = vfs_open(filename);
    if (! fd ) {
	/* Perhaps the file wasn't existing */
	fd = vfs_create(filename);
    }

    return fd; /* Here fd may be NULL or a filehandle */
}
Exemplo n.º 11
0
void cmd_ls(char *arg)
{
    struct file *file = vfs_open(arg);

    if (!file->isdir) {
        _printk("%s", errno_to_string(ENOTDIR));
        return;
    }

    return;
}
Exemplo n.º 12
0
int sys_open(userptr_t filename, int flags, int mode, int *retval)
{
  static char pathname[PATH_MAX];
  int r,i, fd;
  size_t len;
  struct ofte *oftp;
  struct vnode *vnret;
  
  /* ignore mode */
  (void) mode;
  
  /* copy the pathname into a local buffer */
  r = copyinstr(filename, pathname, PATH_MAX, &len);
  if (r) {
    return r;
  }
  
  /* no synch protection needed as only current thread accesses */
  /* allocate a fd entry */
  
  for (i = 0; i < MAX_FD; i ++){
    if (curthread->t_fdt->fd[i] == NULL) {
      fd = i;
      break;
    }
  }
  if (i == MAX_FD) {
    return EMFILE;
  }
			
  oftp = oft_alloc();
  if (oftp == NULL) {
    curthread->t_fdt->fd[fd] = NULL;
    return ENFILE;
  }
		
  /* call  vfs open */
  r = vfs_open(pathname, flags, &vnret);
  if (r) {
    oft_free(oftp);
    curthread->t_fdt->fd[fd] = NULL;
    return r;
  }
  /* fill in the state */
  curthread->t_fdt->fd[fd] = oftp;
  oftp->vn = vnret;
  oftp->fp = 0;
  oftp->flags = flags;
  *retval = fd;
	
  /* return */
  return 0;  
}
Exemplo n.º 13
0
void kmain(struct multiboot* b, uint32_t magic)
{
	hide_cursor();
	cls();
	setup_gdt();
	setup_idt();
	if(b->mods_count == 1)
	{
		uint32_t mods_start_addr=*(uint32_t*)(b->mods_addr);
		uint32_t mods_end_addr=*(uint32_t*)(b->mods_addr + 4);
		if(((uint32_t)&__kernel_end - KERNEL_VMA) < mods_end_addr) kernel_end_addr=(mods_end_addr & 0xFFFFF000) + 0x1000;
	}
	setup_bitmap();
	setup_vmm();
	setup_pic();
	setup_tasking();
	setup_process_tree();
	set_timer_freq(100);

	pci_init();
	setup_network();

	b=(struct multiboot*)((uint8_t*)b+KERNEL_VMA);
	uint32_t mods_addr=*(uint32_t*)(b->mods_addr + KERNEL_VMA) + KERNEL_VMA;
	root_fs=ext2_fs_init((uint8_t*)mods_addr);
	struct inode *devfs=devfs_init();
	struct inode *devfs_root=vfs_search((struct inode*)root_fs, "/dev");
	if(devfs_root)
	{
		vfs_mount(devfs, devfs_root);
		register_tty_driver();
		register_kbd_driver();
		register_rtl8139_driver();
	}
	else kprintf("Could not mount /dev, no such directory\n");

	kprintf("\n%@Welcome to tapiOS!%@\n\n", 0x05, 0x07, b->mods_count, 0x03);

	struct inode *node=vfs_search((struct inode*)root_fs, "/bin/init");
	if(node)
	{
		struct file *init=vfs_open(node, NULL, O_RDONLY);
		uint8_t *init_mem=kmalloc(node->size);
		int read=vfs_read(init, init_mem, node->size);
		vaddr_t entrypoint=init_elf_get_entry_point(init_mem);
		setup_initial_process(entrypoint);
	}
	else kprintf("Init not found\n");
	__asm__ volatile("hltloop: hlt; jmp hltloop");

	PANIC();
}
Exemplo n.º 14
0
Arquivo: io.c Projeto: mrb852/osm-exam
openfile_t io_open(const char* pathname)
{
  openfile_t file = vfs_open(pathname);
  if (file < 0) {
    return file;
  }
  else {
    process_add_file(file);
    /* Don't return a value that can be mistaken for stdin, stdout, or
       stderr. */
    return file + 3;
  }
}
Exemplo n.º 15
0
int32_t sys_open(context_t *c, uint32_t *args)
{
  // this is going to implement VFS open()
  char *filename = (char *) args[0];
  int flags = (int) args[1];

  c_printf("> open(%s, %d)\n", filename, flags);

  // now well have a filename and flags, have vfs_open find the inode
  vfs_open(filename, flags);

  c->eax = -1;
}
Exemplo n.º 16
0
/////////////////////////////////////////////////////////////////////////////
//
// emu virtual I/O
//
static sint32 EMU_CALL vopen(void *vfsstate, uint8 *ram_native, uint32 ram_size, sint32 ofs) {
  char path[256];
  sint32 i;
  for(i = 0; i < 255; i++) {
    char c = ram_native[(ofs & (ram_size-1)) ^ (EMU_ENDIAN_XOR(3))]; ofs++;
    if(!c) break;
    path[i] = c;
  }
  if(!i) return -2; // ENOENT if path is empty
  path[i] = 0;
  i = vfs_open(vfsstate, path);
  return i;
}
Exemplo n.º 17
0
void
vm_bootstrap(void)
{
	char fname[] = "lhd0raw:";
	vm_lock = lock_create("vm_lock");
	int result = vfs_open(fname, 0, O_RDWR , &swap_file);
	if(result)
    		panic("Virtual Memory: Swap space could not be created \n");
	alloc = (struct alloc_status*)kmalloc(64*sizeof(struct alloc_status));	
	coreswap_init();
	coremap_init();	
	myvm_fl=1;
}
Exemplo n.º 18
0
int sys_open(const char *filename, int file_flag, mode_t mode){
	bool create = false;
        if(filename == NULL || !(valid_address_check(curproc->p_addrspace, (vaddr_t)filename))){ //bad memory reference
                errno = EFAULT;
                return -1;
        }
		if(file_flag > 94 || file_flag % 4 == 3 || file_flag & O_APPEND){
			errno = EINVAL;
			return -1;
		}
		/*
       if(file_flag & O_APPEND){ //flags contained invalid values
                errno = EINVAL;
                return -1;
        }*/
	struct vnode* new_file;
	int ret;
	if (curproc->open_num < MAX_fd_table){	// fd table is available
		ret = vfs_open((char *)filename, file_flag, mode , &new_file);	// open file when table has free space
		curproc->open_num++;
		if (ret == 0){
			if ((file_flag & O_CREAT) && (file_flag & O_EXCL)){
				errno = EEXIST;
				return -1;
			}
		}
		else{
			create = true;
			if (file_flag & ~O_CREAT){
				errno = ENOENT;
				return -1;
			}
		}
	}
	else{	// if table is full
		if (create) errno = ENOSPC;
		else errno = EMFILE;
		return -1;
	}
        
        int file_handle = 3; //file handle is the index at which the fd is located
        
        while(curproc->fd_table[file_handle] != NULL) { //find empty slot in fd_table
                file_handle++;
        }
	struct fd* f = create_fd(file_flag, filename, new_file);
        add_fd(f,file_handle);

        return file_handle;  //index of the fd in the fd_fd_table
}
Exemplo n.º 19
0
void keyboard_init()
{
  INODE tmp[2];
  new_pipe(1024, tmp);
  keyboard_pipe = tmp[1];
  /* vfs_mount("/dev/kbd", tmp[0]); */
  vfs_open(keyboard_pipe, O_WRONLY);

  // Make keyboard stdin (first entry in file descriptor table)
  process_t *p = current->proc;
  p->fd[0] = calloc(1, sizeof(file_desc_t));
  fd_get(p->fd[0]);
  p->fd[0]->ino = tmp[0];
  p->fd[0]->flags = O_RDONLY;
  vfs_open(tmp[0], O_RDONLY);

  new_pipe(1024, tmp);
  keyboard_raw = tmp[1];
  vfs_mount("/dev/kbdraw", tmp[0]);
  vfs_open(keyboard_raw, O_WRONLY);

  register_int_handler(IRQ2INT(IRQ_KBD), keyboard_handler);
}
Exemplo n.º 20
0
/*
 * Initialize the virtual memory module
 */
void vm_bootstrap(void)
{    
    //open the disk as a file and use the whle disk0 as the swap space.
    char file_name[] = SWAP_FILE;     
    int result = vfs_open(file_name, O_RDWR , &swap_fp);
    if(result)
        panic("VM: Failed to create Swap area\n");
    
    //initialize the paging mechanism
    init();
    if(PAGE_REPLACEMENT_ALGO == LRU)
	    kprintf("Page replacement algorithm: LRU\n\n");
    else
	    kprintf("Page replacement algorithm: RANDOM\n\n");
}
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;
}
Exemplo n.º 22
0
u64 file_list_get_file_size(u16 *p_name)
{
  hfile_t file = NULL;
  vfs_file_info_t file_info = {0};

  file = vfs_open(p_name, VFS_READ);
  if (file == NULL)
  {
    return 0;
  }

  vfs_get_file_info(file, &file_info);
  vfs_close(file);

  return file_info.file_size;
}
int filetable_addentry(struct proc* process, char* filename, int flags,
		int mode, int * new_fd) {
	struct vnode* vn;
	int result = 0;
	mode = 0;
	result = vfs_open(filename, flags, mode, &vn);
	if (result) {
		// kprintf("TEMPPPP: FiletableENtry failed\n");
		return result;
	}

	*new_fd = filetable_addentryforvnode(process, flags, vn);;

	return 0;

}
Exemplo n.º 24
0
Arquivo: file.c Projeto: gapry/os161
/*
 * file_open
 * opens a file, places it in the filetable, sets RETFD to the file
 * descriptor. the pointer arguments must be kernel pointers.
 * NOTE -- the passed in filename must be a mutable string.
 * 
 * A4: As per the OS/161 man page for open(), you do not need 
 * to do anything with the "mode" argument.
 */
int
file_open(char *filename, int flags, int mode, int *retfd)
{
    DEBUG(DB_VFS, "*** Opening file %s\n", filename);
    
    /* Check if flags is valid. */
    int how = flags & O_ACCMODE;
    if ((how != O_RDONLY) && (how != O_WRONLY) && (how != O_RDWR)) {
        return EINVAL;
    }
    
    /* Find a NULL entry in filetable. */
    int fd;
    struct filetable *ft = curthread->t_filetable;
    
	spinlock_acquire(&ft->ft_spinlock);
    for (fd = 0; fd < __OPEN_MAX; fd++) {
        if (ft->ft_entries[fd] == NULL) {
            break;
        }
    }
    
    /* File table is full. */
    if (fd == __OPEN_MAX) {
        spinlock_release(&ft->ft_spinlock);
        return EMFILE;
    }
    
    /* Open file. */
    struct vnode *new_vnode = NULL;
    int result = vfs_open(filename, flags, mode, &new_vnode);
    if (result > 0) {
        spinlock_release(&ft->ft_spinlock);
        return result;
    }
    
    ft->ft_entries[fd] = (struct filetable_entry *)kmalloc(sizeof(struct filetable_entry));
    ft->ft_entries[fd]->ft_vnode = new_vnode;
    ft->ft_entries[fd]->ft_pos = 0;
    ft->ft_entries[fd]->ft_flags = flags;
    ft->ft_entries[fd]->ft_count = 1;
    
    *retfd = fd;
    
    spinlock_release(&ft->ft_spinlock);
    return 0;
}
Exemplo n.º 25
0
static
void
dowritestress2(const char *filesys)
{
	int i, err;
	char name[32];
	struct vnode *vn;

	init_threadsem();

	kprintf("*** Starting fs write stress test 2 on %s:\n", filesys);

	/* Create and truncate test file */
	fstest_makename(name, sizeof(name), filesys, "");
	err = vfs_open(name, O_WRONLY|O_CREAT|O_TRUNC, 0664, &vn);
	if (err) {
		kprintf("Could not create test file: %s\n", strerror(err));
		kprintf("*** Test failed\n");
		return;
	}
	vfs_close(vn);

	for (i=0; i<NTHREADS; i++) {
		err = thread_fork("writestress2", NULL,
				  writestress2_thread, (char *)filesys, i);
		if (err) {
			panic("writestress2: thread_fork failed: %s\n",
			      strerror(err));
		}
	}

	for (i=0; i<NTHREADS; i++) {
		P(threadsem);
	}

	if (fstest_read(filesys, "")) {
		kprintf("*** Test failed\n");
		return;
	}

	if (fstest_remove(filesys, "")) {
		kprintf("*** Test failed\n");
	}


	kprintf("*** fs write stress test 2 done\n");
}
Exemplo n.º 26
0
static void walk_dir(char* path)
{
    printf("%s:%d: path=%s\n", __FUNCTION__, __LINE__, path);
    vfs_handle_t dhandle;
    struct vfs_fileinfo info;
    
    errval_t err = vfs_opendir(path, &dhandle);
    assert(err_is_ok(err));

    char* name;
    while(err_is_ok(vfs_dir_read_next(dhandle, &name, &info))) {

        if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
            continue;
        char* newpath = malloc(snprintf(NULL, 0, "%s/%s", path, name) + 1);
        sprintf(newpath, "%s/%s", path, name);

        switch (info.type) {
        case VFS_DIRECTORY:
            printf("%s:%d: Found directory %s\n", __FUNCTION__, __LINE__, newpath);
            walk_dir(newpath);
            break;
        
        case VFS_FILE:
            printf("%s:%d: Found file %s\n", __FUNCTION__, __LINE__, newpath);
            vfs_handle_t fhandle;

            err = vfs_open(newpath, &fhandle);
            char* buf = malloc(info.size);
            size_t bytes_read = 0;
            err = vfs_read(fhandle, buf, info.size, &bytes_read);
            assert(err_is_ok(err));
            assert(bytes_read == info.size);
            printf("%s:%d: File content is (bytes=%d):\n%s\n", 
                   __FUNCTION__, __LINE__, bytes_read, buf);

            free(buf);
            break;
        }
        free(name);
        free(newpath);
    }

    err = vfs_closedir(dhandle);
    assert(err_is_ok(err));   
}
Exemplo n.º 27
0
int16_t
parse_cmd_call(char *cmd, char *output, uint16_t len)
{
  char filename[10];
  char line[ECMD_INPUTBUF_LENGTH];
  uint8_t lsize = 0;
  uint8_t run = 0;
  vfs_size_t filesize;

  sscanf_P(cmd, PSTR("%s"), &filename); // should check for ".es" extention!
  current_script.handle = vfs_open(filename);

  if (current_script.handle == NULL)
  {
    SCRIPTDEBUG("%s not found\n", filename);
    return ECMD_FINAL(1);
  }

  filesize = vfs_size(current_script.handle);

  SCRIPTDEBUG("start %s from %i bytes\n", filename, filesize);
  current_script.linenumber = 0;
  current_script.filepointer = 0;

  // open file as long it is open, we have not reached max lines and 
  // not the end of the file as we know it
  while ((current_script.handle != NULL) &&
         (run++ < ECMD_SCRIPT_MAXLINES) &&
         (filesize > current_script.filepointer))
  {

    lsize = readline(line);
    SCRIPTDEBUG("(linenr:%i, pos:%i, bufsize:%i)\n",
                current_script.linenumber, current_script.filepointer, lsize);
    SCRIPTDEBUG("exec: %s\n", line);
    if (lsize != 0)
    {
      ecmd_parse_command(line, output, len);
    }
  }
  SCRIPTDEBUG("end\n");

  parse_cmd_exit(cmd, output, len);

  return ECMD_FINAL_OK;
}
Exemplo n.º 28
0
int file_open(char *path, uint32_t open_flags)
{
	bool readable = 0, writable = 0;
	switch (open_flags & O_ACCMODE) {
	case O_RDONLY:
		readable = 1;
		break;
	case O_WRONLY:
		writable = 1;
		break;
	case O_RDWR:
		readable = writable = 1;
		break;
	default:
		return -E_INVAL;
	}

	int ret;
	struct file *file;
	if ((ret = filemap_alloc(NO_FD, &file)) != 0) {
		return ret;
	}

	struct inode *node;
	if ((ret = vfs_open(path, open_flags, &node)) != 0) {
		filemap_free(file);
		return ret;
	}

	file->pos = 0;
	if (open_flags & O_APPEND) {
		struct stat __stat, *stat = &__stat;
		if ((ret = vop_fstat(node, stat)) != 0) {
			vfs_close(node);
			filemap_free(file);
			return ret;
		}
		file->pos = stat->st_size;
	}

	file->node = node;
	file->readable = readable;
	file->writable = writable;
	filemap_open(file);
	return file->fd;
}
Exemplo n.º 29
0
static int esp8266_Open( sqlite3_vfs * vfs, const char * path, sqlite3_file * file, int flags, int * outflags )
{
	int rc;
	char *mode = "r";
	esp8266_file *p = (esp8266_file*) file;

	if ( path == NULL ) return SQLITE_IOERR;
	if( flags&SQLITE_OPEN_READONLY )  mode = "r";
	if( flags&SQLITE_OPEN_READWRITE || flags&SQLITE_OPEN_MAIN_JOURNAL ) {
		int result;
		if (SQLITE_OK != esp8266_Access(vfs, path, flags, &result))
			return SQLITE_CANTOPEN;

		if (result == 1)
			mode = "r+";
		else
			mode = "w+";
	}

	dbg_printf("esp8266_Open: 1o %s %s\n", path, mode);
	memset (p, 0, sizeof(esp8266_file));

        strncpy (p->name, path, ESP8266_DEFAULT_MAXNAMESIZE);
	p->name[ESP8266_DEFAULT_MAXNAMESIZE-1] = '\0';

	if( flags&SQLITE_OPEN_MAIN_JOURNAL ) {
		p->fd = 0;
		p->cache = sqlite3_malloc(sizeof (filecache_t));
		if (! p->cache )
			return SQLITE_NOMEM;
		memset (p->cache, 0, sizeof(filecache_t));

		p->base.pMethods = &esp8266MemMethods;
		dbg_printf("esp8266_Open: 2o %s %d MEM OK\n", p->name, p->fd);
		return SQLITE_OK;
	}

	p->fd = vfs_open (path, mode);
	if ( p->fd <= 0 ) {
		return SQLITE_CANTOPEN;
	}

	p->base.pMethods = &esp8266IoMethods;
	dbg_printf("esp8266_Open: 2o %s %d OK\n", p->name, p->fd);
	return SQLITE_OK;
}
Exemplo n.º 30
0
int
sys_open(const char *filename, int flags, int *fd) {
  lock_acquire(curproc->opencloselock);
  struct vnode* filenode;
  int filedescriptor = -1;

  //vfs_open returns the vnode in filenode, and returns a integer value denoting it's success.
  char *temp = NULL;
  temp = kstrdup(filename);
  int result = vfs_open(temp, flags, 0, &(filenode));
  if(result != 0) {
    lock_release(curproc->opencloselock);
    return result;
  }
  kfree(temp);
  KASSERT(filenode != NULL);

  //make our file, a wrapper for vnode. Stores vnode, lock, ect.. May be useful later.
  struct file* f;
  f = initfile(filenode, flags, filename);
  KASSERT(f != NULL);
  KASSERT(f->fvnode != NULL);

  //go through the file table, find an unused filedescriptor.
  for(int i = 0; i < 128; i++) {
    if(array_get(curproc->filetable, i) == NULL) {
      filedescriptor = i;
      break;
    }
  }

  //if fd>=0 then a file descriptor was found. Else file table is probably full.
  if(filedescriptor >= 0) {
    array_set(curproc->filetable, filedescriptor, f);
    *fd = filedescriptor;
  } else {
    //kprintf("RAN OUT OF FD FD %d \n", *fd);
    lock_release(curproc->opencloselock);
    cleanupfile(f);
    return EMFILE;
  }
 // kprintf("OPENED FD %d \n", *fd);
  lock_release(curproc->opencloselock);
  return 0;
}