Skip to content

Commit

Permalink
move while decrypting
Browse files Browse the repository at this point in the history
- decrypt command support (re)moving files and directories just like encrypt
- if specified vault doesn't exist, terminates immediately
  • Loading branch information
maxpat78 committed Oct 17, 2024
1 parent a1dbfde commit c6f03dc
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pycryptomator/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
COPYRIGHT = '''Copyright (C)2024, by maxpat78.'''
__version__ = '1.4'
__version__ = '1.5'
__all__ = ["Vault", "init_vault", "backupDirIds"]
4 changes: 4 additions & 0 deletions pycryptomator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
init_vault(args.vault_name, args.password)
sys.exit(0)

if not exists(args.vault_name):
print('Specified vault does not exist:', args.vault_name)
sys.exit(1)

if not args.password and not args.master_keys:
args.password = getpass.getpass()

Expand Down
8 changes: 5 additions & 3 deletions pycryptomator/cmshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,19 @@ def do_backup(p, arg):
def do_decrypt(p, arg):
'Decrypt files or directories from the vault'
argl = split(arg)
move = '-m' in argl
if move: argl.remove('-m')
force = '-f' in argl
if force: argl.remove('-f')
if not argl or argl[0] == '-h' or len(argl) != 2:
print('use: decrypt [-f] <virtual_pathname_source> <real_pathname_destination>')
print('use: decrypt [-m] [-f] <virtual_pathname_source> <real_pathname_destination>')
print('use: decrypt <virtual_pathname_source> -')
return
try:
is_dir = p.vault.getInfo(argl[0]).isDir
if is_dir: p.vault.decryptDir(argl[0], argl[1], force)
if is_dir: p.vault.decryptDir(argl[0], argl[1], force, move)
else:
p.vault.decryptFile(argl[0], argl[1], force)
p.vault.decryptFile(argl[0], argl[1], force, move)
if argl[1] == '-': print()
except:
print(sys.exception())
Expand Down
11 changes: 8 additions & 3 deletions pycryptomator/cryptomator.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def _decryptf(K, src, dst):
dst.write(ds)
n += 1

def decryptFile(p, virtualpath, dest, force=False):
def decryptFile(p, virtualpath, dest, force=False, move=False):
"Decrypt a file from a virtual pathname and puts it in 'dest' (a real pathname or file-like object)"
info = p.getInfo(virtualpath)
while info.pointsTo:
Expand All @@ -361,9 +361,11 @@ def decryptFile(p, virtualpath, dest, force=False):
if dest != '-' and not hasattr(dest, 'write'):
# restore original last access and modification time
os.utime(dest, (st.st_atime, st.st_mtime))
if move:
p.remove(info.pathname)
return st.st_size

def decryptDir(p, virtualpath, dest, force=False):
def decryptDir(p, virtualpath, dest, force=False, move=False):
if (virtualpath[0] != '/'):
raise BaseException('the vault path to decrypt must be absolute!')
x = p.getInfo(virtualpath)
Expand All @@ -382,8 +384,11 @@ def decryptDir(p, virtualpath, dest, force=False):
if not exists(bn):
os.makedirs(bn)
print(dn)
total_bytes += p.decryptFile(fn, dn, force)
total_bytes += p.decryptFile(fn, dn, force, move)
n += 1
if move:
print('moved', virtualpath)
p.rmtree(virtualpath)
T1 = time.time()
print('decrypting %s bytes in %d files and %d directories took %d seconds' % (_fmt_size(total_bytes), n, nn, T1-T0))

Expand Down

0 comments on commit c6f03dc

Please sign in to comment.