casaroli opened a new pull request, #21449:
URL: https://github.com/apache/nuttx/pull/21449

   ## Summary
   
   `open(O_RDWR | O_TRUNC)` and `fopen("w+")` do not truncate an existing file 
on tmpfs, FAT, SmartFS and NFS. POSIX: "If the file exists and is a regular 
file, and the file is successfully opened O_RDWR or O_WRONLY, its length shall 
be truncated to 0".
   
   Commit 9e141acab36 (#19238) changed `O_RDWR` to 2, so `O_RDWR` no longer 
contains the `O_WRONLY` bit. These four file systems still test `(oflags & 
(O_TRUNC | O_WRONLY)) == (O_TRUNC | O_WRONLY)`. This PR tests `(oflags & 
O_TRUNC) != 0 && (oflags & O_ACCMODE) != O_RDONLY`, as mnemofs does.
   
   The first commit is whitespace only: `tools/checkpatch.sh` checks the whole 
of a touched file, and `fs/nfs/nfs_vfsops.c` has older nxstyle errors.
   
   ## Impact
   
   - All build modes and architectures: the change is in common file system 
code.
   - This restores the behaviour from before #19238. Callers that use `O_RDWR | 
O_TRUNC` expect the truncation, for example `system/settings` and 
`wireless/wapi` in nuttx-apps: on master they keep the old end of the file when 
the new contents are shorter.
   - `O_TRUNC` without write access is still ignored. `O_WRONLY | O_TRUNC` does 
not change.
   - The other file systems do not use this test: littlefs, spiffs, mnemofs, 
xipfs, shm, v9fs, hostfs and rpmsgfs. NXFFS rejects `O_RDWR` with `ENOSYS`. A 
search of the whole tree for `O_WRONLY` and `O_RDWR` in a bit test finds this 
truncation test only in these four places.
   - No documentation change: Documentation/ does not describe this.
   - Not a breaking change: it is a fix of a regression, and the API does not 
change.
   
   ## Testing
   
   Test program `rdwrtrunc.c`, 11 checks on each file system: the size after 
`O_WRONLY | O_TRUNC`, `O_RDWR | O_TRUNC`, `O_RDWR | O_CREAT | O_TRUNC`, 
`O_RDWR`, `O_RDONLY | O_TRUNC` and `fopen("w+")`, then write, read and size 
after `O_RDWR | O_TRUNC`. tmpfs is a new mount. FAT is on a RAM disk (`mkrd`, 
`mkfatfs`). SmartFS is on a RAM MTD (`mksmartfs`).
   
   | configuration | file system | master | this PR |
   | --- | --- | --- | --- |
   | qemu-armv8a:knsh (kernel, arm64, QEMU with HVF) | tmpfs | 6 PASS, 5 FAIL | 
11 PASS |
   | qemu-armv8a:knsh | FAT | 6 PASS, 5 FAIL | 11 PASS |
   | qemu-armv8a:nsh (flat, arm64, HVF) | tmpfs | 6 PASS, 5 FAIL | 11 PASS |
   | qemu-armv8a:nsh | FAT | 6 PASS, 5 FAIL | 11 PASS |
   | sim:nsh (flat, macOS host) with `CONFIG_FS_SMARTFS` | SmartFS | 6 PASS, 5 
FAIL | 11 PASS |
   
   On master each failed check shows that the file kept its 10 bytes. `O_WRONLY 
| O_TRUNC`, `O_RDWR` and `O_RDONLY | O_TRUNC` pass on master and with this PR.
   
   NFS is built only (qemu-armv8a:netnsh, rv-virt:netnsh). The test did not run 
on NFS: the rig has no NFS server. The change is the same line as in the other 
file systems.
   
   ostest exits with status 0 on master and with this PR: qemu-armv8a:knsh, 
qemu-armv8a:nsh, and the flat qemu-armv8a:nsh build with the test. All have 
`CONFIG_FS_TMPFS` and `CONFIG_FS_FAT`.
   
   No test in CI checks this behaviour.
   
   Builds with `EXTRAFLAGS="-Wno-cpp -Werror"`, each OK: qemu-armv8a:knsh and 
qemu-armv8a:nsh (with `CONFIG_FS_TMPFS` and `CONFIG_FS_FAT`), 
qemu-armv8a:netnsh, rv-virt:netnsh, sim:nsh, and sim:nsh with 
`CONFIG_FS_SMARTFS` (macOS host). The only warnings come from the linkers: 
`-ld_classic` on macOS, RWX segment on rv-virt:netnsh.
   
   The tests ran on master 39a1f27900c; the branch is rebased on 3d57048f1e1 
(no change in the touched files).
   
   `tools/checkpatch.sh -c -u -m -g` passes.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to