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