def test_tempduppath(self): """Allocate new tempduppath, then open_with_delete""" # pr indicates file is gzipped pr = file_naming.ParseResults("inc", manifest = 1, start_time = 1, end_time = 3, compressed = 1) tdp = dup_temp.new_tempduppath(pr) assert not tdp.exists() fout = tdp.filtered_open("wb") fout.write("hello, there") fout.close() tdp.setdata() assert tdp.isreg() fin1 = gzip.GzipFile(tdp.name, "rb") buf = fin1.read() assert buf == "hello, there", buf fin1.close() fin2 = tdp.filtered_open_with_delete("rb") buf2 = fin2.read() assert buf2 == "hello, there", buf fin2.close() assert not tdp.exists()
def get_fileobj_write(self, filename, parseresults = None, sizelist = None): """ Return fileobj opened for writing, which will cause the file to be written to the backend on close(). The file will be encoded as specified in parseresults (or as read from the filename), and stored in a temp file until it can be copied over and deleted. If sizelist is not None, it should be set to an empty list. The number of bytes will be inserted into the list. """ if not parseresults: parseresults = file_naming.parse(filename) assert parseresults, "Filename %s not correctly parsed" % filename tdp = dup_temp.new_tempduppath(parseresults) def close_file_hook(): """This is called when returned fileobj is closed""" self.put(tdp, filename) if sizelist is not None: tdp.setdata() sizelist.append(tdp.getsize()) tdp.delete() fh = dup_temp.FileobjHooked(tdp.filtered_open("wb")) fh.addhook(close_file_hook) return fh
def get_fileobj_write(self, filename, parseresults=None, sizelist=None): """ Return fileobj opened for writing, which will cause the file to be written to the backend on close(). The file will be encoded as specified in parseresults (or as read from the filename), and stored in a temp file until it can be copied over and deleted. If sizelist is not None, it should be set to an empty list. The number of bytes will be inserted into the list. """ if not parseresults: parseresults = file_naming.parse(filename) assert parseresults, "Filename %s not correctly parsed" % filename tdp = dup_temp.new_tempduppath(parseresults) def close_file_hook(): """This is called when returned fileobj is closed""" self.put(tdp, filename) if sizelist is not None: tdp.setdata() sizelist.append(tdp.getsize()) tdp.delete() fh = dup_temp.FileobjHooked(tdp.filtered_open("wb")) fh.addhook(close_file_hook) return fh
def test_tempduppath(self): """Allocate new tempduppath, then open_with_delete""" # pr indicates file is gzipped pr = file_naming.ParseResults("inc", manifest=1, start_time=1, end_time=3, compressed=1) tdp = dup_temp.new_tempduppath(pr) assert not tdp.exists() fout = tdp.filtered_open("wb") fout.write("hello, there") fout.close() tdp.setdata() assert tdp.isreg() fin1 = gzip.GzipFile(tdp.name, "rb") buf = fin1.read() assert buf == "hello, there", buf fin1.close() fin2 = tdp.filtered_open_with_delete("rb") buf2 = fin2.read() assert buf2 == "hello, there", buf fin2.close() assert not tdp.exists()
def get_fileobj_read(self, filename, parseresults=None): """ Return fileobject opened for reading of filename on backend The file will be downloaded first into a temp file. When the returned fileobj is closed, the temp file will be deleted. """ if not parseresults: parseresults = file_naming.parse(filename) assert parseresults, "Filename not correctly parsed" tdp = dup_temp.new_tempduppath(parseresults) self.get(filename, tdp) tdp.setdata() return tdp.filtered_open_with_delete("rb")
def restore_get_enc_fileobj(backend, filename, volume_info): """Return plaintext fileobj from encrypted filename on backend """ parseresults = file_naming.parse(filename) if filename in filename_tdp: tdp = filename_tdp[filename] else: tdp = dup_temp.new_tempduppath(parseresults) filename_tdp[filename] = tdp backend.get(filename, tdp) if not restore_check_hash(volume_info, tdp): return None fileobj = tdp.filtered_open_with_delete("rb") if parseresults.encrypted and globals.gpg_profile.sign_key: restore_add_sig_check(fileobj) return fileobj