Subversion Repositories HelenOS

Rev

Rev 3046 | Rev 3259 | 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
36
 
37
def usage(prname):
38
    "Print usage syntax"
3021 decky 39
    print prname + " <ALIGNMENT> <PATH> <IMAGE>"
3017 decky 40
 
3046 decky 41
def recursion(root, outf):
42
    "Recursive directory walk"
43
 
44
    payload_size = 0
45
 
46
    for name in os.listdir(root):
47
        canon = os.path.join(root, name)
48
 
49
        if (os.path.isfile(canon)):
50
            outf.write(struct.pack("<BL" + ("%d" % len(name)) + "s", 1, len(name), name))
51
            payload_size += 5 + len(name)
52
            size = os.path.getsize(canon)
53
            rd = 0;
54
            outf.write(struct.pack("<L", size))
55
            payload_size += 4
56
 
57
            inf = file(canon, "r")
58
            while (rd < size):
59
                data = inf.read(4096);
60
                outf.write(data)
61
                payload_size += len(data)
62
                rd += len(data)
63
            inf.close()
64
 
65
        if (os.path.isdir(canon)):
66
            outf.write(struct.pack("<BL" + ("%d" % len(name)) + "s", 2, len(name), name))
67
            payload_size += 5 + len(name)
68
            payload_size += recursion(canon, outf)
69
            outf.write(struct.pack("<BL", 0, 0))
70
            payload_size += 5
71
 
72
    return payload_size
73
 
3017 decky 74
def main():
3244 jermar 75
    if (len(sys.argv) < 3):
3017 decky 76
        usage(sys.argv[0])
77
        return
78
 
3244 jermar 79
    path = os.path.abspath(sys.argv[1])
3017 decky 80
    if (not os.path.isdir(path)):
81
        print "<PATH> must be a directory"
82
        return
83
 
3244 jermar 84
    outf = file(sys.argv[2], "w")
3017 decky 85
 
3046 decky 86
    outf.write(struct.pack("<5s", "TMPFS"))
3244 jermar 87
    recursion(path, outf)
3046 decky 88
    outf.write(struct.pack("<BL", 0, 0))
3244 jermar 89
 
90
    outf.close()
3046 decky 91
 
3017 decky 92
if __name__ == '__main__':
93
    main()