Last active
January 12, 2024 10:41
-
-
Save monperrus/7a114ccc1c2fd20dcc5e852cdf813c5d to your computer and use it in GitHub Desktop.
ecryptfs-map: maps a decrypted filename to the ecrypfs-encrypted counterpart (and vice versa) and outputs the filename to the console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# maps a decrypted filename to the ecrypfs-encrypted counterpart (and vice versa) | |
# and outputs the filename to the console | |
# | |
# Notes: | |
# much faster than ecryptfs-find | |
# because contrary to ecryptfs-find, does not iterate over the whole filesystem tree with find | |
# url for latest version: https://gist.github.com/monperrus/7a114ccc1c2fd20dcc5e852cdf813c5d | |
# author: Martin Monperrus | |
# license: public domain | |
import os | |
import re | |
import sys | |
if len(sys.argv)<2: | |
print("usage: ecryptfs-map <decrypted filename>") | |
sys.exit(-1) | |
HOME=os.environ["HOME"] | |
if not HOME.endswith("/"): HOME+="/" | |
os.chdir(HOME) | |
if os.path.exists(sys.argv[1]): | |
## case DECRYPTED -> ENCRYPTED | |
assert HOME[-1] == "/" | |
mapped_pathelem = HOME[0:-1]+"/.Private" | |
path_elements = os.path.abspath(sys.argv[1]).replace(HOME,'').split(os.sep) | |
for index in range(0,len(path_elements)): | |
pathelem = path_elements[index] | |
#print(pathelem) | |
if pathelem == ".Private": continue | |
#print(path_elements[0:index+1], mapped_pathelem) | |
inode = os.stat(os.sep.join(path_elements[0:index+1])).st_ino | |
for i in os.scandir(mapped_pathelem): | |
if i.inode() == inode and i.name != pathelem: | |
# found corresponding path elem | |
mapped_pathelem += os.sep+ i.name | |
#print(mapped_pathelem) | |
break | |
if os.path.exists(".Private/"+sys.argv[1]): | |
# case "ENCRYPTED -> DECRYPTED" | |
mapped_pathelem = HOME[0:-1] | |
path_elements = (".Private/"+sys.argv[1]).split(os.sep) | |
for index in range(0,len(path_elements)): | |
pathelem = path_elements[index] | |
if pathelem == ".Private": continue | |
#print(path_elements[0:index+1], mapped_pathelem) | |
inode = os.stat(os.sep.join(path_elements[0:index+1])).st_ino | |
#print(pathelem,inode) | |
for i in os.scandir(mapped_pathelem): | |
if i.inode() == inode and i.name != pathelem: | |
# found corresponding path elem | |
mapped_pathelem += os.sep+ i.name | |
#print(mapped_pathelem) | |
break | |
print(mapped_pathelem) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment