Subversion Repositories HelenOS

Rev

Rev 3259 | Rev 3262 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3017 decky 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
3259 decky 36
import xstruct
3017 decky 37
 
3260 decky 38
HEADER = xstruct.convert("little: "
39
    "char[5]  /* 'TMPFS' */ "
40
)
3259 decky 41
 
3260 decky 42
DENTRY = xstruct.convert("little: "
43
    "uint8_t   /* NONE, FILE or DIRECTORY */ "
44
    "uint32_t  /* filename length */ "
45
)
46
 
47
SIZE = xstruct.convert("little: "
48
    "uint32_t  /* file size */ "
49
)
50
 
3259 decky 51
DENTRY_NONE = 0
52
DENTRY_FILE = 1
53
DENTRY_DIRECTORY = 2
54
 
3017 decky 55
def usage(prname):
56
    "Print usage syntax"
3259 decky 57
    print prname + " <PATH> <IMAGE>"
3017 decky 58
 
3046 decky 59
def recursion(root, outf):
60
    "Recursive directory walk"
61
 
62
    for name in os.listdir(root):
63
        canon = os.path.join(root, name)
64
 
65
        if (os.path.isfile(canon)):
3259 decky 66
            outf.write(struct.pack(DENTRY, DENTRY_FILE, len(name)))
67
            outf.write(xstruct.little_string(name))
3046 decky 68
            size = os.path.getsize(canon)
69
            rd = 0;
3259 decky 70
            outf.write(struct.pack(SIZE, size))
3046 decky 71
 
72
            inf = file(canon, "r")
73
            while (rd < size):
74
                data = inf.read(4096);
75
                outf.write(data)
76
                rd += len(data)
77
            inf.close()
78
 
79
        if (os.path.isdir(canon)):
3259 decky 80
            outf.write(struct.pack(DENTRY, DENTRY_DIRECTORY, len(name)))
81
            outf.write(xstruct.little_string(name))
82
            recursion(canon, outf)
83
            outf.write(struct.pack(DENTRY, DENTRY_NONE, 0))
3046 decky 84
 
3017 decky 85
def main():
3244 jermar 86
    if (len(sys.argv) < 3):
3017 decky 87
        usage(sys.argv[0])
88
        return
89
 
3244 jermar 90
    path = os.path.abspath(sys.argv[1])
3017 decky 91
    if (not os.path.isdir(path)):
92
        print "<PATH> must be a directory"
93
        return
94
 
3244 jermar 95
    outf = file(sys.argv[2], "w")
3017 decky 96
 
3259 decky 97
    outf.write(struct.pack(HEADER, "TMPFS"))
3244 jermar 98
    recursion(path, outf)
3259 decky 99
    outf.write(struct.pack(DENTRY, DENTRY_NONE, 0))
3244 jermar 100
 
101
    outf.close()
3046 decky 102
 
3017 decky 103
if __name__ == '__main__':
104
    main()