diff options
author | Bob Ippolito <bob@redivi.com> | 2021-11-28 13:06:44 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-28 13:06:44 -0800 |
commit | a642aa35e0f3d8c150808dc746716706e26bc836 (patch) | |
tree | 38c72c4ef20f15ac55ec112bb301d2fb7746a14b | |
parent | e36a3c87580477e8dbc407bd5ef7c1ed1e987cf0 (diff) | |
parent | 96294f571af66bfe820cc1e64e776a14217af273 (diff) | |
download | xattr-a642aa35e0f3d8c150808dc746716706e26bc836.tar.gz |
Merge pull request #98 from xattr/add-clear-option
tool: add -c(lear) option
-rwxr-xr-x | xattr/tool.py | 40 |
1 files changed, 29 insertions, 11 deletions
diff --git a/xattr/tool.py b/xattr/tool.py index 0623560..d9548e1 100755 --- a/xattr/tool.py +++ b/xattr/tool.py @@ -49,11 +49,13 @@ def usage(e=None): print(" %s -p [-slz] attr_name file [file ...]" % (name,)) print(" %s -w [-sz] attr_name attr_value file [file ...]" % (name,)) print(" %s -d [-s] attr_name file [file ...]" % (name,)) + print(" %s -c [-s] file [file ...]" % (name,)) print("") print("The first form lists the names of all xattrs on the given file(s).") print("The second form (-p) prints the value of the xattr attr_name.") print("The third form (-w) sets the value of the xattr attr_name to attr_value.") print("The fourth form (-d) deletes the xattr attr_name.") + print("The fifth form (-c) deletes (clears) all xattrs.") print("") print("options:") print(" -h: print this help") @@ -89,7 +91,7 @@ def _dump(src, length=16): def main(argv): try: - (optargs, args) = getopt.getopt(argv[1:], "hlpwdzs", ["help"]) + (optargs, args) = getopt.getopt(argv[1:], "hlpwdzsc", ["help"]) except getopt.GetoptError as e: return usage(e) @@ -98,10 +100,11 @@ def main(argv): read = False write = False delete = False + clear = False nofollow = False compress = lambda x: x decompress = compress - status = 0 + errors = [] for opt, arg in optargs: if opt in ("-h", "--help"): @@ -112,23 +115,27 @@ def main(argv): nofollow = True elif opt == "-p": read = True - if write or delete: - return usage("-p not allowed with -w or -d") + if write or delete or clear: + return usage("-p not allowed with -w, -d or -c") elif opt == "-w": write = True - if read or delete: - return usage("-w not allowed with -p or -d") + if read or delete or clear: + return usage("-w not allowed with -p, -d or -c") elif opt == "-d": delete = True - if read or write: - return usage("-d not allowed with -p or -w") + if read or write or clear: + return usage("-d not allowed with -p, -w or -c") + elif opt == "-c": + clear = True + if read or write or delete: + return usage("-c not allowed with -p, -w or -d") elif opt == "-z": compress = zlib.compress decompress = zlib.decompress - if write or delete: + if write or delete or clear: if long_format: - return usage("-l not allowed with -w or -p") + return usage("-l not allowed with -w, -d or -c") if read or write or delete: if not args: @@ -140,6 +147,9 @@ def main(argv): return usage("No attr_value") attr_value = args.pop(0).encode('utf-8') + if len(args) == 0: + return usage("No file") + if len(args) > 1: multiple_files = True else: @@ -151,6 +161,7 @@ def main(argv): for filename in args: def onError(e): + errors.append(e) if not os.path.exists(filename): sys.stderr.write("No such file: %s\n" % (filename,)) else: @@ -179,6 +190,13 @@ def main(argv): onError("No such xattr: %s" % (attr_name,)) continue + elif clear: + try: + attrs.clear() + except (IOError, OSError) as e: + onError(e) + continue + else: try: if read: @@ -230,7 +248,7 @@ def main(argv): else: print("".join((file_prefix, attr_name))) - return status + return 1 if errors else 0 if __name__ == "__main__": sys.exit(main(sys.argv)) |