-
Notifications
You must be signed in to change notification settings - Fork 0
/
createFAT.py
115 lines (103 loc) · 3.11 KB
/
createFAT.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
#!/usr/bin/python
import sys
import getopt
import os
output = 'output.txt'
output_file = open(output, "w")
cluster = 0
directory_number = 0
# 300 = 100 caracteres
cluster_size = 300
ofset = 300
file_number = 0
# Nome do arquivo em 8 bytes
# Extensao do arquivo em 3 bytes
# Cluster de dados em 1 byte (endereco dos dados) ou numero de pasta
# Tamanho do arquivo em 2 bytes
# Numero de pasta onde se encontra 2 bytes
#Exemplo
#n = numero de arquivo ou pasta
#p = parent directory
#f = free space
# name | ext |n | size|f | p
#74 65 73 74 20 20 20 20 74 78 74 00 00 05 00 00
#t e s t 20 20 20 20 t x t 00 00 05 00 00
#70 61 73 74 61 20 20 20 20 20 20 01 00 00 00 00
#p a s t a 20 20 20 20 20 20 01 00 00 00 00
#Analisa as pastas e subpastas
def parseDirectory (directory, parentDirectory):
global cluster
global directory_number
global file_number
for filename in os.listdir(directory):
count = 0
if(os.path.isdir(directory + '/' + filename) == 1):
for c in filename:
output_file.write(c.encode("hex") +' ')
#output_file.write(c +' ')
#A diferença de cumprimento do nome até 8 sao espaços
#A extensao de uma pasta sao espaços
for i in range(11 - len(filename)):
output_file.write('20 ')
directory_number += 1
output_file.write("%02d" % (directory_number,) + ' ')
#O tamanho de uma pasta e considerado como nulo
output_file.write('00 00 ')
output_file.write('00 ')
output_file.write("%02d" % (parentDirectory,) + '\n')
print filename + " is a directory"
file_number += 1
os.chdir(directory)
parseDirectory(filename, directory_number)
os.chdir("..")
else:
print filename
for c in filename:
if hex(ord(c)) != '0x2e':
output_file.write(c.encode("hex") +' ')
#output_file.write(c +' ')
count = count + 1
else:
diff = 8 - count
for i in range(diff):
output_file.write('20 ')
output_file.write("%02d" % (cluster,) + ' ')
output_file.seek(ofset + cluster*cluster_size)
#TODO open file in binary
current_file = open(directory + '/' + filename)
size = 0
for line in current_file:
for c in line:
output_file.write(c.encode("hex") +'\n')
size = size + 1
output_file.seek(file_number*16*3 + 3*12)
cluster = cluster + 1
file_number += 1
#TODO increase size bytes to support bigger files
high = size/256
output_file.write("%02x" % (high,) + ' ')
low = size - 256*high
output_file.write("%02x" % (low,) + ' ')
output_file.write('00 ')
output_file.write("%02d" % (parentDirectory,) + '\n')
def main(argv):
directory = ''
try:
opts, args = getopt.getopt(argv,"hd:",["directory"])
except getopt.GetoptError:
print 'createFAT.py -d <directory>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'createFAT.py -d <directory>'
sys.exit()
elif opt in ("-d", "--directory"):
directory = arg
print 'Directory to analyse: ', directory
#Deixa espaco para escrever no arquivo
for i in range(1000):
output_file.write(' \n')
output_file.seek(0)
parseDirectory(directory, 00)
if __name__ == "__main__":
main(sys.argv[1:])