-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_diskfs.py
executable file
·64 lines (50 loc) · 1.89 KB
/
create_diskfs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# This file is part of asmc, a bootstrapping OS with minimal seed
# Copyright (C) 2018 Giovanni Mascellani <[email protected]>
# https://gitlab.com/giomasce/asmc
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import sys
import os
import struct
import shutil
def main():
args = sys.stdin.read().split(None)
assert len(args) % 2 == 0
file_num = (len(args)) // 2
table_len = 0
files_len = 0
names = []
sizes = []
starts = []
for i in range(file_num):
name = args[2*i].encode('ascii')
data_filename = args[2*i+1]
data_size = os.stat(data_filename).st_size
names.append(name)
sizes.append(data_size)
starts.append(files_len)
table_len += 9 + len(name)
files_len += data_size
sys.stdout.buffer.write(b'DISKFS ')
# 8 bytes for DISKFS and one another for the final null
table_len += 9
for i in range(file_num):
sys.stdout.buffer.write(names[i])
sys.stdout.buffer.write(b'\0')
sys.stdout.buffer.write(struct.pack('!II', starts[i] + table_len, sizes[i]))
sys.stdout.buffer.write(b'\0')
for i in range(file_num):
with open(args[2*i+1], 'rb') as fin:
shutil.copyfileobj(fin, sys.stdout.buffer)
if __name__ == '__main__':
main()