Rev 3260 | Rev 3263 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3260 | Rev 3262 | ||
---|---|---|---|
Line 30... | Line 30... | ||
30 | TMPFS creator |
30 | TMPFS creator |
31 | """ |
31 | """ |
32 | 32 | ||
33 | import sys |
33 | import sys |
34 | import os |
34 | import os |
35 | import struct |
- | |
36 | import xstruct |
35 | import xstruct |
37 | 36 | ||
38 | HEADER = xstruct.convert("little: " |
37 | HEADER = """little: |
39 | "char[5] /* 'TMPFS' */ " |
38 | char[5] tag /* 'TMPFS' */ |
- | 39 | """ |
|
40 | ) |
40 | |
- | 41 | DENTRY_NONE = """little: |
|
- | 42 | uint8_t kind /* NONE */ |
|
- | 43 | uint32_t fname_len /* 0 */ |
|
- | 44 | """ |
|
41 | 45 | ||
42 | DENTRY = xstruct.convert("little: " |
46 | DENTRY_FILE = """little: |
43 | "uint8_t /* NONE, FILE or DIRECTORY */ " |
47 | uint8_t kind /* FILE */ |
44 | "uint32_t /* filename length */ " |
48 | uint32_t fname_len /* filename length */ |
- | 49 | char[%d] fname /* filename */ |
|
- | 50 | uint32_t flen /* file length */ |
|
45 | ) |
51 | """ |
46 | 52 | ||
47 | SIZE = xstruct.convert("little: " |
53 | DENTRY_DIRECTORY = """little: |
- | 54 | uint8_t kind /* DIRECTORY */ |
|
48 | "uint32_t /* file size */ " |
55 | uint32_t fname_len /* filename length */ |
- | 56 | char[%d] fname /* filename */ |
|
49 | ) |
57 | """ |
50 | 58 | ||
51 | DENTRY_NONE = 0 |
59 | TMPFS_NONE = 0 |
52 | DENTRY_FILE = 1 |
60 | TMPFS_FILE = 1 |
53 | DENTRY_DIRECTORY = 2 |
61 | TMPFS_DIRECTORY = 2 |
54 | 62 | ||
55 | def usage(prname): |
63 | def usage(prname): |
56 | "Print usage syntax" |
64 | "Print usage syntax" |
57 | print prname + " <PATH> <IMAGE>" |
65 | print prname + " <PATH> <IMAGE>" |
58 | 66 | ||
Line 61... | Line 69... | ||
61 | 69 | ||
62 | for name in os.listdir(root): |
70 | for name in os.listdir(root): |
63 | canon = os.path.join(root, name) |
71 | canon = os.path.join(root, name) |
64 | 72 | ||
65 | if (os.path.isfile(canon)): |
73 | if (os.path.isfile(canon)): |
66 | outf.write(struct.pack(DENTRY, DENTRY_FILE, len(name))) |
- | |
67 | outf.write(xstruct.little_string(name)) |
- | |
68 | size = os.path.getsize(canon) |
74 | size = os.path.getsize(canon) |
- | 75 | ||
- | 76 | dentry = xstruct.create(DENTRY_FILE % len(name)) |
|
- | 77 | dentry.kind = TMPFS_FILE |
|
- | 78 | dentry.fname_len = len(name) |
|
- | 79 | dentry.fname = name |
|
69 | rd = 0; |
80 | dentry.flen = size |
- | 81 | ||
70 | outf.write(struct.pack(SIZE, size)) |
82 | outf.write(dentry.pack()) |
71 | 83 | ||
72 | inf = file(canon, "r") |
84 | inf = file(canon, "r") |
- | 85 | rd = 0; |
|
73 | while (rd < size): |
86 | while (rd < size): |
74 | data = inf.read(4096); |
87 | data = inf.read(4096); |
75 | outf.write(data) |
88 | outf.write(data) |
76 | rd += len(data) |
89 | rd += len(data) |
77 | inf.close() |
90 | inf.close() |
78 | 91 | ||
79 | if (os.path.isdir(canon)): |
92 | if (os.path.isdir(canon)): |
80 | outf.write(struct.pack(DENTRY, DENTRY_DIRECTORY, len(name))) |
93 | dentry = xstruct.create(DENTRY_DIRECTORY % len(name)) |
- | 94 | dentry.kind = TMPFS_DIRECTORY |
|
- | 95 | dentry.fname_len = len(name) |
|
- | 96 | dentry.fname = name |
|
- | 97 | ||
81 | outf.write(xstruct.little_string(name)) |
98 | outf.write(dentry.pack()) |
- | 99 | ||
82 | recursion(canon, outf) |
100 | recursion(canon, outf) |
- | 101 | ||
83 | outf.write(struct.pack(DENTRY, DENTRY_NONE, 0)) |
102 | dentry = xstruct.create(DENTRY_NONE) |
- | 103 | dentry.kind = TMPFS_NONE |
|
- | 104 | dentry.fname_len = 0 |
|
- | 105 | ||
- | 106 | outf.write(dentry.pack()) |
|
84 | 107 | ||
85 | def main(): |
108 | def main(): |
86 | if (len(sys.argv) < 3): |
109 | if (len(sys.argv) < 3): |
87 | usage(sys.argv[0]) |
110 | usage(sys.argv[0]) |
88 | return |
111 | return |
Line 92... | Line 115... | ||
92 | print "<PATH> must be a directory" |
115 | print "<PATH> must be a directory" |
93 | return |
116 | return |
94 | 117 | ||
95 | outf = file(sys.argv[2], "w") |
118 | outf = file(sys.argv[2], "w") |
96 | 119 | ||
- | 120 | header = xstruct.create(HEADER) |
|
- | 121 | header.tag = "TMPFS" |
|
- | 122 | ||
97 | outf.write(struct.pack(HEADER, "TMPFS")) |
123 | outf.write(header.pack()) |
- | 124 | ||
98 | recursion(path, outf) |
125 | recursion(path, outf) |
- | 126 | ||
99 | outf.write(struct.pack(DENTRY, DENTRY_NONE, 0)) |
127 | dentry = xstruct.create(DENTRY_NONE) |
- | 128 | dentry.kind = TMPFS_NONE |
|
- | 129 | dentry.fname_len = 0 |
|
100 | 130 | ||
- | 131 | outf.write(dentry.pack()) |
|
- | 132 | ||
101 | outf.close() |
133 | outf.close() |
102 | 134 | ||
103 | if __name__ == '__main__': |
135 | if __name__ == '__main__': |
104 | main() |
136 | main() |