Rev 3244 | Rev 3260 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3244 | Rev 3259 | ||
---|---|---|---|
Line 31... | Line 31... | ||
31 | """ |
31 | """ |
32 | 32 | ||
33 | import sys |
33 | import sys |
34 | import os |
34 | import os |
35 | import struct |
35 | import struct |
- | 36 | import xstruct |
|
- | 37 | ||
- | 38 | HEADER = xstruct.convert("little: char[5]") |
|
- | 39 | DENTRY = xstruct.convert("little: uint8_t uint32_t") |
|
- | 40 | SIZE = xstruct.convert("little: uint32_t") |
|
- | 41 | ||
- | 42 | DENTRY_NONE = 0 |
|
- | 43 | DENTRY_FILE = 1 |
|
- | 44 | DENTRY_DIRECTORY = 2 |
|
36 | 45 | ||
37 | def usage(prname): |
46 | def usage(prname): |
38 | "Print usage syntax" |
47 | "Print usage syntax" |
39 | print prname + " <ALIGNMENT> <PATH> <IMAGE>" |
48 | print prname + " <PATH> <IMAGE>" |
40 | 49 | ||
41 | def recursion(root, outf): |
50 | def recursion(root, outf): |
42 | "Recursive directory walk" |
51 | "Recursive directory walk" |
43 | 52 | ||
44 | payload_size = 0 |
- | |
45 | - | ||
46 | for name in os.listdir(root): |
53 | for name in os.listdir(root): |
47 | canon = os.path.join(root, name) |
54 | canon = os.path.join(root, name) |
48 | 55 | ||
49 | if (os.path.isfile(canon)): |
56 | if (os.path.isfile(canon)): |
50 | outf.write(struct.pack("<BL" + ("%d" % len(name)) + "s", 1, len(name), name)) |
57 | outf.write(struct.pack(DENTRY, DENTRY_FILE, len(name))) |
51 | payload_size += 5 + len(name) |
58 | outf.write(xstruct.little_string(name)) |
52 | size = os.path.getsize(canon) |
59 | size = os.path.getsize(canon) |
53 | rd = 0; |
60 | rd = 0; |
54 | outf.write(struct.pack("<L", size)) |
61 | outf.write(struct.pack(SIZE, size)) |
55 | payload_size += 4 |
- | |
56 | 62 | ||
57 | inf = file(canon, "r") |
63 | inf = file(canon, "r") |
58 | while (rd < size): |
64 | while (rd < size): |
59 | data = inf.read(4096); |
65 | data = inf.read(4096); |
60 | outf.write(data) |
66 | outf.write(data) |
61 | payload_size += len(data) |
- | |
62 | rd += len(data) |
67 | rd += len(data) |
63 | inf.close() |
68 | inf.close() |
64 | 69 | ||
65 | if (os.path.isdir(canon)): |
70 | if (os.path.isdir(canon)): |
66 | outf.write(struct.pack("<BL" + ("%d" % len(name)) + "s", 2, len(name), name)) |
71 | outf.write(struct.pack(DENTRY, DENTRY_DIRECTORY, len(name))) |
67 | payload_size += 5 + len(name) |
72 | outf.write(xstruct.little_string(name)) |
68 | payload_size += recursion(canon, outf) |
73 | recursion(canon, outf) |
69 | outf.write(struct.pack("<BL", 0, 0)) |
74 | outf.write(struct.pack(DENTRY, DENTRY_NONE, 0)) |
70 | payload_size += 5 |
- | |
71 | - | ||
72 | return payload_size |
- | |
73 | 75 | ||
74 | def main(): |
76 | def main(): |
75 | if (len(sys.argv) < 3): |
77 | if (len(sys.argv) < 3): |
76 | usage(sys.argv[0]) |
78 | usage(sys.argv[0]) |
77 | return |
79 | return |
Line 81... | Line 83... | ||
81 | print "<PATH> must be a directory" |
83 | print "<PATH> must be a directory" |
82 | return |
84 | return |
83 | 85 | ||
84 | outf = file(sys.argv[2], "w") |
86 | outf = file(sys.argv[2], "w") |
85 | 87 | ||
86 | outf.write(struct.pack("<5s", "TMPFS")) |
88 | outf.write(struct.pack(HEADER, "TMPFS")) |
87 | recursion(path, outf) |
89 | recursion(path, outf) |
88 | outf.write(struct.pack("<BL", 0, 0)) |
90 | outf.write(struct.pack(DENTRY, DENTRY_NONE, 0)) |
89 | 91 | ||
90 | outf.close() |
92 | outf.close() |
91 | 93 | ||
92 | if __name__ == '__main__': |
94 | if __name__ == '__main__': |
93 | main() |
95 | main() |