Răsfoiți Sursa

Merge pull request #5466 from schors/1.1-stdinmode

create: backport implementation --stdin-mode, --stdin-user and --stdin-group, #5462
TW 4 ani în urmă
părinte
comite
01d73e4d75
3 a modificat fișierele cu 25 adăugiri și 6 ștergeri
  1. 10 5
      src/borg/archive.py
  2. 11 1
      src/borg/archiver.py
  3. 4 0
      src/borg/constants.py

+ 10 - 5
src/borg/archive.py

@@ -1025,14 +1025,19 @@ Utilization of max. archive size: {csize_max:.0%}
                 for chunk in item.chunks:
                     cache.chunk_incref(chunk.id, dummy_stats, size=chunk.size)
 
-    def process_stdin(self, path, cache):
-        uid, gid = 0, 0
+    def process_stdin(self, path, cache, mode, user, group):
+        uid = user2uid(user)
+        if uid is None:
+            raise Error("no such user: %s" % user)
+        gid = group2gid(group)
+        if gid is None:
+            raise Error("no such group: %s" % group)
         t = int(time.time()) * 1000000000
         item = Item(
             path=path,
-            mode=0o100660,  # regular file, ug=rw
-            uid=uid, user=uid2user(uid),
-            gid=gid, group=gid2group(gid),
+            mode=mode & 0o107777 | 0o100000,  # forcing regular file mode
+            uid=uid, user=user,
+            gid=gid, group=group,
             mtime=t, atime=t, ctime=t,
         )
         fd = sys.stdin.buffer  # binary

+ 11 - 1
src/borg/archiver.py

@@ -71,6 +71,7 @@ try:
     from .helpers import dash_open
     from .helpers import umount
     from .helpers import msgpack, msgpack_fallback
+    from .helpers import uid2user, gid2group
     from .nanorst import rst_to_terminal
     from .patterns import ArgparsePatternAction, ArgparseExcludeFileAction, ArgparsePatternFileAction, parse_exclude_pattern
     from .patterns import PatternMatcher
@@ -524,9 +525,12 @@ class Archiver:
             for path in args.paths:
                 if path == '-':  # stdin
                     path = args.stdin_name
+                    mode = args.stdin_mode
+                    user = args.stdin_user
+                    group = args.stdin_group
                     if not dry_run:
                         try:
-                            status = archive.process_stdin(path, cache)
+                            status = archive.process_stdin(path, cache, mode, user, group)
                         except BackupOSError as e:
                             status = 'E'
                             self.print_warning('%s: %s', path, e)
@@ -3408,6 +3412,12 @@ class Archiver:
                                help='do not load/update the file metadata cache used to detect unchanged files')
         subparser.add_argument('--stdin-name', metavar='NAME', dest='stdin_name', default='stdin',
                                help='use NAME in archive for stdin data (default: "stdin")')
+        subparser.add_argument('--stdin-user', metavar='USER', dest='stdin_user', default=uid2user(0),
+                                help='set user USER in archive for stdin data (default: %(default)r)')
+        subparser.add_argument('--stdin-group', metavar='GROUP', dest='stdin_group', default=gid2group(0),
+                                help='set group GROUP in archive for stdin data (default: %(default)r)')
+        subparser.add_argument('--stdin-mode', metavar='M', dest='stdin_mode', type=lambda s: int(s, 8), default=STDIN_MODE_DEFAULT,
+                                help='set mode to M in archive for stdin data (default: %(default)04o)')
 
         exclude_group = define_exclusion_group(subparser, tag_files=True)
         exclude_group.add_argument('--exclude-nodump', dest='exclude_nodump', action='store_true',

+ 4 - 0
src/borg/constants.py

@@ -20,6 +20,10 @@ REQUIRED_ARCHIVE_KEYS = frozenset(['version', 'name', 'items', 'cmdline', 'time'
 # default umask, overridden by --umask, defaults to read/write only for owner
 UMASK_DEFAULT = 0o077
 
+# default file mode to store stdin data, defaults to read/write for owner and group
+# forcing to 0o100XXX later
+STDIN_MODE_DEFAULT = 0o660
+
 CACHE_TAG_NAME = 'CACHEDIR.TAG'
 CACHE_TAG_CONTENTS = b'Signature: 8a477f597d28d172789f06886806bc55'