int os_find_u_boot(char *fname, int maxlen) { struct sandbox_state *state = state_get_current(); const char *progname = state->argv[0]; int len = strlen(progname); const char *suffix; char *p; int fd; if (len >= maxlen || len < 4) return -ENOSPC; strcpy(fname, progname); suffix = fname + len - 4; /* If we are TPL, boot to SPL */ if (!strcmp(suffix, "-tpl")) { fname[len - 3] = 's'; fd = os_open(fname, O_RDONLY); if (fd >= 0) { close(fd); return 0; } /* Look for 'u-boot-tpl' in the tpl/ directory */ p = strstr(fname, "/tpl/"); if (p) { p[1] = 's'; fd = os_open(fname, O_RDONLY); if (fd >= 0) { close(fd); return 0; } } return -ENOENT; } /* Look for 'u-boot' in the same directory as 'u-boot-spl' */ if (!strcmp(suffix, "-spl")) { fname[len - 4] = '\0'; fd = os_open(fname, O_RDONLY); if (fd >= 0) { close(fd); return 0; } } /* Look for 'u-boot' in the parent directory of spl/ */ p = strstr(fname, "/spl/"); if (p) { strcpy(p, p + 4); fd = os_open(fname, O_RDONLY); if (fd >= 0) { close(fd); return 0; } } return -ENOENT; }
/* * util_file_map_whole -- maps the entire file into memory */ void * util_file_map_whole(const char *path) { LOG(3, "path \"%s\"", path); int fd; int olderrno; void *addr = NULL; if ((fd = os_open(path, O_RDWR)) < 0) { ERR("!open \"%s\"", path); return NULL; } ssize_t size = util_file_get_size(path); if (size < 0) { LOG(2, "cannot determine file length \"%s\"", path); goto out; } addr = util_map(fd, (size_t)size, MAP_SHARED, 0, 0, NULL); if (addr == NULL) { LOG(2, "failed to map entire file \"%s\"", path); goto out; } out: olderrno = errno; (void) os_close(fd); errno = olderrno; return addr; }
static int server_open( u8 *p ) { const char *filename; int mode, flags, fd; char separator[ 2 ] = { PLATFORM_PATH_SEPARATOR, 0 }; // Validate request log_msg( "server_open: request handler starting\n" ); if( remotefs_open_read_request( p, &filename, &flags, &mode ) == ELUARPC_ERR ) { log_msg( "server_open: unable to read request\n" ); return SERVER_ERR; } // Get real filename server_fullname[ 0 ] = server_fullname[ PLATFORM_MAX_FNAME_LEN ] = 0; strncpy( server_fullname, server_basedir, PLATFORM_MAX_FNAME_LEN ); if( filename && strlen( filename ) > 0 ) { if( server_fullname[ strlen( server_fullname ) - 1 ] != PLATFORM_PATH_SEPARATOR ) strncat( server_fullname, separator, PLATFORM_MAX_FNAME_LEN ); strncat( server_fullname, filename, PLATFORM_MAX_FNAME_LEN ); } log_msg( "server_open: full file path is %s\n", server_fullname ); fd = os_open( server_fullname, flags, mode ); log_msg( "server_open: OS file handler is %d\n", fd ); remotefs_open_write_response( p, fd ); return SERVER_OK; }
int sandbox_fs_write_at(const char *filename, loff_t pos, void *buffer, loff_t towrite, loff_t *actwrite) { ssize_t size; int fd, ret; fd = os_open(filename, OS_O_RDWR | OS_O_CREAT); if (fd < 0) return fd; ret = os_lseek(fd, pos, OS_SEEK_SET); if (ret == -1) { os_close(fd); return ret; } size = os_write(fd, buffer, towrite); os_close(fd); if (size == -1) { ret = -1; } else { ret = 0; *actwrite = size; } return ret; }
/* * util_file_create -- create a new memory pool file */ int util_file_create(const char *path, size_t size, size_t minsize) { LOG(3, "path \"%s\" size %zu minsize %zu", path, size, minsize); ASSERTne(size, 0); if (size < minsize) { ERR("size %zu smaller than %zu", size, minsize); errno = EINVAL; return -1; } if (((os_off_t)size) < 0) { ERR("invalid size (%zu) for os_off_t", size); errno = EFBIG; return -1; } int fd; int mode; int flags = O_RDWR | O_CREAT | O_EXCL; #ifndef _WIN32 mode = 0; #else mode = S_IWRITE | S_IREAD; flags |= O_BINARY; #endif /* * Create file without any permission. It will be granted once * initialization completes. */ if ((fd = os_open(path, flags, mode)) < 0) { ERR("!open \"%s\"", path); return -1; } if ((errno = os_posix_fallocate(fd, 0, (os_off_t)size)) != 0) { ERR("!posix_fallocate \"%s\", %zu", path, size); goto err; } /* for windows we can't flock until after we fallocate */ if (os_flock(fd, OS_LOCK_EX | OS_LOCK_NB) < 0) { ERR("!flock \"%s\"", path); goto err; } return fd; err: LOG(4, "error clean up"); int oerrno = errno; if (fd != -1) (void) os_close(fd); os_unlink(path); errno = oerrno; return -1; }
s32 DI_Config_Save(struct dipConfigState *cfg) { s32 fd, ret; /* Create config file */ __Config_Create(__dip_cfg_filename); /* Open config file */ fd = os_open(__dip_cfg_filename, ISFS_OPEN_WRITE); if (fd < 0) return fd; /* Write config */ ret = os_write(fd, cfg, sizeof(*cfg)); /* Write frag list */ if (ret == sizeof(*cfg)) { if (cfg->mode == MODE_FRAG) { s32 ret2 = os_write(fd, &fraglist_data, cfg->frag_size); if (ret2 < 0) ret = ret2; else ret += ret2; } } /* Close config */ os_close(fd); return ret; }
/// Check what `name` is: /// @return NODE_NORMAL: file or directory (or doesn't exist) /// NODE_WRITABLE: writable device, socket, fifo, etc. /// NODE_OTHER: non-writable things int os_nodetype(const char *name) { #ifdef WIN32 // Edge case from Vim os_win32.c: // We can't open a file with a name "\\.\con" or "\\.\prn", trying to read // from it later will cause Vim to hang. Thus return NODE_WRITABLE here. if (STRNCMP(name, "\\\\.\\", 4) == 0) { return NODE_WRITABLE; } #endif uv_stat_t statbuf; if (0 != os_stat(name, &statbuf)) { return NODE_NORMAL; // File doesn't exist. } #ifndef WIN32 // libuv does not handle BLK and DIR in uv_handle_type. // Related: https://github.com/joyent/libuv/pull/1421 if (S_ISREG(statbuf.st_mode) || S_ISDIR(statbuf.st_mode)) { return NODE_NORMAL; } if (S_ISBLK(statbuf.st_mode)) { // block device isn't writable return NODE_OTHER; } #endif // Vim os_win32.c:mch_nodetype does this (since patch 7.4.015): // if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) { // wn = enc_to_utf16(name, NULL); // hFile = CreatFile(wn, ...) // to get a HANDLE. But libuv just calls win32's _get_osfhandle() on the fd we // give it. uv_fs_open calls fs__capture_path which does a similar dance and // saves us the hassle. int nodetype = NODE_WRITABLE; int fd = os_open(name, O_RDONLY, 0); switch(uv_guess_handle(fd)) { case UV_TTY: // FILE_TYPE_CHAR nodetype = NODE_WRITABLE; break; case UV_FILE: // FILE_TYPE_DISK nodetype = NODE_NORMAL; break; case UV_NAMED_PIPE: // not handled explicitly in Vim os_win32.c case UV_UDP: // unix only case UV_TCP: // unix only case UV_UNKNOWN_HANDLE: default: #ifdef WIN32 nodetype = NODE_NORMAL; #else nodetype = NODE_WRITABLE; // Everything else is writable? #endif break; } close(fd); return nodetype; }
bool usbstorage_Init(void) { s32 ret; /* Already open */ if (fd >= 0) return true; /* Open USB device */ fd = os_open(fs, 0); if (fd < 0) return false; /* Initialize USB storage */ os_ioctlv(fd, USB_IOCTL_UMS_INIT, 0, 0, NULL); /* Get device capacity */ ret = __usbstorage_GetCapacity(NULL); if (ret <= 0) goto err; return true; err: /* Close USB device */ usbstorage_Shutdown(); return false; }
/* * device_dax_size -- (internal) checks the size of a given dax device */ static ssize_t device_dax_size(const char *path) { LOG(3, "path \"%s\"", path); os_stat_t st; int olderrno; if (os_stat(path, &st) < 0) { ERR("!stat \"%s\"", path); return -1; } char spath[PATH_MAX]; snprintf(spath, PATH_MAX, "/sys/dev/char/%u:%u/size", os_major(st.st_rdev), os_minor(st.st_rdev)); LOG(4, "device size path \"%s\"", spath); int fd = os_open(spath, O_RDONLY); if (fd < 0) { ERR("!open \"%s\"", spath); return -1; } ssize_t size = -1; char sizebuf[MAX_SIZE_LENGTH + 1]; ssize_t nread; if ((nread = read(fd, sizebuf, MAX_SIZE_LENGTH)) < 0) { ERR("!read"); goto out; } sizebuf[nread] = 0; /* null termination */ char *endptr; olderrno = errno; errno = 0; size = strtoll(sizebuf, &endptr, 0); if (endptr == sizebuf || *endptr != '\n' || ((size == LLONG_MAX || size == LLONG_MIN) && errno == ERANGE)) { ERR("invalid device size %s", sizebuf); size = -1; goto out; } errno = olderrno; out: olderrno = errno; (void) os_close(fd); errno = olderrno; LOG(4, "device size %zu", size); return size; }
/* * util_file_open -- open a memory pool file */ int util_file_open(const char *path, size_t *size, size_t minsize, int flags) { LOG(3, "path \"%s\" size %p minsize %zu flags %d", path, size, minsize, flags); int oerrno; int fd; #ifdef _WIN32 flags |= O_BINARY; #endif if ((fd = os_open(path, flags)) < 0) { ERR("!open \"%s\"", path); return -1; } if (os_flock(fd, OS_LOCK_EX | OS_LOCK_NB) < 0) { ERR("!flock \"%s\"", path); (void) os_close(fd); return -1; } if (size || minsize) { if (size) ASSERTeq(*size, 0); ssize_t actual_size = util_file_get_size(path); if (actual_size < 0) { ERR("stat \"%s\": negative size", path); errno = EINVAL; goto err; } if ((size_t)actual_size < minsize) { ERR("size %zu smaller than %zu", (size_t)actual_size, minsize); errno = EINVAL; goto err; } if (size) { *size = (size_t)actual_size; LOG(4, "actual file size %zu", *size); } } return fd; err: oerrno = errno; if (os_flock(fd, OS_LOCK_UN)) ERR("!flock unlock"); (void) os_close(fd); errno = oerrno; return -1; }
s32 DI_Config_Load(struct dipConfigState *cfg) { s32 fd, ret; #ifdef DEBUG svc_write("DIP: Config_Load(): Loading config file "); svc_write(__dip_cfg_filename); svc_write("\n"); #endif /* Open config file */ fd = os_open(__dip_cfg_filename, ISFS_OPEN_READ); #ifdef DEBUG svc_write("DIP: Config_Load(): Config file "); svc_write(fd < 0 ? "NOT found\n" : "found\n"); #endif if (fd < 0) return fd; /* Read config */ ret = os_read(fd, cfg, sizeof(struct dipConfigState)); if (ret == sizeof(struct dipConfigState)) { if (cfg->mode == MODE_FRAG) { s32 ret2; /* Read frag list */ memset(&fraglist_data, 0, sizeof(fraglist_data)); if (cfg->frag_size > sizeof(fraglist_data)) { ret2 = -1; } else { ret2 = os_read(fd, &fraglist_data, cfg->frag_size); } if (ret2 != cfg->frag_size) ret = -1; } } else if (ret >= 0) ret = -1; /* Close config */ os_close(fd); /* Delete config file */ __Config_Delete(__dip_cfg_filename); #ifdef DEBUG if (ret < 0) svc_write("DIP: Config_Load(): Config file has unexpected size!!!\n"); #endif return ret; }
static bool get_elf_platform_path(const char *exe_path, dr_platform_t *platform) { file_t fd = os_open(exe_path, OS_OPEN_READ); bool res = false; if (fd != INVALID_FILE) { res = get_elf_platform(fd, platform); os_close(fd); } return res; }
static int sandbox_flash_probe(struct udevice *dev) { struct sandbox_flash_plat *plat = dev_get_platdata(dev); struct sandbox_flash_priv *priv = dev_get_priv(dev); priv->fd = os_open(plat->pathname, OS_O_RDONLY); if (priv->fd != -1) return os_get_filesize(plat->pathname, &priv->file_size); return 0; }
static int sandbox_sf_setup(void **priv, const char *spec) { /* spec = idcode:file */ struct sandbox_spi_flash *sbsf; const char *file; size_t i, len, idname_len; const struct sandbox_spi_flash_data *data; file = strchr(spec, ':'); if (!file) { printf("sandbox_sf: unable to parse file\n"); goto error; } idname_len = file - spec; ++file; for (i = 0; i < ARRAY_SIZE(sandbox_sf_flashes); ++i) { data = &sandbox_sf_flashes[i]; len = strlen(data->name); if (idname_len != len) continue; if (!memcmp(spec, data->name, len)) break; } if (i == ARRAY_SIZE(sandbox_sf_flashes)) { printf("sandbox_sf: unknown flash '%*s'\n", (int)idname_len, spec); goto error; } if (sandbox_sf_0xff[0] == 0x00) memset(sandbox_sf_0xff, 0xff, sizeof(sandbox_sf_0xff)); sbsf = calloc(sizeof(*sbsf), 1); if (!sbsf) { printf("sandbox_sf: out of memory\n"); goto error; } sbsf->fd = os_open(file, 02); if (sbsf->fd == -1) { free(sbsf); printf("sandbox_sf: unable to open file '%s'\n", file); goto error; } sbsf->data = data; *priv = sbsf; return 0; error: return 1; }
/* * util_file_zero -- zeroes the specified region of the file */ int util_file_zero(const char *path, os_off_t off, size_t len) { LOG(3, "path \"%s\" off %ju len %zu", path, off, len); int fd; int olderrno; int ret = 0; if ((fd = os_open(path, O_RDWR)) < 0) { ERR("!open \"%s\"", path); return -1; } ssize_t size = util_file_get_size(path); if (size < 0) { LOG(2, "cannot determine file length \"%s\"", path); ret = -1; goto out; } if (off > size) { LOG(2, "offset beyond file length, %ju > %ju", off, size); ret = -1; goto out; } if ((size_t)off + len > (size_t)size) { LOG(2, "requested size of write goes beyond the file length, " "%zu > %zu", (size_t)off + len, size); LOG(4, "adjusting len to %zu", size - off); len = (size_t)(size - off); } void *addr = util_map(fd, (size_t)size, MAP_SHARED, 0, 0, NULL); if (addr == NULL) { LOG(2, "failed to map entire file \"%s\"", path); ret = -1; goto out; } /* zero initialize the specified region */ memset((char *)addr + off, 0, len); util_unmap(addr, (size_t)size); out: olderrno = errno; (void) os_close(fd); errno = olderrno; return ret; }
static void pf_read_package_file (PFunArgs) { char *filename = mhtml_evaluate_string (get_positional_arg (vars, 0)); int packages_read = 0; if (!empty_string_p (filename)) { int fd; /* If the filename specification doesn't start with a slash, then add the current directory and relative path. */ if (*filename != '/') { BPRINTF_BUFFER *temp = bprintf_create_buffer (); char *pre = pagefunc_get_variable ("mhtml::include-prefix"); char *rel = pagefunc_get_variable ("mhtml::relative-prefix"); if (pre) { if (!rel) rel = ""; bprintf (temp, "%s%s/%s", pre, rel, filename); free (filename); filename = temp->buffer; free (temp); } } fd = os_open (filename, O_RDONLY, 0666); if (fd > -1) { Package *pack; while ((pack = symbol_load_package (fd)) != (Package *)NULL) packages_read++; close (fd); } } if (filename) free (filename); if (packages_read) { bprintf_insert (page, start, "%d", packages_read); if (mhtml_user_keywords == (Package *)NULL) mhtml_user_keywords = symbol_get_package_hash ("*user-functions*", 577); } }
bool module_file_has_module_header(const char *filename) { bool res = false; struct mach_header hdr; file_t fd = os_open(filename, OS_OPEN_READ); if (fd == INVALID_FILE) return false; if (os_read(fd, &hdr, sizeof(hdr)) == sizeof(hdr) && is_macho_header((app_pc)&hdr, sizeof(hdr))) res = true; os_close(fd); return res; }
int app_open(const char *path, int oflag, ...) { int flags = IS_FILE; if (oflag & O_ACCMODE) flags |= NEED_WRITE; char realpath[MAX_PATH]; const char *fpath = handle_special_dirs(path, flags, realpath, sizeof (realpath)); if (!fpath) FILE_ERROR_RETURN(ENAMETOOLONG, -1); return os_open(fpath, oflag __OPEN_MODE_ARG); }
/* * check_cpu_cache -- (internal) check if file contains "cpu_cache" entry */ static int check_cpu_cache(const char *domain_path) { LOG(3, "domain_path: %s", domain_path); char domain_value[DOMAIN_VALUE_LEN]; int domain_fd; int cpu_cache = 0; if ((domain_fd = os_open(domain_path, O_RDONLY)) < 0) { LOG(1, "!open(\"%s\", O_RDONLY)", domain_path); goto end; } ssize_t len = read(domain_fd, domain_value, DOMAIN_VALUE_LEN); if (len < 0) { ERR("!read(%d, %p, %d)", domain_fd, domain_value, DOMAIN_VALUE_LEN); cpu_cache = -1; goto end; } else if (len == 0) { errno = ENODATA; ERR("read(%d, %p, %d) empty string", domain_fd, domain_value, DOMAIN_VALUE_LEN); cpu_cache = -1; goto end; } else if (domain_value[len - 1] != '\n') { ERR("!read(%d, %p, %d) invalid format", domain_fd, domain_value, DOMAIN_VALUE_LEN); cpu_cache = -1; goto end; } LOG(15, "detected persistent_domain: %s", domain_value); if (strncmp(domain_value, "cpu_cache", strlen("cpu_cache")) == 0) { LOG(15, "cpu_cache in persistent_domain: %s", domain_path); cpu_cache = 1; } else { LOG(15, "cpu_cache not in persistent_domain: %s", domain_path); cpu_cache = 0; } end: if (domain_fd >= 0) os_close(domain_fd); return cpu_cache; }
static void pf_write_package_file (PFunArgs) { char *filename = mhtml_evaluate_string (get_positional_arg (vars, 0)); char *flagged = mhtml_evaluate_string (get_value (vars, "flagged")); char *no_source = mhtml_evaluate_string (get_value (vars, "no-source")); int packages_written = 0; if (!empty_string_p (filename)) { int fd = os_open (filename, O_WRONLY | O_TRUNC | O_CREAT, 0666); if (fd > -1) { register int i = 1; char *arg; while ((arg = get_positional_arg (vars, i)) != (char *)NULL) { char *packname = mhtml_evaluate_string (arg); if (!empty_string_p (packname)) { Package *pack = symbol_lookup_package (packname); if (pack != (Package *)NULL) { symbol_dump_package (fd, pack, !empty_string_p (flagged), !empty_string_p (no_source)); packages_written++; } } if (packname) free (packname); i++; } close (fd); } } xfree (filename); xfree (flagged); xfree (no_source); if (packages_written) bprintf_insert (page, start, "%d", packages_written); }
/* * ut_open -- an open that cannot return < 0 */ int ut_open(const char *file, int line, const char *func, const char *path, int flags, ...) { va_list ap; int mode; va_start(ap, flags); mode = va_arg(ap, int); int retval = os_open(path, flags, mode); va_end(ap); if (retval < 0) ut_fatal(file, line, func, "!open: %s", path); return retval; }
s32 DI_Config_Save(void) { s32 fd, ret; /* Open /dev/di */ fd = os_open("/dev/di", 0); if (fd < 0) return fd; /* Save config */ inbuf[0] = IOCTL_DI_SAVE_CONFIG << 24; ret = os_ioctl(fd, IOCTL_DI_SAVE_CONFIG, inbuf, sizeof(inbuf), outbuf, sizeof(outbuf)); /* Close /dev/di */ os_close(fd); return ret; }
/* * Make a test fdt * * @param fdt Device tree pointer * @param size Size of device tree blob * @param aliases Specifies alias assignments. Format is a list of items * separated by space. Items are #a where * # is the alias number * a is the node to point to * @param nodes Specifies nodes to generate (a=0, b=1), upper case * means to create a disabled node */ static int make_fdt(void *fdt, int size, const char *aliases, const char *nodes) { char name[20], value[20]; const char *s; int fd; CHECK(fdt_create(fdt, size)); CHECK(fdt_finish_reservemap(fdt)); CHECK(fdt_begin_node(fdt, "")); CHECK(fdt_begin_node(fdt, "aliases")); for (s = aliases; *s;) { sprintf(name, "i2c%c", *s); sprintf(value, "/i2c%d@0", s[1] - 'a'); CHECK(fdt_property_string(fdt, name, value)); s += 2 + (s[2] != '\0'); } CHECK(fdt_end_node(fdt)); for (s = nodes; *s; s++) { sprintf(value, "i2c%d@0", (*s & 0xdf) - 'A'); CHECK(fdt_begin_node(fdt, value)); CHECK(fdt_property_string(fdt, "compatible", fdtdec_get_compatible(COMPAT_UNKNOWN))); if (*s <= 'Z') CHECK(fdt_property_string(fdt, "status", "disabled")); CHECK(fdt_end_node(fdt)); } CHECK(fdt_end_node(fdt)); CHECK(fdt_finish(fdt)); CHECK(fdt_pack(fdt)); #if defined(DEBUG) && defined(CONFIG_SANDBOX) fd = os_open("/tmp/fdtdec-text.dtb", OS_O_CREAT | OS_O_WRONLY); if (fd == -1) { printf("Could not open .dtb file to write\n"); return -1; } os_write(fd, fdt, size); os_close(fd); #endif return 0; }
int os_write_file(const char *name, const void *buf, int size) { char fname[256]; int fd; fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT | OS_O_TRUNC); if (fd < 0) { printf("Cannot open file '%s'\n", fname); return -EIO; } if (os_write(fd, buf, size) != size) { printf("Cannot write to file '%s'\n", fname); return -EIO; } os_close(fd); printf("Write '%s', size %#x (%d)\n", name, size, size); return 0; }
FILE * lfopen(const char *name, enum lfopen_mode om, os_open_permissions_type open_perms) { int fd; FILE *file; fd = os_open(name, (om == lfopen_truncate) ? os_truncate_open_flags : os_append_open_flags, open_perms); if (fd < 0) { return NULL; } file = os_fdopen(fd, (om == lfopen_truncate) ? "w+" : "a+"); if (!file) { close(fd); } /* file inherits fd */ return file; }
bool memquery_iterator_start(memquery_iter_t *iter, app_pc start, bool may_alloc) { char maps_name[24]; /* should only need 16 for 5-digit tid */ maps_iter_t *mi = (maps_iter_t *) &iter->internal; /* Don't assign the local ptrs until the lock is grabbed to make * their relationship clear. */ if (may_alloc) { mutex_lock(&maps_iter_buf_lock); mi->buf = (char *) &buf_iter; mi->comment_buffer = (char *) &comment_buf_iter; } else { mutex_lock(&memory_info_buf_lock); mi->buf = (char *) &buf_scratch; mi->comment_buffer = (char *) &comment_buf_scratch; } /* We need the maps for our thread id, not process id. * "/proc/self/maps" uses pid which fails if primary thread in group * has exited. */ snprintf(maps_name, BUFFER_SIZE_ELEMENTS(maps_name), "/proc/%d/maps", get_thread_id()); mi->maps = os_open(maps_name, OS_OPEN_READ); ASSERT(mi->maps != INVALID_FILE); mi->buf[BUFSIZE-1] = '\0'; /* permanently */ mi->newline = NULL; mi->bufread = 0; iter->comment = mi->comment_buffer; iter->may_alloc = may_alloc; /* XXX: it's quite difficult to start at the region containing "start": * we either need to support walking backward a line (complicated by * the incremental os_read() scheme) or make two passes. * Thus, the interface doesn't promise we'll start there. */ iter->vm_start = NULL; return true; }
/* * os_badblocks_clear_file -- clear the given bad blocks in the regular file * (not in a dax device) */ static int os_badblocks_clear_file(const char *file, struct badblocks *bbs) { LOG(3, "file %s badblocks %p", file, bbs); ASSERTne(bbs, NULL); int ret = 0; int fd; if ((fd = os_open(file, O_RDWR)) < 0) { ERR("!open: %s", file); return -1; } for (unsigned b = 0; b < bbs->bb_cnt; b++) { off_t offset = (off_t)bbs->bbv[b].offset; off_t length = (off_t)bbs->bbv[b].length; LOG(10, "clearing bad block: logical offset %li length %li (in 512B sectors) -- '%s'", B2SEC(offset), B2SEC(length), file); /* deallocate bad blocks */ if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, offset, length)) { ERR("!fallocate"); ret = -1; break; } /* allocate new blocks */ if (fallocate(fd, FALLOC_FL_KEEP_SIZE, offset, length)) { ERR("!fallocate"); ret = -1; break; } } os_close(fd); return ret; }
static int server_readdir( u8 *p ) { const char* name; u32 fsize = 0, d; int fd; char separator[ 2 ] = { PLATFORM_PATH_SEPARATOR, 0 }; log_msg( "server_readdir: request handler starting\n" ); if( remotefs_readdir_read_request( p, &d ) == ELUARPC_ERR ) { log_msg( "server_readdir: unable to read request\n" ); return SERVER_ERR; } log_msg( "server_readdir: DIR = %08X\n", d ); os_readdir( d, &name ); if( name ) { // Need to compute size now // Get real filename server_fullname[ 0 ] = server_fullname[ PLATFORM_MAX_FNAME_LEN ] = 0; strncpy( server_fullname, server_basedir, PLATFORM_MAX_FNAME_LEN ); if( name && strlen( name ) > 0 ) { if( server_fullname[ strlen( server_fullname ) - 1 ] != PLATFORM_PATH_SEPARATOR ) strncat( server_fullname, separator, PLATFORM_MAX_FNAME_LEN ); strncat( server_fullname, name, PLATFORM_MAX_FNAME_LEN ); } fd = os_open( server_fullname, RFS_OPEN_FLAG_RDONLY, 0 ); if( fd ) { fsize = os_lseek( fd, 0, RFS_LSEEK_END ); os_close( fd ); } else { log_msg( "server_readdir: unable to open file %s\n", server_fullname ); name = NULL; } } log_msg( "server_readdir: OS response is fname = %s, fsize = %u\n", name, ( unsigned )fsize ); remotefs_readdir_write_response( p, name, fsize, 0 ); return SERVER_OK; }
long sandbox_fs_write_at(const char *filename, unsigned long pos, void *buffer, unsigned long towrite) { ssize_t size; int fd, ret; fd = os_open(filename, OS_O_RDWR | OS_O_CREAT); if (fd < 0) return fd; ret = os_lseek(fd, pos, OS_SEEK_SET); if (ret == -1) { os_close(fd); return ret; } size = os_write(fd, buffer, towrite); os_close(fd); return size; }
/* * NAME: vol->open() * DESCRIPTION: open volume source and lock against concurrent updates */ int v_open(hfsvol *vol, const char *path, int mode) { if (vol->flags & HFS_VOL_OPEN) ERROR(EINVAL, "volume already open"); if (os_open(&vol->priv, path, mode) == -1) goto fail; vol->flags |= HFS_VOL_OPEN; /* initialize volume block cache (OK to fail) */ if (vol->flags & HFS_VOL_USECACHE) b_init(vol); return 0; fail: return -1; }