Commit 6425c3bf authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Use tar_stream/tarfile instead of star in primary.py

parent eac7e7fc
......@@ -12,6 +12,7 @@ import sys
import threading
import time
import uuid
import tar_stream
def cond_unlink(path, log):
try:
......@@ -48,9 +49,9 @@ class Server:
self.uuid = uuid
self.socket_path = '/tmp/%s_server' % (self.uuid)
self.mutex = threading.Lock()
self.failed = None
self.failed = True
self.thread_hash = None
self.thread_star = None
self.thread_tar = None
self.thread_server = threading.Thread(daemon=True, target=self.run)
self.thread_server.start()
......@@ -70,20 +71,20 @@ class Server:
args=(config_HASH_socket,))
self.thread_hash.start()
star_socket,_ = server.accept()
self.log.DEBUG('CPIO', star_socket)
tar_socket,_ = server.accept()
self.log.DEBUG('TAR', tar_socket)
with self.mutex:
self.thread_star = threading.Thread(daemon=True,
target=self.run_star,
args=(star_socket,))
self.thread_star.start()
self.thread_tar = threading.Thread(daemon=True,
target=self.run_tar,
args=(tar_socket,))
self.thread_tar.start()
cond_unlink(self.socket_path, self.log)
def pending(self):
with self.mutex:
return ((self.thread_hash and self.thread_hash.thread.is_alive()) or
(self.thread_star and self.thread_star.thread.is_alive()) or
(self.thread_tar and self.thread_tar.thread.is_alive()) or
(self.thread_server.is_alive()))
def send_HASH(self, config_HASH):
......@@ -106,30 +107,41 @@ class Server:
config_HASH.shutdown(socket.SHUT_RDWR)
config_HASH.close()
def run_star(self, star_socket):
self.log.DEBUG('START run_star',
def run_tar(self, tar_socket):
self.log.DEBUG('START run_tar',
self.config.primary.mount.path,
self.path)
cmd = [ '/bin/star', '-c', '-acl', '-Hexustar', '-dump',
'-read0', '-list=-', '-no-statistics' ]
cwd = os.path.join(self.config.primary.mount.path, self.path)
stdin = star_socket.makefile('rb')
stdout = star_socket.makefile('wb')
stdin = tar_socket.makefile('rb')
stdout = tar_socket.makefile('wb')
try:
subprocess.check_call(cmd, cwd=cwd,
stdin=stdin,
stdout=stdout,
stderr=self.log.makefile(encoding='utf-8'))
self.log.DEBUG('OK run_star', cmd, cwd)
except Exception as e:
with self.mutex:
self.failed = True
def readlines_nul(f):
buf = b''
while True:
pos = buf.find(b'\x00')
if pos == -1:
tmp = f.read(4096)
if len(tmp) == 0:
if len(buf) == 0:
return
print(len(buf), 'EOF', file=sys.stderr)
raise EOFError()
buf += tmp
continue
yield buf[0:pos]
buf = buf[pos+1:]
pass
pass
pass
writer = tar_stream.TarWriter(stdout)
for path in readlines_nul(stdin):
arcname = path.decode('utf8', errors='surrogateescape')
writer.add(os.path.join(cwd, arcname), arcname=arcname)
writer.close()
self.failed = False
finally:
self.log.MESSAGE('shutting down star_socket')
star_socket.shutdown(socket.SHUT_RDWR)
self.log.DEBUG('DONE run_star', cmd, cwd)
tar_socket.shutdown(socket.SHUT_RDWR)
pass
class Client:
......@@ -228,7 +240,10 @@ def do_backup(hash_name, options, config):
log=log) for e in b.entry ]
while any([ c.pending() for c in client ]):
time.sleep(1)
pass
if any([c.failed for c in client]):
raise Exception("Backup failed")
pass
pass
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment