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