720.2.89
by Martin Sucha
Add support for ext2 to be used as RAM disk format |
1 |
#!/usr/bin/env python
|
2 |
#
|
|
3 |
# Copyright (c) 2008 Martin Decky
|
|
4 |
# Copyright (c) 2011 Martin Sucha
|
|
5 |
# All rights reserved.
|
|
6 |
#
|
|
7 |
# Redistribution and use in source and binary forms, with or without
|
|
8 |
# modification, are permitted provided that the following conditions
|
|
9 |
# are met:
|
|
10 |
#
|
|
11 |
# - Redistributions of source code must retain the above copyright
|
|
12 |
# notice, this list of conditions and the following disclaimer.
|
|
13 |
# - Redistributions in binary form must reproduce the above copyright
|
|
14 |
# notice, this list of conditions and the following disclaimer in the
|
|
15 |
# documentation and/or other materials provided with the distribution.
|
|
16 |
# - The name of the author may not be used to endorse or promote products
|
|
17 |
# derived from this software without specific prior written permission.
|
|
18 |
#
|
|
19 |
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
20 |
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
21 |
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
22 |
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
23 |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
24 |
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
25 |
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
26 |
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
27 |
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
28 |
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
29 |
#
|
|
30 |
||
31 |
"""
|
|
32 |
Utilities for filesystem image creators
|
|
33 |
"""
|
|
34 |
||
35 |
import os |
|
36 |
import stat |
|
37 |
||
38 |
exclude_names = set(['.svn', '.bzr']) |
|
39 |
||
40 |
def align_up(size, alignment): |
|
41 |
"Return size aligned up to alignment"
|
|
42 |
||
43 |
if (size % alignment == 0): |
|
44 |
return size |
|
45 |
||
46 |
return ((size // alignment) + 1) * alignment |
|
47 |
||
48 |
def count_up(size, alignment): |
|
49 |
"Return units necessary to fit the size"
|
|
50 |
||
51 |
if (size % alignment == 0): |
|
52 |
return (size // alignment) |
|
53 |
||
54 |
return ((size // alignment) + 1) |
|
55 |
||
56 |
def num_of_trailing_bin_zeros(num): |
|
57 |
"Return number of trailing zeros in binary representation"
|
|
58 |
||
59 |
i = 0 |
|
60 |
if (num == 0): raise ValueError() |
|
61 |
while num & 1 == 0: |
|
62 |
i += 1 |
|
63 |
num = num >> 1 |
|
64 |
return i |
|
65 |
||
66 |
def get_bit(number, n): |
|
67 |
"Return True if n-th least-significant bit is set in the given number"
|
|
68 |
||
69 |
return bool((number >> n) & 1) |
|
70 |
||
71 |
def set_bit(number, n): |
|
72 |
"Return the number with n-th least-significant bit set"
|
|
73 |
||
74 |
return number | (1 << n) |
|
75 |
||
76 |
class ItemToPack: |
|
77 |
"Stores information about one directory item to be added to the image"
|
|
78 |
||
79 |
def __init__(self, parent, name): |
|
80 |
self.parent = parent |
|
81 |
self.name = name |
|
82 |
self.path = os.path.join(parent, name) |
|
83 |
self.stat = os.stat(self.path) |
|
84 |
self.is_dir = stat.S_ISDIR(self.stat.st_mode) |
|
85 |
self.is_file = stat.S_ISREG(self.stat.st_mode) |
|
86 |
self.size = self.stat.st_size |
|
87 |
||
88 |
def listdir_items(path): |
|
89 |
"Return a list of items to be packed inside a fs image"
|
|
90 |
||
91 |
for name in os.listdir(path): |
|
92 |
if name in exclude_names: |
|
93 |
continue
|
|
1460.1.1
by Martin Decky
cstyle |
94 |
|
720.2.89
by Martin Sucha
Add support for ext2 to be used as RAM disk format |
95 |
item = ItemToPack(path, name) |
1460.1.1
by Martin Decky
cstyle |
96 |
|
720.2.89
by Martin Sucha
Add support for ext2 to be used as RAM disk format |
97 |
if not (item.is_dir or item.is_file): |
98 |
continue
|
|
1460.1.1
by Martin Decky
cstyle |
99 |
|
720.2.89
by Martin Sucha
Add support for ext2 to be used as RAM disk format |
100 |
yield item |
101 |
||
102 |
def chunks(item, chunk_size): |
|
103 |
"Iterate contents of a file in chunks of a given size"
|
|
104 |
||
105 |
inf = open(item.path, 'rb') |
|
106 |
rd = 0 |
|
1460.1.1
by Martin Decky
cstyle |
107 |
|
720.2.89
by Martin Sucha
Add support for ext2 to be used as RAM disk format |
108 |
while (rd < item.size): |
109 |
data = bytes(inf.read(chunk_size)) |
|
110 |
yield data |
|
111 |
rd += len(data) |
|
1460.1.1
by Martin Decky
cstyle |
112 |
|
720.2.89
by Martin Sucha
Add support for ext2 to be used as RAM disk format |
113 |
inf.close() |