本文整理汇总了C++中os_open函数的典型用法代码示例。如果您正苦于以下问题:C++ os_open函数的具体用法?C++ os_open怎么用?C++ os_open使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了os_open函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: os_find_u_boot
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;
}
示例2: util_file_map_whole
/*
* 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;
}
示例3: server_open
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;
}
示例4: sandbox_fs_write_at
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;
}
示例5: util_file_create
/*
* 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;
}
示例6: DI_Config_Save
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;
}
示例7: os_nodetype
/// 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;
}
示例8: usbstorage_Init
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;
}
示例9: device_dax_size
/*
* 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;
}
示例10: util_file_open
/*
* 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;
}
示例11: DI_Config_Load
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;
}
示例12: get_elf_platform_path
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;
}
示例13: sandbox_flash_probe
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;
}
示例14: sandbox_sf_setup
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;
}
示例15: util_file_zero
/*
* 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;
}