[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
|
---|
[8c25a4a] | 35 | import xstruct
|
---|
| 36 |
|
---|
[0516fd7] | 37 | HEADER = """little:
|
---|
[8f2a852] | 38 | char tag[5] /* 'TMPFS' */
|
---|
[0516fd7] | 39 | """
|
---|
| 40 |
|
---|
| 41 | DENTRY_NONE = """little:
|
---|
| 42 | uint8_t kind /* NONE */
|
---|
| 43 | uint32_t fname_len /* 0 */
|
---|
| 44 | """
|
---|
[b3105ff6] | 45 |
|
---|
[0516fd7] | 46 | DENTRY_FILE = """little:
|
---|
| 47 | uint8_t kind /* FILE */
|
---|
| 48 | uint32_t fname_len /* filename length */
|
---|
[8f2a852] | 49 | char fname[%d] /* filename */
|
---|
[0516fd7] | 50 | uint32_t flen /* file length */
|
---|
| 51 | """
|
---|
[b3105ff6] | 52 |
|
---|
[0516fd7] | 53 | DENTRY_DIRECTORY = """little:
|
---|
| 54 | uint8_t kind /* DIRECTORY */
|
---|
| 55 | uint32_t fname_len /* filename length */
|
---|
[8f2a852] | 56 | char fname[%d] /* filename */
|
---|
[0516fd7] | 57 | """
|
---|
[8c25a4a] | 58 |
|
---|
[0516fd7] | 59 | TMPFS_NONE = 0
|
---|
| 60 | TMPFS_FILE = 1
|
---|
| 61 | TMPFS_DIRECTORY = 2
|
---|
[5a55ae6] | 62 |
|
---|
| 63 | def usage(prname):
|
---|
| 64 | "Print usage syntax"
|
---|
[8c25a4a] | 65 | print prname + " <PATH> <IMAGE>"
|
---|
[5a55ae6] | 66 |
|
---|
[9b70394] | 67 | def recursion(root, outf):
|
---|
| 68 | "Recursive directory walk"
|
---|
| 69 |
|
---|
| 70 | for name in os.listdir(root):
|
---|
| 71 | canon = os.path.join(root, name)
|
---|
| 72 |
|
---|
| 73 | if (os.path.isfile(canon)):
|
---|
| 74 | size = os.path.getsize(canon)
|
---|
[0516fd7] | 75 |
|
---|
| 76 | dentry = xstruct.create(DENTRY_FILE % len(name))
|
---|
| 77 | dentry.kind = TMPFS_FILE
|
---|
| 78 | dentry.fname_len = len(name)
|
---|
| 79 | dentry.fname = name
|
---|
| 80 | dentry.flen = size
|
---|
| 81 |
|
---|
| 82 | outf.write(dentry.pack())
|
---|
[9b70394] | 83 |
|
---|
| 84 | inf = file(canon, "r")
|
---|
[0516fd7] | 85 | rd = 0;
|
---|
[9b70394] | 86 | while (rd < size):
|
---|
| 87 | data = inf.read(4096);
|
---|
| 88 | outf.write(data)
|
---|
| 89 | rd += len(data)
|
---|
| 90 | inf.close()
|
---|
| 91 |
|
---|
| 92 | if (os.path.isdir(canon)):
|
---|
[0516fd7] | 93 | dentry = xstruct.create(DENTRY_DIRECTORY % len(name))
|
---|
| 94 | dentry.kind = TMPFS_DIRECTORY
|
---|
| 95 | dentry.fname_len = len(name)
|
---|
| 96 | dentry.fname = name
|
---|
| 97 |
|
---|
| 98 | outf.write(dentry.pack())
|
---|
| 99 |
|
---|
[8c25a4a] | 100 | recursion(canon, outf)
|
---|
[0516fd7] | 101 |
|
---|
| 102 | dentry = xstruct.create(DENTRY_NONE)
|
---|
| 103 | dentry.kind = TMPFS_NONE
|
---|
| 104 | dentry.fname_len = 0
|
---|
| 105 |
|
---|
| 106 | outf.write(dentry.pack())
|
---|
[9b70394] | 107 |
|
---|
[5a55ae6] | 108 | def main():
|
---|
[677f620] | 109 | if (len(sys.argv) < 3):
|
---|
[5a55ae6] | 110 | usage(sys.argv[0])
|
---|
| 111 | return
|
---|
| 112 |
|
---|
[677f620] | 113 | path = os.path.abspath(sys.argv[1])
|
---|
[5a55ae6] | 114 | if (not os.path.isdir(path)):
|
---|
| 115 | print "<PATH> must be a directory"
|
---|
| 116 | return
|
---|
| 117 |
|
---|
[677f620] | 118 | outf = file(sys.argv[2], "w")
|
---|
[5a55ae6] | 119 |
|
---|
[0516fd7] | 120 | header = xstruct.create(HEADER)
|
---|
| 121 | header.tag = "TMPFS"
|
---|
| 122 |
|
---|
| 123 | outf.write(header.pack())
|
---|
| 124 |
|
---|
[677f620] | 125 | recursion(path, outf)
|
---|
[0516fd7] | 126 |
|
---|
| 127 | dentry = xstruct.create(DENTRY_NONE)
|
---|
| 128 | dentry.kind = TMPFS_NONE
|
---|
| 129 | dentry.fname_len = 0
|
---|
| 130 |
|
---|
| 131 | outf.write(dentry.pack())
|
---|
| 132 |
|
---|
[677f620] | 133 | outf.close()
|
---|
[0516fd7] | 134 |
|
---|
[5a55ae6] | 135 | if __name__ == '__main__':
|
---|
| 136 | main()
|
---|