-
Notifications
You must be signed in to change notification settings - Fork 0
/
scandir.py
executable file
·183 lines (150 loc) · 4.41 KB
/
scandir.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/python
#####################################################################################
#
# scandir.py
#
# Description: Scan directory privileges
#
# Author : Fernando Diaz Sanchez <[email protected]>
# Date : 18 Jul 2014
#
# How to use : scandir.py <directorio>
#
#####################################################################################
import os
import sys
import stat
import pwd
import grp
from datetime import datetime
def processFile(currentDir):
''' Process files within this directory. '''
# Get the absolute path of the currentDir
currentDir = os.path.abspath(currentDir)
# Get a list of files in currentDir
try:
filesInCurDir = os.listdir(currentDir)
except OSError:
return
# Check all files
for file in filesInCurDir:
curFile = os.path.join(currentDir, file)
# Check if it's a normal file or directory
if os.path.isfile(curFile):
# Get the file extension
curFileExtension = curFile[-3:]
else:
get_attr(curFile)
processFile(curFile)
def get_attr(curFile):
# Check if it's a normal file or directory
if os.path.isfile(curFile):
# Get the file extension
curFileExtension = curFile[-3:]
else:
try:
statinfo = os.stat(curFile)
except OSError:
return
tam = get_size(curFile)
tamc = get_human_size(tam)
uid = get_uid_name(statinfo.st_uid)
gid = get_group_name(statinfo.st_gid)
operm = get_other_perm(curFile,statinfo)
gperm = get_group_perm(curFile,statinfo)
uperm = get_owner_perm(curFile,statinfo)
text = "%s:%s:%s:%s:%s:%s:%s:%s:%s" % (curFile,
tam,statinfo.st_uid,uid,statinfo.st_gid,
gid,uperm,gperm,operm)
print text
def get_uid_name(uid):
try:
uidn = pwd.getpwuid(uid)[0]
except KeyError:
uidn = "Unknown Owner"
return uidn
def get_group_name(gid):
try:
gidn = grp.getgrgid(gid)[0]
except KeyError:
gidn = "Unknown Group"
return gidn
def get_other_perm(curFile,statinfo):
is_read = "-"
is_write = "-"
is_exec = "-"
if bool(statinfo.st_mode & stat.S_IROTH):
is_read = "r"
if bool(statinfo.st_mode & stat.S_IWOTH):
is_write = "w"
if bool(statinfo.st_mode & stat.S_IXOTH):
is_exec = "x"
result = is_read + is_write + is_exec
return result
def get_group_perm(curFile,statinfo):
is_read = "-"
is_write = "-"
is_exec = "-"
if bool(statinfo.st_mode & stat.S_IRGRP):
is_read = "r"
if bool(statinfo.st_mode & stat.S_IWGRP):
is_write = "w"
if bool(statinfo.st_mode & stat.S_IXGRP):
is_exec = "x"
result = is_read + is_write + is_exec
return result
def get_owner_perm(curFile,statinfo):
is_read = "-"
is_write = "-"
is_exec = "-"
if bool(statinfo.st_mode & stat.S_IRUSR):
is_read = "r"
if bool(statinfo.st_mode & stat.S_IWUSR):
is_write = "w"
if bool(statinfo.st_mode & stat.S_IXUSR):
is_exec = "x"
result = is_read + is_write + is_exec
return result
def get_name_size(tam):
name_size = ""
if (tam > 1024):
if(tam > (1024 * 1024)):
return "MB"
else:
return "KB"
else:
return "bytes"
def get_human_size(tam):
name_size = ""
if (tam > 1024):
if(tam > (1024 * 1024)):
return (tam / 1024) / 1024
else:
return tam / 1024
else:
return tam
def get_size(start_path='.'):
total_size = 0
seen = {}
for dirpath, dirnames, filenames in os.walk(start_path):
for f in filenames:
fp = os.path.join(dirpath, f)
try:
stat = os.stat(fp)
except OSError:
continue
try:
seen[stat.st_ino]
except KeyError:
seen[stat.st_ino] = True
else:
continue
total_size += stat.st_size
return total_size
if __name__ == '__main__':
# Get the current working directory
currentDir = sys.argv[1]
print "Path:Size:UID:Owner:GID:Group:PermOwner:PermGroup:PermOthers"
# Start Processing
get_attr(os.path.abspath(currentDir))
processFile(currentDir)