Rev 3403 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3403 | Rev 3561 | ||
---|---|---|---|
1 | #!/usr/bin/env python |
1 | #!/usr/bin/env python |
2 | # |
2 | # |
3 | # Copyright (c) 2008 Martin Decky |
3 | # Copyright (c) 2008 Martin Decky |
4 | # All rights reserved. |
4 | # All rights reserved. |
5 | # |
5 | # |
6 | # Redistribution and use in source and binary forms, with or without |
6 | # Redistribution and use in source and binary forms, with or without |
7 | # modification, are permitted provided that the following conditions |
7 | # modification, are permitted provided that the following conditions |
8 | # are met: |
8 | # are met: |
9 | # |
9 | # |
10 | # - Redistributions of source code must retain the above copyright |
10 | # - Redistributions of source code must retain the above copyright |
11 | # notice, this list of conditions and the following disclaimer. |
11 | # notice, this list of conditions and the following disclaimer. |
12 | # - Redistributions in binary form must reproduce the above copyright |
12 | # - Redistributions in binary form must reproduce the above copyright |
13 | # notice, this list of conditions and the following disclaimer in the |
13 | # notice, this list of conditions and the following disclaimer in the |
14 | # documentation and/or other materials provided with the distribution. |
14 | # documentation and/or other materials provided with the distribution. |
15 | # - The name of the author may not be used to endorse or promote products |
15 | # - The name of the author may not be used to endorse or promote products |
16 | # derived from this software without specific prior written permission. |
16 | # derived from this software without specific prior written permission. |
17 | # |
17 | # |
18 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
19 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
24 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | # |
28 | # |
29 | """ |
29 | """ |
30 | TMPFS creator |
30 | TMPFS creator |
31 | """ |
31 | """ |
32 | 32 | ||
33 | import sys |
33 | import sys |
34 | import os |
34 | import os |
35 | import xstruct |
35 | import xstruct |
36 | 36 | ||
- | 37 | exclude_names = set(['.svn']) |
|
- | 38 | ||
37 | HEADER = """little: |
39 | HEADER = """little: |
38 | char tag[5] /* 'TMPFS' */ |
40 | char tag[5] /* 'TMPFS' */ |
39 | """ |
41 | """ |
40 | 42 | ||
41 | DENTRY_NONE = """little: |
43 | DENTRY_NONE = """little: |
42 | uint8_t kind /* NONE */ |
44 | uint8_t kind /* NONE */ |
43 | uint32_t fname_len /* 0 */ |
45 | uint32_t fname_len /* 0 */ |
44 | """ |
46 | """ |
45 | 47 | ||
46 | DENTRY_FILE = """little: |
48 | DENTRY_FILE = """little: |
47 | uint8_t kind /* FILE */ |
49 | uint8_t kind /* FILE */ |
48 | uint32_t fname_len /* filename length */ |
50 | uint32_t fname_len /* filename length */ |
49 | char fname[%d] /* filename */ |
51 | char fname[%d] /* filename */ |
50 | uint32_t flen /* file length */ |
52 | uint32_t flen /* file length */ |
51 | """ |
53 | """ |
52 | 54 | ||
53 | DENTRY_DIRECTORY = """little: |
55 | DENTRY_DIRECTORY = """little: |
54 | uint8_t kind /* DIRECTORY */ |
56 | uint8_t kind /* DIRECTORY */ |
55 | uint32_t fname_len /* filename length */ |
57 | uint32_t fname_len /* filename length */ |
56 | char fname[%d] /* filename */ |
58 | char fname[%d] /* filename */ |
57 | """ |
59 | """ |
58 | 60 | ||
59 | TMPFS_NONE = 0 |
61 | TMPFS_NONE = 0 |
60 | TMPFS_FILE = 1 |
62 | TMPFS_FILE = 1 |
61 | TMPFS_DIRECTORY = 2 |
63 | TMPFS_DIRECTORY = 2 |
62 | 64 | ||
63 | def usage(prname): |
65 | def usage(prname): |
64 | "Print usage syntax" |
66 | "Print usage syntax" |
65 | print prname + " <PATH> <IMAGE>" |
67 | print prname + " <PATH> <IMAGE>" |
66 | 68 | ||
67 | def recursion(root, outf): |
69 | def recursion(root, outf): |
68 | "Recursive directory walk" |
70 | "Recursive directory walk" |
69 | 71 | ||
70 | for name in os.listdir(root): |
72 | for name in os.listdir(root): |
71 | canon = os.path.join(root, name) |
73 | canon = os.path.join(root, name) |
72 | 74 | ||
73 | if (os.path.isfile(canon)): |
75 | if (os.path.isfile(canon) and (not name in exclude_names)): |
74 | size = os.path.getsize(canon) |
76 | size = os.path.getsize(canon) |
75 | 77 | ||
76 | dentry = xstruct.create(DENTRY_FILE % len(name)) |
78 | dentry = xstruct.create(DENTRY_FILE % len(name)) |
77 | dentry.kind = TMPFS_FILE |
79 | dentry.kind = TMPFS_FILE |
78 | dentry.fname_len = len(name) |
80 | dentry.fname_len = len(name) |
79 | dentry.fname = name |
81 | dentry.fname = name |
80 | dentry.flen = size |
82 | dentry.flen = size |
81 | 83 | ||
82 | outf.write(dentry.pack()) |
84 | outf.write(dentry.pack()) |
83 | 85 | ||
84 | inf = file(canon, "r") |
86 | inf = file(canon, "r") |
85 | rd = 0; |
87 | rd = 0; |
86 | while (rd < size): |
88 | while (rd < size): |
87 | data = inf.read(4096); |
89 | data = inf.read(4096); |
88 | outf.write(data) |
90 | outf.write(data) |
89 | rd += len(data) |
91 | rd += len(data) |
90 | inf.close() |
92 | inf.close() |
91 | 93 | ||
92 | if (os.path.isdir(canon)): |
94 | if (os.path.isdir(canon) and (not name in exclude_names)): |
93 | dentry = xstruct.create(DENTRY_DIRECTORY % len(name)) |
95 | dentry = xstruct.create(DENTRY_DIRECTORY % len(name)) |
94 | dentry.kind = TMPFS_DIRECTORY |
96 | dentry.kind = TMPFS_DIRECTORY |
95 | dentry.fname_len = len(name) |
97 | dentry.fname_len = len(name) |
96 | dentry.fname = name |
98 | dentry.fname = name |
97 | 99 | ||
98 | outf.write(dentry.pack()) |
100 | outf.write(dentry.pack()) |
99 | 101 | ||
100 | recursion(canon, outf) |
102 | recursion(canon, outf) |
101 | 103 | ||
102 | dentry = xstruct.create(DENTRY_NONE) |
104 | dentry = xstruct.create(DENTRY_NONE) |
103 | dentry.kind = TMPFS_NONE |
105 | dentry.kind = TMPFS_NONE |
104 | dentry.fname_len = 0 |
106 | dentry.fname_len = 0 |
105 | 107 | ||
106 | outf.write(dentry.pack()) |
108 | outf.write(dentry.pack()) |
107 | 109 | ||
108 | def main(): |
110 | def main(): |
109 | if (len(sys.argv) < 3): |
111 | if (len(sys.argv) < 3): |
110 | usage(sys.argv[0]) |
112 | usage(sys.argv[0]) |
111 | return |
113 | return |
112 | 114 | ||
113 | path = os.path.abspath(sys.argv[1]) |
115 | path = os.path.abspath(sys.argv[1]) |
114 | if (not os.path.isdir(path)): |
116 | if (not os.path.isdir(path)): |
115 | print "<PATH> must be a directory" |
117 | print "<PATH> must be a directory" |
116 | return |
118 | return |
117 | 119 | ||
118 | outf = file(sys.argv[2], "w") |
120 | outf = file(sys.argv[2], "w") |
119 | 121 | ||
120 | header = xstruct.create(HEADER) |
122 | header = xstruct.create(HEADER) |
121 | header.tag = "TMPFS" |
123 | header.tag = "TMPFS" |
122 | 124 | ||
123 | outf.write(header.pack()) |
125 | outf.write(header.pack()) |
124 | 126 | ||
125 | recursion(path, outf) |
127 | recursion(path, outf) |
126 | 128 | ||
127 | dentry = xstruct.create(DENTRY_NONE) |
129 | dentry = xstruct.create(DENTRY_NONE) |
128 | dentry.kind = TMPFS_NONE |
130 | dentry.kind = TMPFS_NONE |
129 | dentry.fname_len = 0 |
131 | dentry.fname_len = 0 |
130 | 132 | ||
131 | outf.write(dentry.pack()) |
133 | outf.write(dentry.pack()) |
132 | 134 | ||
133 | outf.close() |
135 | outf.close() |
134 | 136 | ||
135 | if __name__ == '__main__': |
137 | if __name__ == '__main__': |
136 | main() |
138 | main() |
137 | 139 |