Skip to content

Commit 29b007e

Browse files
committed
nvme: add O_TRUNC when opening output files for read commands
The file might already exist, thus in the case make sure it's truncated to zero before writing to it. Signed-off-by: Daniel Wagner <wagi@kernel.org>
1 parent 24860ff commit 29b007e

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

nvme.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,7 +2256,7 @@ static int io_mgmt_recv(int argc, char **argv, struct command *acmd, struct plug
22562256
cfg.nsid);
22572257

22582258
if (cfg.file) {
2259-
dfd = open(cfg.file, O_WRONLY | O_CREAT, 0644);
2259+
dfd = open(cfg.file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
22602260
if (dfd < 0) {
22612261
nvme_show_perror(cfg.file);
22622262
return -errno;
@@ -8486,7 +8486,7 @@ static int submit_io(int opcode, char *command, const char *desc, int argc, char
84868486
flags = O_RDONLY;
84878487
} else {
84888488
dfd = mfd = STDOUT_FILENO;
8489-
flags = O_WRONLY | O_CREAT;
8489+
flags = O_WRONLY | O_CREAT | O_TRUNC;
84908490
}
84918491

84928492
if (strlen(cfg.data)) {
@@ -9364,7 +9364,7 @@ static int passthru(int argc, char **argv, bool admin,
93649364

93659365
if (cfg.opcode & 0x02) {
93669366
cfg.read = true;
9367-
flags = O_WRONLY | O_CREAT;
9367+
flags = O_WRONLY | O_CREAT | O_TRUNC;
93689368
dfd = mfd = STDOUT_FILENO;
93699369
}
93709370

@@ -10484,7 +10484,7 @@ static int libnvme_mi(int argc, char **argv, __u8 admin_opcode, const char *desc
1048410484
fd = STDIN_FILENO;
1048510485
send = true;
1048610486
} else {
10487-
flags = O_WRONLY | O_CREAT;
10487+
flags = O_WRONLY | O_CREAT | O_TRUNC;
1048810488
fd = STDOUT_FILENO;
1048910489
send = false;
1049010490
}

0 commit comments

Comments
 (0)