From a085a0a70b48fdf49957bd00c86c80dbf2d0d2e5 Mon Sep 17 00:00:00 2001 From: Juha Niskanen Date: Tue, 5 Nov 2019 07:32:23 -0600 Subject: [PATCH] fs/smartfs/smartfs_smart.c: Honor O_APPEND on writes. Also document pwrite() bug/limitation. --- fs/smartfs/smartfs_smart.c | 14 ++++++++++++++ fs/smartfs/smartfs_utils.c | 4 ++-- fs/vfs/fs_pwrite.c | 6 ++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/fs/smartfs/smartfs_smart.c b/fs/smartfs/smartfs_smart.c index 540c724e57..3e7d38c1ca 100644 --- a/fs/smartfs/smartfs_smart.c +++ b/fs/smartfs/smartfs_smart.c @@ -673,6 +673,20 @@ static ssize_t smartfs_write(FAR struct file *filep, const char *buffer, goto errout_with_semaphore; } + /* Test if we opened for APPEND mode. If we did, then seek to the + * end of the file. + */ + + if (sf->oflags & O_APPEND) + { + ret = smartfs_seek_internal(fs, sf, 0, SEEK_END); + if (ret < 0) + { + ret = -EIO; + goto errout_with_semaphore; + } + } + /* First test if we are overwriting an existing location or writing to * a new one. */ diff --git a/fs/smartfs/smartfs_utils.c b/fs/smartfs/smartfs_utils.c index 003d49df70..c76f55fc8e 100644 --- a/fs/smartfs/smartfs_utils.c +++ b/fs/smartfs/smartfs_utils.c @@ -1901,7 +1901,7 @@ int smartfs_extendfile(FAR struct smartfs_mountpt_s *fs, /* Loop until either (1) the file has been fully extended with zeroed data * or (2) an error occurs. We assume we start with the current sector in - * cache (ff_currentsector) + * cache (ff_currentsector). */ oldsize = sf->entry.datlen; @@ -1909,7 +1909,7 @@ int smartfs_extendfile(FAR struct smartfs_mountpt_s *fs, DEBUGASSERT(length > oldsize); /* Seek to the end of the file for the append/write operation, remembering - * the current file position. It will be retored before returneding; the + * the current file position. It will be restored before returning; the * truncate operation must not alter the file position. */ diff --git a/fs/vfs/fs_pwrite.c b/fs/vfs/fs_pwrite.c index 8485ca43cd..5364985f90 100644 --- a/fs/vfs/fs_pwrite.c +++ b/fs/vfs/fs_pwrite.c @@ -131,6 +131,12 @@ ssize_t file_pwrite(FAR struct file *filep, FAR const void *buf, * end-of-file condition, or -1 on failure with errno set appropriately. * See write() return values * + * Assumptions/Limitations: + * POSIX requires that opening a file with the O_APPEND flag should have no + * effect on the location at which pwrite() writes data. However, on NuttX + * like on Linux, if a file is opened with O_APPEND, pwrite() appends data + * to the end of the file, regardless of the value of offset. + * ****************************************************************************/ ssize_t pwrite(int fd, FAR const void *buf, size_t nbytes, off_t offset)