fs/smartfs/smartfs_smart.c: Fix O_CREAT without O_TRUNC truncating existing file. Also nothing in POSIX says that O_APPEND should prevent O_TRUNC.

This commit is contained in:
Juha Niskanen
2019-11-05 07:28:24 -06:00
committed by Gregory Nutt
parent 0154126c01
commit 3268eb882e
+8 -12
View File
@@ -260,23 +260,19 @@ static int smartfs_open(FAR struct file *filep, const char *relpath,
/* TODO: Test open mode based on the file mode */ /* TODO: Test open mode based on the file mode */
/* The file exists. Check if we are opening it for O_CREAT or /* If O_TRUNC is specified and the file is opened for writing,
* O_TRUNC mode and delete the sector chain if we are. * then truncate the file. This operation requires that the file
* is writeable. O_TRUNC without write access is ignored.
*/ */
if ((oflags & (O_CREAT | O_TRUNC)) != 0) if ((oflags & (O_TRUNC | O_WROK)) == (O_TRUNC | O_WROK))
{ {
/* Don't truncate if open for APPEND */ /* Truncate the file as part of the open */
if (!(oflags & O_APPEND)) ret = smartfs_shrinkfile(fs, sf, 0);
if (ret < 0)
{ {
/* Truncate the file as part of the open */ goto errout_with_buffer;
ret = smartfs_shrinkfile(fs, sf, 0);
if (ret < 0)
{
goto errout_with_buffer;
}
} }
} }
} }