Subversion Repositories HelenOS

Rev

Rev 3046 | Rev 3259 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3046 Rev 3244
1
#!/usr/bin/env python
1
#!/usr/bin/env python
2
#
2
#
3
# Copyright (c) 2008 Martin Decky
3
# Copyright (c) 2008 Martin Decky
4
# All rights reserved.
4
# All rights reserved.
5
#
5
#
6
# Redistribution and use in source and binary forms, with or without
6
# Redistribution and use in source and binary forms, with or without
7
# modification, are permitted provided that the following conditions
7
# modification, are permitted provided that the following conditions
8
# are met:
8
# are met:
9
#
9
#
10
# - Redistributions of source code must retain the above copyright
10
# - Redistributions of source code must retain the above copyright
11
#   notice, this list of conditions and the following disclaimer.
11
#   notice, this list of conditions and the following disclaimer.
12
# - Redistributions in binary form must reproduce the above copyright
12
# - Redistributions in binary form must reproduce the above copyright
13
#   notice, this list of conditions and the following disclaimer in the
13
#   notice, this list of conditions and the following disclaimer in the
14
#   documentation and/or other materials provided with the distribution.
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
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.
16
#   derived from this software without specific prior written permission.
17
#
17
#
18
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
19
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
24
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
27
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#
28
#
29
"""
29
"""
30
TMPFS creator
30
TMPFS creator
31
"""
31
"""
32
 
32
 
33
import sys
33
import sys
34
import os
34
import os
35
import struct
35
import struct
36
 
36
 
37
def align_up(size, alignment):
-
 
38
    "Align upwards to a given alignment"
-
 
39
    return (((size) + ((alignment) - 1)) & ~((alignment) - 1))
-
 
40
 
-
 
41
def usage(prname):
37
def usage(prname):
42
    "Print usage syntax"
38
    "Print usage syntax"
43
    print prname + " <ALIGNMENT> <PATH> <IMAGE>"
39
    print prname + " <ALIGNMENT> <PATH> <IMAGE>"
44
 
40
 
45
def recursion(root, outf):
41
def recursion(root, outf):
46
    "Recursive directory walk"
42
    "Recursive directory walk"
47
   
43
   
48
    payload_size = 0
44
    payload_size = 0
49
   
45
   
50
    for name in os.listdir(root):
46
    for name in os.listdir(root):
51
        canon = os.path.join(root, name)
47
        canon = os.path.join(root, name)
52
       
48
       
53
        if (os.path.isfile(canon)):
49
        if (os.path.isfile(canon)):
54
            outf.write(struct.pack("<BL" + ("%d" % len(name)) + "s", 1, len(name), name))
50
            outf.write(struct.pack("<BL" + ("%d" % len(name)) + "s", 1, len(name), name))
55
            payload_size += 5 + len(name)
51
            payload_size += 5 + len(name)
56
            size = os.path.getsize(canon)
52
            size = os.path.getsize(canon)
57
            rd = 0;
53
            rd = 0;
58
            outf.write(struct.pack("<L", size))
54
            outf.write(struct.pack("<L", size))
59
            payload_size += 4
55
            payload_size += 4
60
           
56
           
61
            inf = file(canon, "r")
57
            inf = file(canon, "r")
62
            while (rd < size):
58
            while (rd < size):
63
                data = inf.read(4096);
59
                data = inf.read(4096);
64
                outf.write(data)
60
                outf.write(data)
65
                payload_size += len(data)
61
                payload_size += len(data)
66
                rd += len(data)
62
                rd += len(data)
67
            inf.close()
63
            inf.close()
68
       
64
       
69
        if (os.path.isdir(canon)):
65
        if (os.path.isdir(canon)):
70
            outf.write(struct.pack("<BL" + ("%d" % len(name)) + "s", 2, len(name), name))
66
            outf.write(struct.pack("<BL" + ("%d" % len(name)) + "s", 2, len(name), name))
71
            payload_size += 5 + len(name)
67
            payload_size += 5 + len(name)
72
            payload_size += recursion(canon, outf)
68
            payload_size += recursion(canon, outf)
73
            outf.write(struct.pack("<BL", 0, 0))
69
            outf.write(struct.pack("<BL", 0, 0))
74
            payload_size += 5
70
            payload_size += 5
75
   
71
   
76
    return payload_size
72
    return payload_size
77
 
73
 
78
def main():
74
def main():
79
    if (len(sys.argv) < 4):
75
    if (len(sys.argv) < 3):
80
        usage(sys.argv[0])
76
        usage(sys.argv[0])
81
        return
77
        return
82
   
78
   
83
    if (not sys.argv[1].isdigit()):
-
 
84
        print "<ALIGNMENT> must be a number"
-
 
85
        return
-
 
86
   
-
 
87
    align = int(sys.argv[1], 0)
-
 
88
    path = os.path.abspath(sys.argv[2])
79
    path = os.path.abspath(sys.argv[1])
89
    if (not os.path.isdir(path)):
80
    if (not os.path.isdir(path)):
90
        print "<PATH> must be a directory"
81
        print "<PATH> must be a directory"
91
        return
82
        return
92
   
83
   
93
    header_size = align_up(18, align)
-
 
94
    outf = file(sys.argv[3], "w")
84
    outf = file(sys.argv[2], "w")
95
    outf.write(struct.pack("<" + ("%d" % header_size) + "x"))
-
 
96
   
85
   
97
    outf.write(struct.pack("<5s", "TMPFS"))
86
    outf.write(struct.pack("<5s", "TMPFS"))
98
    payload_size = 5
-
 
99
   
-
 
100
    payload_size += recursion(path, outf)
87
    recursion(path, outf)
101
       
-
 
102
    outf.write(struct.pack("<BL", 0, 0))
88
    outf.write(struct.pack("<BL", 0, 0))
103
    payload_size += 5
-
 
104
   
-
 
105
    aligned_size = align_up(payload_size, align)
-
 
106
   
-
 
107
    if (aligned_size - payload_size > 0):
-
 
108
        outf.write(struct.pack("<" + ("%d" % (aligned_size - payload_size)) + "x"))
-
 
109
       
-
 
110
    outf.seek(0)
-
 
111
    outf.write(struct.pack("<4sBBLQ", "HORD", 1, 1, header_size, aligned_size))
-
 
112
    outf.close()
-
 
113
 
89
 
-
 
90
    outf.close()
-
 
91
       
114
if __name__ == '__main__':
92
if __name__ == '__main__':
115
    main()
93
    main()
116
 
94