66#include <cryptutils.h>
77#include <walk.h>
88
9- void walk_and_encrypt (const char * path , const unsigned char * key ) {
9+ // Helper to remove ".enc" suffix for decryption
10+ static void remove_enc_extension (const char * filename , char * output , size_t size ) {
11+ size_t len = strlen (filename );
12+ if (len > 4 && strcmp (filename + len - 4 , ".enc" ) == 0 ) {
13+ snprintf (output , size , "%.*s" , (int )(len - 4 ), filename );
14+ } else {
15+ snprintf (output , size , "%s" , filename );
16+ }
17+ }
18+
19+ // Process each directory and apply encryption or decryption
20+ static void process_directory (const char * path , const unsigned char * key , int encrypt_mode ) {
1021 DIR * dir = opendir (path );
1122 if (!dir ) {
1223 perror ("opendir failed" );
@@ -28,47 +39,33 @@ void walk_and_encrypt(const char *path, const unsigned char *key) {
2839 }
2940
3041 if (S_ISDIR (st .st_mode )) {
31- walk_and_encrypt (full_path , key );
42+ process_directory (full_path , key , encrypt_mode );
3243 } else if (S_ISREG (st .st_mode )) {
33- // Create output path: append ".enc" to filename
3444 char output_path [1024 ];
35- snprintf (output_path , sizeof (output_path ), "%s.enc" , full_path );
3645
37- printf ("Encrypting: %s -> %s\n" , full_path , output_path );
38- encrypt_file (full_path , output_path , key );
46+ if (encrypt_mode ) {
47+ // Encryption: Append ".enc" extension
48+ snprintf (output_path , sizeof (output_path ), "%s.enc" , full_path );
49+ printf ("Encrypting: %s -> %s\n" , full_path , output_path );
50+ encrypt_file (full_path , output_path , key );
51+ } else {
52+ // Decryption: Remove ".enc" extension
53+ remove_enc_extension (full_path , output_path , sizeof (output_path ));
54+ printf ("Decrypting: %s -> %s\n" , full_path , output_path );
55+ decrypt_file (full_path , output_path , key );
56+ }
3957 }
4058 }
4159
4260 closedir (dir );
4361}
4462
45- void walk_and_decrypt (const char * path , const unsigned char * key ) {
46- DIR * dir = opendir (path );
47- if (!dir ) {
48- perror ("opendir failed" );
49- return ;
50- }
51-
52- struct dirent * entry ;
53- while ((entry = readdir (dir ))) {
54- if (strcmp (entry -> d_name , "." ) == 0 || strcmp (entry -> d_name , ".." ) == 0 )
55- continue ;
56-
57- char full_path [1024 ];
58- snprintf (full_path , sizeof (full_path ), "%s/%s" , path , entry -> d_name );
59-
60- struct stat st ;
61- if (stat (full_path , & st ) == -1 ) {
62- perror ("stat failed" );
63- continue ;
64- }
65-
66- if (S_ISDIR (st .st_mode )) {
67- walk_and_encrypt (full_path , key );
68- } else if (S_ISREG (st .st_mode )) {
69- decrypt_file (full_path , full_path , key );
70- }
71- }
63+ // Encrypt all files in the given directory
64+ void walk_and_encrypt (const char * path , const unsigned char * key ) {
65+ process_directory (path , key , 1 );
66+ }
7267
73- closedir (dir );
74- }
68+ // Decrypt all files in the given directory
69+ void walk_and_decrypt (const char * path , const unsigned char * key ) {
70+ process_directory (path , key , 0 );
71+ }
0 commit comments