summaryrefslogtreecommitdiff
path: root/perm_offset.py
blob: 4e13eb3c59228109b1a2c071b8abd508cb82ba4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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)