summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2020-12-15 17:43:07 +0100
committerAnton Khirnov <anton@khirnov.net>2020-12-15 17:43:07 +0100
commit71f9452cca49bcd9da0ddda3047c2e18e58b9083 (patch)
tree31f144873f2b6fbd0068dbc62226a3e557987aba
Initial commit.
-rwxr-xr-xperm_offset.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/perm_offset.py b/perm_offset.py
new file mode 100755
index 0000000..4e13eb3
--- /dev/null
+++ b/perm_offset.py
@@ -0,0 +1,50 @@
+#!/usr/bin/python3
+
+import argparse
+import logging
+import os
+import sys
+
+def remap_file(fpath, src, dst, count, dry_run):
+ stat = os.lstat(fpath)
+
+ uid = stat.st_uid
+ if uid >= src and uid < src + count:
+ uid += dst - src
+ gid = stat.st_gid
+ if gid >= src and gid < src + count:
+ gid += dst - src
+
+ logging.debug('chown(%s, %d->%d, %d->%d)' % (fpath, stat.st_uid, uid,
+ stat.st_gid, gid))
+ if not dry_run:
+ os.chown(fpath, uid, gid, follow_symlinks = False)
+
+parser = argparse.ArgumentParser(
+ description = "Remap UIDs/GIDs in a directory tree from one range to another"
+)
+
+parser.add_argument('-c', '--count', type = int, default = 65536,
+ help = 'Number of IDs in range')
+parser.add_argument('-n', '--dry-run', action = 'store_true')
+parser.add_argument('-v', '--verbose', action = 'count', default = 0)
+
+parser.add_argument('src_start', type = int, help = 'First ID of the range to remap from')
+parser.add_argument('dst_start', type = int, help = 'First ID of the range to remap to')
+parser.add_argument('targets', nargs = '+')
+
+args = parser.parse_args(sys.argv[1:])
+
+logging.basicConfig(level = max(3 - args.verbose, 0) * 10)
+
+for tgt in args.targets:
+ logging.info('Processing target: %s' % tgt)
+
+ for dirpath, dirnames, filenames in os.walk(tgt):
+ logging.debug('At directory: %s' % dirpath)
+
+ remap_file(dirpath, args.src_start, args.dst_start, args.count, args.dry_run)
+
+ for fn in filenames:
+ remap_file(os.path.join(dirpath, fn), args.src_start,
+ args.dst_start, args.count, args.dry_run)