3 # Copyright (C) 2010 Rob Browning
5 # This code is covered under the terms of the GNU Library General
6 # Public License as described in the bup LICENSE file.
8 # TODO: Add tar-like -C option.
11 from bup import metadata
12 from bup import options
13 from bup.helpers import handle_ctrl_c, log, saved_errors
17 if not name or name == '-':
19 return open(name, 'r')
22 def open_output(name):
23 if not name or name == '-':
25 return open(name, 'w')
29 bup meta --create [OPTION ...] <PATH ...>
30 bup meta --list [OPTION ...]
31 bup meta --extract [OPTION ...]
32 bup meta --start-extract [OPTION ...]
33 bup meta --finish-extract [OPTION ...]
34 bup meta --edit [OPTION ...] <PATH ...>
36 c,create write metadata for PATHs to stdout (or --file)
37 t,list display metadata
38 x,extract perform --start-extract followed by --finish-extract
39 start-extract build tree matching metadata provided on standard input (or --file)
40 finish-extract finish applying standard input (or --file) metadata to filesystem
41 edit alter metadata; write to stdout (or --file)
42 f,file= specify source or destination file
43 R,recurse recurse into subdirectories
44 xdev,one-file-system don't cross filesystem boundaries
45 numeric-ids apply numeric IDs (user, group, etc.) rather than names
46 symlinks handle symbolic links (default is true)
47 paths include paths in metadata (default is true)
48 set-uid= set metadata uid (via --edit)
49 set-gid= set metadata gid (via --edit)
50 set-user= set metadata user (via --edit)
51 unset-user remove metadata user (via --edit)
52 set-group= set metadata group (via --edit)
53 unset-group remove metadata group (via --edit)
54 v,verbose increase log output (can be used more than once)
55 q,quiet don't show progress meter
60 o = options.Options(optspec)
61 (opt, flags, remainder) = o.parse(['--paths', '--symlinks'] + sys.argv[1:])
63 opt.verbose = opt.verbose or 0
64 opt.quiet = opt.quiet or 0
65 metadata.verbose = opt.verbose - opt.quiet
67 action_count = sum([bool(x) for x in [opt.create, opt.list, opt.extract,
68 opt.start_extract, opt.finish_extract,
71 o.fatal("bup: only one action permitted: --create --list --extract --edit")
73 o.fatal("bup: no action specified")
76 if len(remainder) < 1:
77 o.fatal("no paths specified for create")
78 output_file = open_output(opt.file)
79 metadata.save_tree(output_file,
82 write_paths=opt.paths,
83 save_symlinks=opt.symlinks,
86 if len(remainder) > 0:
87 o.fatal("cannot specify paths for --list")
88 src = open_input(opt.file)
89 metadata.display_archive(src)
90 elif opt.start_extract:
91 if len(remainder) > 0:
92 o.fatal("cannot specify paths for --start-extract")
93 src = open_input(opt.file)
94 metadata.start_extract(src, create_symlinks=opt.symlinks)
95 elif opt.finish_extract:
96 if len(remainder) > 0:
97 o.fatal("cannot specify paths for --finish-extract")
98 src = open_input(opt.file)
99 metadata.finish_extract(src, restore_numeric_ids=opt.numeric_ids)
101 if len(remainder) > 0:
102 o.fatal("cannot specify paths for --extract")
103 src = open_input(opt.file)
104 metadata.extract(src,
105 restore_numeric_ids=opt.numeric_ids,
106 create_symlinks=opt.symlinks)
108 if len(remainder) < 1:
109 o.fatal("no paths specified for edit")
110 output_file = open_output(opt.file)
112 unset_user = False # True if --unset-user was the last relevant option.
113 unset_group = False # True if --unset-group was the last relevant option.
115 if flag[0] == '--set-user':
117 elif flag[0] == '--unset-user':
119 elif flag[0] == '--set-group':
121 elif flag[0] == '--unset-group':
124 for path in remainder:
127 for m in metadata._ArchiveIterator(f):
128 if opt.set_uid is not None:
130 m.uid = int(opt.set_uid)
132 o.fatal("uid must be an integer")
134 if opt.set_gid is not None:
136 m.gid = int(opt.set_gid)
138 o.fatal("gid must be an integer")
142 elif opt.set_user is not None:
143 m.user = opt.set_user
147 elif opt.set_group is not None:
148 m.group = opt.set_group
156 log('WARNING: %d errors encountered.\n' % len(saved_errors))