~martin-decky/helenos/rcu

« back to all changes in this revision

Viewing changes to tools/mkfat.py

  • Committer: Martin Sucha
  • Date: 2011-07-24 21:18:16 UTC
  • mto: This revision was merged to the branch mainline in revision 1123.
  • Revision ID: sucha14@st.fmph.uniba.sk-20110724211816-as9v1bgyz4t1u0z6
Add support for ext2 to be used as RAM disk format

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
import random
37
37
import xstruct
38
38
import array
39
 
 
40
 
exclude_names = set(['.svn', '.bzr'])
41
 
 
42
 
def align_up(size, alignment):
43
 
        "Return size aligned up to alignment"
44
 
        
45
 
        if (size % alignment == 0):
46
 
                return size
47
 
        
48
 
        return ((size // alignment) + 1) * alignment
 
39
from imgutil import *
49
40
 
50
41
def subtree_size(root, cluster_size, dirent_size):
51
42
        "Recursive directory walk and calculate size"
53
44
        size = 0
54
45
        files = 2
55
46
        
56
 
        for name in os.listdir(root):
57
 
                canon = os.path.join(root, name)
58
 
                
59
 
                if (os.path.isfile(canon) and (not name in exclude_names)):
60
 
                        size += align_up(os.path.getsize(canon), cluster_size)
 
47
        for item in listdir_items(root):
 
48
                if item.is_file:
 
49
                        size += align_up(item.size, cluster_size)
61
50
                        files += 1
62
 
                
63
 
                if (os.path.isdir(canon) and (not name in exclude_names)):
64
 
                        size += subtree_size(canon, cluster_size, dirent_size)
 
51
                elif item.is_dir:
 
52
                        size += subtree_size(item.path, cluster_size, dirent_size)
65
53
                        files += 1
66
54
        
67
55
        return size + align_up(files * dirent_size, cluster_size)
71
59
        
72
60
        return len(os.listdir(root))
73
61
 
74
 
def write_file(path, outf, cluster_size, data_start, fat, reserved_clusters):
 
62
def write_file(item, outf, cluster_size, data_start, fat, reserved_clusters):
75
63
        "Store the contents of a file"
76
64
        
77
 
        size = os.path.getsize(path)
78
65
        prev = -1
79
66
        first = 0
80
67
        
81
 
        inf = open(path, "rb")
82
 
        rd = 0;
83
 
        while (rd < size):
 
68
        for data in chunks(item, cluster_size):
84
69
                empty_cluster = fat.index(0)
85
70
                fat[empty_cluster] = 0xffff
86
71
                
91
76
                
92
77
                prev = empty_cluster
93
78
                
94
 
                data = bytes(inf.read(cluster_size));
95
79
                outf.seek(data_start + (empty_cluster - reserved_clusters) * cluster_size)
96
80
                outf.write(data)
97
 
                rd += len(data)
98
 
        inf.close()
99
81
        
100
 
        return first, size
 
82
        return first, item.size
101
83
 
102
84
def write_directory(directory, outf, cluster_size, data_start, fat, reserved_clusters, dirent_size, empty_cluster):
103
85
        "Store the contents of a directory"
302
284
        else:
303
285
                empty_cluster = 0
304
286
        
305
 
        for name in os.listdir(root):
306
 
                canon = os.path.join(root, name)
307
 
                
308
 
                if (os.path.isfile(canon) and (not name in exclude_names)):
309
 
                        rv = write_file(canon, outf, cluster_size, data_start, fat, reserved_clusters)
310
 
                        directory.append(create_dirent(name, False, rv[0], rv[1]))
311
 
                
312
 
                if (os.path.isdir(canon) and (not name in exclude_names)):
313
 
                        rv = recursion(False, canon, outf, cluster_size, root_start, data_start, fat, reserved_clusters, dirent_size, empty_cluster)
314
 
                        directory.append(create_dirent(name, True, rv[0], rv[1]))
 
287
        for item in listdir_items(root):                
 
288
                if item.is_file:
 
289
                        rv = write_file(item, outf, cluster_size, data_start, fat, reserved_clusters)
 
290
                        directory.append(create_dirent(item.name, False, rv[0], rv[1]))
 
291
                elif item.is_dir:
 
292
                        rv = recursion(False, item.path, outf, cluster_size, root_start, data_start, fat, reserved_clusters, dirent_size, empty_cluster)
 
293
                        directory.append(create_dirent(item.name, True, rv[0], rv[1]))
315
294
        
316
295
        if (head):
317
296
                outf.seek(root_start)