Skip to content

Commit

Permalink
Added an option to (re)move encrypted files
Browse files Browse the repository at this point in the history
  • Loading branch information
maxpat78 committed Oct 15, 2024
1 parent db73cb0 commit 5a233db
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 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.1'
__version__ = '1.2'
__all__ = ["Vault", "init_vault", "backupDirIds"]
12 changes: 7 additions & 5 deletions pycryptomator/cmshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .cryptomator import *

if os.name == 'nt':
from w32lex import split # shlex ban \ in pathnames!
from .w32lex import split # shlex ban \ in pathnames!
else:
from shlex import split

Expand Down Expand Up @@ -62,16 +62,18 @@ def do_decrypt(p, arg):
print(sys.exception())

def do_encrypt(p, arg):
'Encrypt files or directories into the vault'
'Encrypt files or directories into the vault, eventually moving them'
argl = split(arg)
move = '-m' in argl
if move: argl.remove('-m')
if not argl or argl[0] == '-h' or len(argl) != 2:
print('use: encrypt <real_pathname_source> <virtual_pathname_destination>')
print('use: encrypt [-m] <real_pathname_source> <virtual_pathname_destination>')
return
try:
if isdir(argl[0]):
p.vault.encryptDir(argl[0], argl[1])
p.vault.encryptDir(argl[0], argl[1], move=move)
else:
p.vault.encryptFile(argl[0], argl[1])
p.vault.encryptFile(argl[0], argl[1], move=move)
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 @@ -265,7 +265,7 @@ def _encryptf(K, src, dst):
dst.write(tag)
n += 1

def encryptFile(p, src, virtualpath, force=False):
def encryptFile(p, src, virtualpath, force=False, move=False):
"Encrypt a 'src' file into a pre-existant vault's virtual directory (or a file-like object into a real path)"
if hasattr(src, 'read'): # if it's file
f = src
Expand All @@ -285,9 +285,11 @@ def encryptFile(p, src, virtualpath, force=False):
if not hasattr(src, 'read'):
st = os.stat(src)
os.utime(out.name, (st.st_atime, st.st_mtime))
if move:
os.remove(src)
return cb

def encryptDir(p, src, virtualpath, force=False):
def encryptDir(p, src, virtualpath, force=False, move=False):
if (virtualpath[0] != '/'):
raise BaseException('the vault path must be absolute!')
real = p.mkdir(virtualpath)
Expand All @@ -302,8 +304,11 @@ def encryptDir(p, src, virtualpath, force=False):
dn = join(virtualpath, fn[len(src)+1:]) # target pathname
p.mkdir(dirname(dn))
print(dn)
total_bytes += p.encryptFile(fn, dn, force)
total_bytes += p.encryptFile(fn, dn, force, move)
n += 1
if move:
print('moved', src)
shutil.rmtree(src)
T1 = time.time()
print('encrypting %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 5a233db

Please sign in to comment.