Changeset 1:97acb08caae5

Show
Ignore:
Timestamp:
08/13/01 20:16:38 (11 years ago)
Author:
Allan Saddi <allan@saddi.com>
branch:
default
Message:

Wow, everything works.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • yafic.py

    r0 r1  
    22 
    33import sys, os, os.path, xreadlines 
     4from stat import * 
    45 
    56ATTR_PERM = 1 
     
    4344        attr = attrTemplate[attrStr[0]] 
    4445        attrStr = attrStr[1:] 
    45     elif attrStr.startswith('+') or attrStr.startsWith('-'): 
     46    elif attrStr.startswith('+') or attrStr.startswith('-'): 
    4647        attr = ATTR_DEFAULT 
    4748    else: 
     
    136137    return s.hexdigest() 
    137138 
     139attrChars = 'pinugsamc' 
     140statMap = [ST_MODE, ST_INO, ST_NLINK, 
     141           ST_UID, ST_GID, ST_SIZE, 
     142           ST_ATIME, ST_MTIME, ST_CTIME] 
     143attrStatMap = [ATTR_PERM, ATTR_INODE, ATTR_NLINK, 
     144               ATTR_UID, ATTR_GID, ATTR_SIZE, 
     145               ATTR_ATIME, ATTR_MTIME, ATTR_CTIME] 
     146 
    138147def checkFile(db, root, name, sb, attr): 
    139     from stat import * 
    140  
    141148    if db.has_key(name): 
    142149        oldsb = db[name] 
     
    145152            oldsb[i] = long(oldsb[i]) 
    146153         
    147         changedstr = "changed: %s" % name 
    148         if sb[ST_MODE] != oldsb[0] and attr & ATTR_PERM: 
    149             changedstr = changedstr + " p(%d:%d)" % (oldsb[0], sb[ST_MODE]) 
    150         if sb[ST_INO] != oldsb[1] and attr & ATTR_INODE: 
    151             pass 
    152         if sb[ST_NLINK] != oldsb[2] and attr & ATTR_INODE: 
    153             pass 
    154         if sb[ST_UID] != oldsb[3] and attr & ATTR_INODE: 
    155             pass 
    156         if sb[ST_GID] != oldsb[4] and attr & ATTR_INODE: 
    157             pass 
    158         if sb[ST_SIZE] != oldsb[5] and attr & ATTR_INODE: 
    159             pass 
    160         if sb[ST_ATIME] != oldsb[6] and attr & ATTR_INODE: 
    161             pass 
    162         if sb[ST_MTIME] != oldsb[7] and attr & ATTR_INODE: 
    163             pass 
    164         if sb[ST_CTIME] != oldsb[8] and attr & ATTR_INODE: 
    165             pass 
     154        changed = 0 
     155        changedstr = "changed: %s" % (root + name) 
     156        for i in range(9): 
     157            if sb[statMap[i]] != oldsb[i] and attr & attrStatMap[i]: 
     158                changed = 1 
     159                changedstr = changedstr + " %c(%d:%d)" % \ 
     160                             (attrChars[i], oldsb[i], sb[statMap[i]]) 
    166161        if attr & ATTR_HASH: 
    167             pass 
    168     else: 
    169         print "added: %s" 
     162            hash = hashFile(root + name) 
     163            if hash != oldsb[9]: 
     164                changed = 1 
     165                changedstr = changedstr + " h(%s:%s)" % \ 
     166                             (oldsb[9], hash) 
     167        if changed: 
     168            print changedstr 
     169    else: 
     170        if attr & ATTR_HASH: 
     171            hash = hashFile(root + name) 
     172        else: 
     173            hash = "-" 
     174        print "added: %s p(%d) i(%d) n(%d) u(%d) g(%d) s(%d) a(%d) m(%d) c(%d) h(%s)" % \ 
     175              (root + name, 
     176               sb[ST_MODE], sb[ST_INO], sb[ST_NLINK], 
     177               sb[ST_UID], sb[ST_GID], sb[ST_SIZE], 
     178               sb[ST_ATIME], sb[ST_MTIME], sb[ST_CTIME], 
     179               hash) 
    170180 
    171181def updateFile(db, root, name, sb, attr): 
    172     from stat import * 
    173      
    174     print "Scanning %s%s..." % (root, name) 
     182    #print "Scanning %s%s..." % (root, name) 
    175183    if attr & ATTR_HASH: 
    176184        hash = hashFile(root + name) 
     
    185193def scanFiles(root, dbName, oldDBName, doCheck, doUpdate): 
    186194    import bsddb 
    187     from stat import * 
    188195 
    189196    if not doCheck and not doUpdate: 
     
    202209 
    203210    while len(queue): 
     211        #print queue 
    204212        name = queue[0] 
    205213        queue = queue[1:] 
     
    234242                    if not attr & ATTR_IGNORE: 
    235243                        if doCheck: 
    236                             checkFile(oldDB, root, name, sb, attr) 
     244                            checkFile(oldDB, root, newName, sb, attr) 
    237245                        if doUpdate: 
    238246                            updateFile(db, root, newName, sb, attr) 
     
    242250                        queue.sort() 
    243251 
     252    if doUpdate: 
     253        db.close() 
     254    if doCheck: 
     255        oldDB.close() 
     256 
     257def checkDeleted(root, dbName, oldDBName): 
     258    import bsddb 
     259     
     260    oldDB = bsddb.hashopen(oldDBName) 
     261    db = bsddb.hashopen(dbName) 
     262 
     263    for entry in oldDB.keys(): 
     264        if not db.has_key(entry): 
     265            oldsb = oldDB[entry] 
     266            oldsb = oldsb.split() 
     267            for i in range(9): 
     268                oldsb[i] = long(oldsb[i]) 
     269                 
     270            print "deleted: %s p(%d) i(%d) n(%d) u(%d) g(%d) s(%d) a(%d) m(%d) c(%d) h(%s)" % \ 
     271                  (root + entry, 
     272                   oldsb[0], oldsb[1], oldsb[2], 
     273                   oldsb[3], oldsb[4], oldsb[5], 
     274                   oldsb[6], oldsb[7], oldsb[8], 
     275                   oldsb[9]) 
     276 
    244277    db.close() 
    245          
     278    oldDB.close() 
     279     
    246280def main(): 
    247281    ruleSet['/'] = (ATTR_DEFAULT, ATTR_DEFAULT) 
    248282    parseRules('test.conf') 
    249     scanFiles('', 'test_new.db', 'test.db', 0) 
    250  
     283    #print ruleSet 
     284    scanFiles('', 'test_new.db', 'test.db', 1, 1) 
     285    checkDeleted('', 'test_new.db', 'test.db') 
     286     
    251287if __name__ == '__main__': 
    252288    main()