11#!/usr/bin/env python3
22
33"""
4- Cockpit Navigator - A File System Browser for Cockpit.
5- Copyright (C) 2021 Josh Boudreau <jboudreau@45drives.com>
4+ Cockpit Navigator - A File System Browser for Cockpit.
5+ Copyright (C) 2021 Josh Boudreau <jboudreau@45drives.com>
66
7- This file is part of Cockpit Navigator.
8- Cockpit Navigator is free software: you can redistribute it and/or modify
9- it under the terms of the GNU General Public License as published by
10- the Free Software Foundation, either version 3 of the License, or
11- (at your option) any later version.
12- Cockpit Navigator is distributed in the hope that it will be useful,
13- but WITHOUT ANY WARRANTY; without even the implied warranty of
14- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15- GNU General Public License for more details.
16- You should have received a copy of the GNU General Public License
17- along with Cockpit Navigator. If not, see <https://www.gnu.org/licenses/>.
7+ This file is part of Cockpit Navigator.
8+ Cockpit Navigator is free software: you can redistribute it and/or modify
9+ it under the terms of the GNU General Public License as published by
10+ the Free Software Foundation, either version 3 of the License, or
11+ (at your option) any later version.
12+ Cockpit Navigator is distributed in the hope that it will be useful,
13+ but WITHOUT ANY WARRANTY; without even the implied warranty of
14+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+ GNU General Public License for more details.
16+ You should have received a copy of the GNU General Public License
17+ along with Cockpit Navigator. If not, see <https://www.gnu.org/licenses/>.
1818"""
1919
2020"""
2121Synopsis: `echo <newline delimited JSON objects> | write-chunks.py3`
2222JSON objects are of form:
2323obj = {
24- seek: <byte offset>
25- chunk: <base64 encoded data chunk>
24+ seek: <byte offset>
25+ chunk: <base64 encoded data chunk>
2626}
2727"""
2828
@@ -31,53 +31,48 @@ import os
3131import sys
3232import json
3333
34- def write_chunk (chunk , file ):
35- if not file :
36- path = sys .argv [1 ]
37- parent_path = os .path .dirname (path )
38- try :
39- if not os .path .exists (parent_path ):
40- os .makedirs (parent_path , exist_ok = True )
41- elif os .path .isfile (parent_path ):
42- print (parent_path + ": exists and is not a directory." )
43- sys .exit (1 )
44- file = open (path , "wb" )
45- except Exception as e :
46- print (e )
47- sys .exit (1 )
48- seek = chunk ["seek" ]
49- data = base64 .b64decode (chunk ["chunk" ])
50- try :
51- file .seek (seek )
52- file .write (data )
53- except Exception as e :
54- print (e )
55- sys .exit (1 )
34+ def write_chunk (chunk , fd ):
35+ seek = chunk ["seek" ]
36+ data = base64 .b64decode (chunk ["chunk" ])
37+ os .lseek (fd , seek , os .SEEK_SET )
38+ os .write (fd , data )
39+
40+ def create_path (path ):
41+ try :
42+ if not os .path .exists (path ):
43+ os .makedirs (path , exist_ok = True )
44+ elif os .path .isfile (path ):
45+ print (path + ": exists and is not a directory." )
46+ sys .exit (1 )
47+ except Exception as e :
48+ print (e )
49+ sys .exit (1 )
5650
5751def main ():
58- file = None
59- if len (sys .argv ) != 2 :
60- print ("Invalid number of arguments." )
61- sys .exit (1 )
62- while True :
63- try :
64- json_in = input ()
65- except EOFError :
66- break
67- json_list = json_in .split ("\n " ) # need to split in case writes happen faster than reads
68- for json_obj in json_list :
69- try :
70- obj_in = json .loads (json_obj )
71- except Exception as e :
72- print (e )
73- log = open ("/var/log/navigator.log" , "w" )
74- log .write (json_in )
75- log .close ()
76- sys .exit (1 )
77- write_chunk (obj_in , file )
78- if file :
79- file .close ()
80- sys .exit (0 )
52+ if len (sys .argv ) != 2 :
53+ print ("Invalid number of arguments." )
54+ sys .exit (1 )
55+ fd = None
56+ path = sys .argv [1 ]
57+ parent_path = os .path .dirname (path )
58+ create_path (parent_path )
59+ try :
60+ fd = os .open (sys .argv [1 ], os .O_WRONLY | os .O_TRUNC | os .O_CREAT )
61+ while True :
62+ try :
63+ json_in = input ()
64+ except EOFError :
65+ break
66+ json_list = json_in .split ("\n " ) # need to split in case writes happen faster than reads
67+ for json_obj in json_list :
68+ obj_in = json .loads (json_obj )
69+ write_chunk (obj_in , fd )
70+ except Exception as e :
71+ print (e )
72+ os .close (fd )
73+ sys .exit (1 )
74+ os .close (fd )
75+ sys .exit (0 )
8176
8277if __name__ == "__main__" :
83- main ()
78+ main ()
0 commit comments