void getfsbytes(com32sys_t *regs) { int sectors; bool have_more; uint32_t bytes_read; char *buf; struct file *file; uint16_t handle; handle = regs->esi.w[0]; file = handle_to_file(handle); sectors = regs->ecx.w[0] >> SECTOR_SHIFT(file->fs); buf = MK_PTR(regs->es, regs->ebx.w[0]); bytes_read = file->fs->fs_ops->getfssec(file, buf, sectors, &have_more); /* * If we reach EOF, the filesystem driver will have already closed * the underlying file... this really should be cleaner. */ if (!have_more) { _close_file(file); regs->esi.w[0] = 0; } regs->ecx.l = bytes_read; }
__export int open_file(const char *name, int flags, struct com32_filedata *filedata) { int rv; struct file *file; char mangled_name[FILENAME_MAX]; dprintf("open_file %s\n", name); mangle_name(mangled_name, name); rv = searchdir(mangled_name, flags); if (rv < 0) return rv; file = handle_to_file(rv); if (file->inode->mode != DT_REG) { _close_file(file); return -1; } filedata->size = file->inode->size; filedata->blocklg2 = SECTOR_SHIFT(file->fs); filedata->handle = rv; return rv; }
int chdir(const char *src) { int rv; struct file *file; char cwd_buf[CURRENTDIR_MAX]; if (this_fs->fs_ops->chdir) return this_fs->fs_ops->chdir(this_fs, src); /* Otherwise it is a "conventional filesystem" */ rv = searchdir(src); if (rv < 0) return rv; file = handle_to_file(rv); if (file->inode->mode != DT_DIR) { _close_file(file); return -1; } put_inode(this_fs->cwd); this_fs->cwd = get_inode(file->inode); _close_file(file); /* Save the current working directory */ realpath(cwd_buf, src, CURRENTDIR_MAX); /* Make sure the cwd_name ends in a slash, it's supposed to be a prefix */ join_paths(this_fs->cwd_name, CURRENTDIR_MAX, cwd_buf, "/"); return 0; }
__export void close_file(uint16_t handle) { struct file *file; if (handle) { file = handle_to_file(handle); _close_file(file); } }
void pm_searchdir(com32sys_t *regs) { char *name = MK_PTR(regs->ds, regs->edi.w[0]); int rv; rv = searchdir(name); if (rv < 0) { regs->esi.w[0] = 0; regs->eax.l = 0; regs->eflags.l |= EFLAGS_ZF; } else { regs->esi.w[0] = rv; regs->eax.l = handle_to_file(rv)->inode->size; regs->eflags.l &= ~EFLAGS_ZF; } }
/* * Open a directory */ __export DIR *opendir(const char *path) { int rv; struct file *file; rv = searchdir(path, O_RDONLY|O_DIRECTORY); if (rv < 0) return NULL; file = handle_to_file(rv); if (file->inode->mode != DT_DIR) { _close_file(file); return NULL; } return (DIR *)file; }
size_t pmapi_read_file(uint16_t *handle, void *buf, size_t sectors) { bool have_more; size_t bytes_read; struct file *file; file = handle_to_file(*handle); bytes_read = file->fs->fs_ops->getfssec(file, buf, sectors, &have_more); /* * If we reach EOF, the filesystem driver will have already closed * the underlying file... this really should be cleaner. */ if (!have_more) { _close_file(file); *handle = 0; } return bytes_read; }
void pm_open_file(com32sys_t *regs) { int rv; struct file *file; const char *name = MK_PTR(regs->es, regs->esi.w[0]); char mangled_name[FILENAME_MAX]; dprintf("pm_open_file %s\n", name); mangle_name(mangled_name, name); rv = searchdir(mangled_name); if (rv < 0) { regs->eflags.l |= EFLAGS_CF; } else { file = handle_to_file(rv); regs->eflags.l &= ~EFLAGS_CF; regs->eax.l = file->inode->size; regs->ecx.w[0] = SECTOR_SIZE(file->fs); regs->esi.w[0] = rv; } }
void pm_load_high(com32sys_t *regs) { struct fs_info *fs; uint32_t bytes; uint32_t zero_mask; bool have_more; uint32_t bytes_read; char *buf, *limit; struct file *file; uint32_t sector_mask; size_t pad; uint32_t retflags = 0; bytes = regs->eax.l; zero_mask = regs->edx.w[0]; buf = (char *)regs->edi.l; limit = (char *)(regs->ebp.l & ~zero_mask); file = handle_to_file(regs->esi.w[0]); fs = file->fs; sector_mask = SECTOR_SIZE(fs) - 1; while (bytes) { uint32_t sectors; uint32_t chunk; if (buf + SECTOR_SIZE(fs) > limit) { /* Can't fit even one more sector in... */ retflags = EFLAGS_OF; break; } chunk = bytes; if (regs->ebx.w[0]) { call16((void (*)(void))(size_t)regs->ebx.w[0], &zero_regs, NULL); chunk = min(chunk, MAX_CHUNK); } if (chunk > (((char *)limit - buf) & ~sector_mask)) chunk = ((char *)limit - buf) & ~sector_mask; sectors = (chunk + sector_mask) >> SECTOR_SHIFT(fs); bytes_read = fs->fs_ops->getfssec(file, buf, sectors, &have_more); if (bytes_read > chunk) bytes_read = chunk; buf += bytes_read; bytes -= bytes_read; if (!have_more) { /* * If we reach EOF, the filesystem driver will have already closed * the underlying file... this really should be cleaner. */ _close_file(file); regs->esi.w[0] = 0; retflags = EFLAGS_CF; break; } } pad = (size_t)buf & zero_mask; if (pad) memset(buf, 0, pad); regs->ebx.l = (size_t)buf; regs->edi.l = (size_t)buf + pad; set_flags(regs, retflags); }