
From: Simon Glass <sjg@chromium.org> It is convenient to be able to report the system error when available. Update os_open() to return it. Signed-off-by: Simon Glass <sjg@chromium.org> --- arch/sandbox/cpu/os.c | 14 +++++++++----- drivers/block/host_dev.c | 4 ++-- drivers/mtd/spi/sandbox.c | 2 +- drivers/scsi/sandbox_scsi.c | 2 +- drivers/usb/emul/sandbox_flash.c | 2 +- include/os.h | 2 +- 6 files changed, 15 insertions(+), 11 deletions(-) diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c index f5c9a8aecf2..d1fda05444d 100644 --- a/arch/sandbox/cpu/os.c +++ b/arch/sandbox/cpu/os.c @@ -100,7 +100,7 @@ off_t os_lseek(int fd, off_t offset, int whence) int os_open(const char *pathname, int os_flags) { - int flags; + int flags, ret; switch (os_flags & OS_O_MASK) { case OS_O_RDONLY: @@ -127,7 +127,11 @@ int os_open(const char *pathname, int os_flags) */ flags |= O_CLOEXEC; - return open(pathname, flags, 0644); + ret = open(pathname, flags, 0644); + if (ret == -1) + return -errno; + + return ret; } int os_close(int fd) @@ -172,7 +176,7 @@ int os_write_file(const char *fname, const void *buf, int size) 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; + return fd; } if (os_write(fd, buf, size) != size) { printf("Cannot write to file '%s'\n", fname); @@ -206,7 +210,7 @@ int os_read_file(const char *fname, void **bufp, int *sizep) fd = os_open(fname, OS_O_RDONLY); if (fd < 0) { printf("Cannot open file '%s'\n", fname); - return -EIO; + return fd; } size = os_filesize(fd); if (size < 0) { @@ -242,7 +246,7 @@ int os_map_file(const char *pathname, int os_flags, void **bufp, int *sizep) ifd = os_open(pathname, os_flags); if (ifd < 0) { printf("Cannot open file '%s'\n", pathname); - return -EIO; + return ifd; } size = os_filesize(ifd); if (size < 0) { diff --git a/drivers/block/host_dev.c b/drivers/block/host_dev.c index b3ff3cd1fab..504a196e631 100644 --- a/drivers/block/host_dev.c +++ b/drivers/block/host_dev.c @@ -39,11 +39,11 @@ static int host_sb_attach_file(struct udevice *dev, const char *filename) return ret; fd = os_open(filename, OS_O_RDWR); - if (fd == -1) { + if (fd < 0) { printf("Failed to access host backing file '%s', trying read-only\n", filename); fd = os_open(filename, OS_O_RDONLY); - if (fd == -1) { + if (fd < 0) { printf("- still failed\n"); return log_msg_ret("open", -ENOENT); } diff --git a/drivers/mtd/spi/sandbox.c b/drivers/mtd/spi/sandbox.c index e5ebc3479fb..d9afe77e52d 100644 --- a/drivers/mtd/spi/sandbox.c +++ b/drivers/mtd/spi/sandbox.c @@ -171,7 +171,7 @@ static int sandbox_sf_probe(struct udevice *dev) memset(sandbox_sf_0xff, 0xff, sizeof(sandbox_sf_0xff)); sbsf->fd = os_open(pdata->filename, 02); - if (sbsf->fd == -1) { + if (sbsf->fd < 0) { printf("%s: unable to open file '%s'\n", __func__, pdata->filename); ret = -EIO; diff --git a/drivers/scsi/sandbox_scsi.c b/drivers/scsi/sandbox_scsi.c index 544a0247083..3c451313109 100644 --- a/drivers/scsi/sandbox_scsi.c +++ b/drivers/scsi/sandbox_scsi.c @@ -105,7 +105,7 @@ static int sandbox_scsi_probe(struct udevice *dev) if (priv->pathname) { priv->fd = os_open(priv->pathname, OS_O_RDONLY); - if (priv->fd != -1) { + if (priv->fd >= 0) { ret = os_get_filesize(priv->pathname, &info->file_size); if (ret) return log_msg_ret("sz", ret); diff --git a/drivers/usb/emul/sandbox_flash.c b/drivers/usb/emul/sandbox_flash.c index b5176bb30ce..25d968e91c7 100644 --- a/drivers/usb/emul/sandbox_flash.c +++ b/drivers/usb/emul/sandbox_flash.c @@ -342,7 +342,7 @@ static int sandbox_flash_probe(struct udevice *dev) int ret; priv->fd = os_open(plat->pathname, OS_O_RDWR); - if (priv->fd != -1) { + if (priv->fd >= 0) { ret = os_get_filesize(plat->pathname, &info->file_size); if (ret) return log_msg_ret("sz", ret); diff --git a/include/os.h b/include/os.h index ae3ca6d42a2..f4f2667d206 100644 --- a/include/os.h +++ b/include/os.h @@ -71,7 +71,7 @@ off_t os_filesize(int fd); * * @pathname: Pathname of file to open * @flags: Flags, like OS_O_RDONLY, OS_O_RDWR - * Return: file descriptor, or -1 on error + * Return: file descriptor, or -errno on error */ int os_open(const char *pathname, int flags); -- 2.43.0