Changeset 61

Show
Ignore:
Timestamp:
05/31/07 08:14:40 (2 years ago)
Author:
zcutlip
Message:

-do a find ! -type d once outside the os.walk loop in order to catch any non-dir files in the root
-do a non-recursive find using -maxdepth 1 since we are already recursing using os.walk
-quote 2nd argment to chcon to protect spaces, etc in filenames
-reworked input file parsing in relabel() to deal with possible relative paths. no longer backwards compatible with older input file format
-disabled -e option--individual file relabeling doesn't appear to work

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/RHEL5/scripts/xar

    r48 r61  
    4848    elif re.match('^/selinux(/.*)?', path): 
    4949        return False 
     50    elif re.match('^/dev(/.*)?', path): 
     51        return False 
    5052    return True 
    5153 
     
    6668        print "Indexing extended attributes..." 
    6769     
    68     # Traverse the root directory, running indexcon on each subdir 
     70   #Do the find once outside the for loop to catch any non-dir files in the root 
     71    cmd_str= "find '%s' -maxdepth 1 ! -type d -printf \'%%Z\t%%p\\n' 2>/dev/null >> %s" % \ 
     72              (root, ctx_file) 
     73    if verbose: 
     74        print path 
     75    os.system(cmd_str) 
     76     
     77    # Traverse the root directory, running find non-recursively on each subdir 
     78 
    6979    for root, dirs, files in os.walk(root):         
    7080        for dirname in dirs: 
    7181            path = os.path.join(root,dirname) 
    72              
    7382            # Check to see that the path isn't one we're excluding 
    7483            if is_legal_path(path): 
    75                 #ZJC use find instead of indexcon/searchcon 
    7684                #output in the form of  
    7785                #user_u:object_r:file_t<tab>/path/to/file 
    78                 cmd_str= "find '%s/' -printf '%%Z\t%%p\\n' >> %s" % \ 
     86                cmd_str= "find '%s' -maxdepth 1 -printf '%%Z\t%%p\\n' 2>/dev/null >> %s" % \ 
    7987                          (path, ctx_file) 
    8088                if verbose: 
     
    93101 
    94102def update_ctx(filename, ctx): 
    95     cmd = "chcon %s %s" % (ctx, filename) 
     103    cmd = "chcon %s '%s'" % (ctx, filename) 
    96104    os.system(cmd) 
    97105 
    98106def relabel_file(filename, ctx_file): 
     107    #GIANT FIXME: individual file relabeling doesn't appear to work 
     108    #and has been disabled.  probably something trivial... 
    99109    try: 
    100110        ifile = open(ctx_file, 'r') 
     
    106116    regex = re.compile('\.\/', re.VERBOSE) 
    107117    for l in lines: 
    108         vals = re.split("\s", l) 
    109         ctx = vals[0]; path = vals[2] 
    110         # Remove trailing './' from entries in the cwd 
    111         clean_path = regex.sub('', path) 
     118         
     119        #ZJC Grab the filename, context from the line 
     120        #remove whitespace (eol markers, etc) from line start/end 
     121        l=l.strip() 
     122         
     123        #ZJC split line only one time on whitepace, grab both halves 
     124        ctx,path = re.split('\s+',l,1) 
    112125 
    113         # We have this file's context in the context file 
     126        if verbose: 
     127            print path 
     128        # Check to see that the path isn't one we're excluding 
    114129        if (clean_path == filename): 
    115130            update_ctx(filename, ctx) 
     
    132147     
    133148    for l in lines: 
    134        #ZJC--continue to work with the previous input format 
    135        #or the new one generated by find 
     149        #ZJC--continue to work with the previous input format 
     150        #or the new one generated by find 
    136151         
    137152        #ZJC Grab the filename, context from the line 
    138        #remove whitespace (eol markers, etc) from line start/end 
     153        #remove whitespace (eol markers, etc) from line start/end 
    139154        l=l.strip() 
    140155         
    141         #ZJC split line only on time on whitepace, grab the 1st field 
    142         vals = re.split("\s", l, 1) 
    143         ctx = vals[0] 
    144          
    145         #ZJC split line only one time on slash, 
    146         #path is a "/" plus the 2nd field 
    147         vals = re.split("/", l, 1) 
    148         path = "/" +vals[1] 
     156        #ZJC split line only one time on whitepace, grab both halves 
     157        ctx,path = re.split('\s+',l,1) 
    149158 
    150159        if verbose: 
     
    178187        relabel(ctx_file, verbose) 
    179188    elif opts.has_key('-e'): 
    180         relabel_file(opts['-e'], ctx_file) 
     189        print "Individual file relabeling not implemented." 
     190        exit(-1) 
     191        #relabel_file(opts['-e'], ctx_file) 
    181192    elif opts.has_key('-h'): 
    182193        usage()