[5a55ae6] | 1 | #!/usr/bin/env python
|
---|
| 2 | #
|
---|
| 3 | # Copyright (c) 2008 Martin Decky
|
---|
| 4 | # All rights reserved.
|
---|
| 5 | #
|
---|
| 6 | # Redistribution and use in source and binary forms, with or without
|
---|
| 7 | # modification, are permitted provided that the following conditions
|
---|
| 8 | # are met:
|
---|
| 9 | #
|
---|
| 10 | # - Redistributions of source code must retain the above copyright
|
---|
| 11 | # notice, this list of conditions and the following disclaimer.
|
---|
| 12 | # - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | # notice, this list of conditions and the following disclaimer in the
|
---|
| 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
|
---|
| 16 | # derived from this software without specific prior written permission.
|
---|
| 17 | #
|
---|
| 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
|
---|
| 20 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 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
|
---|
| 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
|
---|
| 27 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | #
|
---|
| 29 | """
|
---|
| 30 | TMPFS creator
|
---|
| 31 | """
|
---|
| 32 |
|
---|
| 33 | import sys
|
---|
| 34 | import os
|
---|
| 35 | import struct
|
---|
[8c25a4a] | 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
|
---|
[5a55ae6] | 45 |
|
---|
| 46 | def usage(prname):
|
---|
| 47 | "Print usage syntax"
|
---|
[8c25a4a] | 48 | print prname + " <PATH> <IMAGE>"
|
---|
[5a55ae6] | 49 |
|
---|
[9b70394] | 50 | def recursion(root, outf):
|
---|
| 51 | "Recursive directory walk"
|
---|
| 52 |
|
---|
| 53 | for name in os.listdir(root):
|
---|
| 54 | canon = os.path.join(root, name)
|
---|
| 55 |
|
---|
| 56 | if (os.path.isfile(canon)):
|
---|
[8c25a4a] | 57 | outf.write(struct.pack(DENTRY, DENTRY_FILE, len(name)))
|
---|
| 58 | outf.write(xstruct.little_string(name))
|
---|
[9b70394] | 59 | size = os.path.getsize(canon)
|
---|
| 60 | rd = 0;
|
---|
[8c25a4a] | 61 | outf.write(struct.pack(SIZE, size))
|
---|
[9b70394] | 62 |
|
---|
| 63 | inf = file(canon, "r")
|
---|
| 64 | while (rd < size):
|
---|
| 65 | data = inf.read(4096);
|
---|
| 66 | outf.write(data)
|
---|
| 67 | rd += len(data)
|
---|
| 68 | inf.close()
|
---|
| 69 |
|
---|
| 70 | if (os.path.isdir(canon)):
|
---|
[8c25a4a] | 71 | outf.write(struct.pack(DENTRY, DENTRY_DIRECTORY, len(name)))
|
---|
| 72 | outf.write(xstruct.little_string(name))
|
---|
| 73 | recursion(canon, outf)
|
---|
| 74 | outf.write(struct.pack(DENTRY, DENTRY_NONE, 0))
|
---|
[9b70394] | 75 |
|
---|
[5a55ae6] | 76 | def main():
|
---|
[677f620] | 77 | if (len(sys.argv) < 3):
|
---|
[5a55ae6] | 78 | usage(sys.argv[0])
|
---|
| 79 | return
|
---|
| 80 |
|
---|
[677f620] | 81 | path = os.path.abspath(sys.argv[1])
|
---|
[5a55ae6] | 82 | if (not os.path.isdir(path)):
|
---|
| 83 | print "<PATH> must be a directory"
|
---|
| 84 | return
|
---|
| 85 |
|
---|
[677f620] | 86 | outf = file(sys.argv[2], "w")
|
---|
[5a55ae6] | 87 |
|
---|
[8c25a4a] | 88 | outf.write(struct.pack(HEADER, "TMPFS"))
|
---|
[677f620] | 89 | recursion(path, outf)
|
---|
[8c25a4a] | 90 | outf.write(struct.pack(DENTRY, DENTRY_NONE, 0))
|
---|
[5a55ae6] | 91 |
|
---|
[677f620] | 92 | outf.close()
|
---|
| 93 |
|
---|
[5a55ae6] | 94 | if __name__ == '__main__':
|
---|
| 95 | main()
|
---|