mirror of
https://github.com/45Drives/cockpit-navigator.git
synced 2025-07-29 16:45:13 +02:00
use os.open and os.write instead of regular file
This commit is contained in:
parent
330ca90008
commit
ee8c110203
@ -1,28 +1,28 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Cockpit Navigator - A File System Browser for Cockpit.
|
Cockpit Navigator - A File System Browser for Cockpit.
|
||||||
Copyright (C) 2021 Josh Boudreau <jboudreau@45drives.com>
|
Copyright (C) 2021 Josh Boudreau <jboudreau@45drives.com>
|
||||||
|
|
||||||
This file is part of Cockpit Navigator.
|
This file is part of Cockpit Navigator.
|
||||||
Cockpit Navigator is free software: you can redistribute it and/or modify
|
Cockpit Navigator is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
(at your option) any later version.
|
(at your option) any later version.
|
||||||
Cockpit Navigator is distributed in the hope that it will be useful,
|
Cockpit Navigator is distributed in the hope that it will be useful,
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
GNU General Public License for more details.
|
GNU General Public License for more details.
|
||||||
You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
||||||
along with Cockpit Navigator. If not, see <https://www.gnu.org/licenses/>.
|
along with Cockpit Navigator. If not, see <https://www.gnu.org/licenses/>.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Synopsis: `echo <newline delimited JSON objects> | write-chunks.py3`
|
Synopsis: `echo <newline delimited JSON objects> | write-chunks.py3`
|
||||||
JSON objects are of form:
|
JSON objects are of form:
|
||||||
obj = {
|
obj = {
|
||||||
seek: <byte offset>
|
seek: <byte offset>
|
||||||
chunk: <base64 encoded data chunk>
|
chunk: <base64 encoded data chunk>
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -31,53 +31,48 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
|
|
||||||
def write_chunk(chunk, file):
|
def write_chunk(chunk, fd):
|
||||||
if not file:
|
seek = chunk["seek"]
|
||||||
path = sys.argv[1]
|
data = base64.b64decode(chunk["chunk"])
|
||||||
parent_path = os.path.dirname(path)
|
os.lseek(fd, seek, os.SEEK_SET)
|
||||||
try:
|
os.write(fd, data)
|
||||||
if not os.path.exists(parent_path):
|
|
||||||
os.makedirs(parent_path, exist_ok=True)
|
def create_path(path):
|
||||||
elif os.path.isfile(parent_path):
|
try:
|
||||||
print(parent_path + ": exists and is not a directory.")
|
if not os.path.exists(path):
|
||||||
sys.exit(1)
|
os.makedirs(path, exist_ok=True)
|
||||||
file = open(path, "wb")
|
elif os.path.isfile(path):
|
||||||
except Exception as e:
|
print(path + ": exists and is not a directory.")
|
||||||
print(e)
|
sys.exit(1)
|
||||||
sys.exit(1)
|
except Exception as e:
|
||||||
seek = chunk["seek"]
|
print(e)
|
||||||
data = base64.b64decode(chunk["chunk"])
|
sys.exit(1)
|
||||||
try:
|
|
||||||
file.seek(seek)
|
|
||||||
file.write(data)
|
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
file = None
|
if len(sys.argv) != 2:
|
||||||
if len(sys.argv) != 2:
|
print("Invalid number of arguments.")
|
||||||
print("Invalid number of arguments.")
|
sys.exit(1)
|
||||||
sys.exit(1)
|
fd = None
|
||||||
while True:
|
path = sys.argv[1]
|
||||||
try:
|
parent_path = os.path.dirname(path)
|
||||||
json_in = input()
|
create_path(parent_path)
|
||||||
except EOFError:
|
try:
|
||||||
break
|
fd = os.open(sys.argv[1], os.O_WRONLY | os.O_TRUNC | os.O_CREAT)
|
||||||
json_list = json_in.split("\n") # need to split in case writes happen faster than reads
|
while True:
|
||||||
for json_obj in json_list:
|
try:
|
||||||
try:
|
json_in = input()
|
||||||
obj_in = json.loads(json_obj)
|
except EOFError:
|
||||||
except Exception as e:
|
break
|
||||||
print(e)
|
json_list = json_in.split("\n") # need to split in case writes happen faster than reads
|
||||||
log = open("/var/log/navigator.log", "w")
|
for json_obj in json_list:
|
||||||
log.write(json_in)
|
obj_in = json.loads(json_obj)
|
||||||
log.close()
|
write_chunk(obj_in, fd)
|
||||||
sys.exit(1)
|
except Exception as e:
|
||||||
write_chunk(obj_in, file)
|
print(e)
|
||||||
if file:
|
os.close(fd)
|
||||||
file.close()
|
sys.exit(1)
|
||||||
sys.exit(0)
|
os.close(fd)
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user