summaryrefslogtreecommitdiff
path: root/cmds-send.c
diff options
context:
space:
mode:
authorDavid Sterba <dsterba@suse.com>2016-11-07 16:51:20 +0100
committerDavid Sterba <dsterba@suse.com>2016-11-11 16:23:29 +0100
commit84d1f29d47839ab60e8362f0e05f176d0c6d0755 (patch)
treee3a45544de22c0c62bf44d68eb88f4108d8fc1c4 /cmds-send.c
parent7d405df0da1c6b881fdd251755bc2f71a03f2b62 (diff)
btrfs-progs: send: check for output file existence before creating
In some cases the root might not be able to create the output file (and streaming to stdout is not an option). Make the output file creation two step and let it work if the file is already created. Signed-off-by: David Sterba <dsterba@suse.com>
Diffstat (limited to 'cmds-send.c')
-rw-r--r--cmds-send.c15
1 files changed, 14 insertions, 1 deletions
diff --git a/cmds-send.c b/cmds-send.c
index fbb2f804..7aa4b36d 100644
--- a/cmds-send.c
+++ b/cmds-send.c
@@ -571,7 +571,20 @@ int cmd_send(int argc, char **argv)
usage(cmd_send_usage);
if (outname[0]) {
- send.dump_fd = creat(outname, 0600);
+ int tmpfd;
+
+ /*
+ * Try to use an existing file first. Even if send runs as
+ * root, it might not have permissions to create file (eg. on a
+ * NFS) but it should still be able to use a pre-created file.
+ */
+ tmpfd = open(outname, O_WRONLY | O_TRUNC);
+ if (tmpfd < 0) {
+ if (errno == ENOENT)
+ tmpfd = open(outname,
+ O_CREAT | O_WRONLY | O_TRUNC, 0600);
+ }
+ send.dump_fd = tmpfd;
if (send.dump_fd == -1) {
ret = -errno;
error("cannot create '%s': %s", outname, strerror(-ret));