Przeglądaj źródła

create: if "-" is given as path, read binary from stdin

Thomas Waldmann 10 lat temu
rodzic
commit
a3f335e0ff
2 zmienionych plików z 25 dodań i 0 usunięć
  1. 17 0
      attic/archive.py
  2. 8 0
      attic/archiver.py

+ 17 - 0
attic/archive.py

@@ -381,6 +381,23 @@ class Archive:
         item.update(self.stat_attrs(st, path))
         self.add_item(item)
 
+    def process_stdin(self, path, cache):
+        uid, gid = 0, 0
+        fd = sys.stdin.buffer  # binary
+        chunks = []
+        for chunk in self.chunker.chunkify(fd):
+            chunks.append(cache.add_chunk(self.key.id_hash(chunk), chunk, self.stats))
+        self.stats.nfiles += 1
+        item = {
+            b'path': path,
+            b'chunks': chunks,
+            b'mode': 0o100660,  # regular file, ug=rw
+            b'uid': uid, b'user': uid2user(uid),
+            b'gid': gid, b'group': gid2group(gid),
+            b'mtime': int_to_bigint(int(time.time()) * 1000000000)
+        }
+        self.add_item(item)
+
     def process_file(self, path, st, cache):
         safe_path = make_path_safe(path)
         # Is it a hard link?

+ 8 - 0
attic/archiver.py

@@ -116,6 +116,14 @@ Type "Yes I am sure" if you understand this and want to continue.\n""")
             except IOError:
                 pass
         for path in args.paths:
+            if path == '-':  # stdin
+                path = 'stdin'
+                self.print_verbose(path)
+                try:
+                    archive.process_stdin(path, cache)
+                except IOError as e:
+                    self.print_error('%s: %s', path, e)
+                continue
             path = os.path.normpath(path)
             if args.dontcross:
                 try: