Skip to content

Commit 8999131

Browse files
feat: add --restore-dest
1 parent 23f57d4 commit 8999131

File tree

3 files changed

+80
-14
lines changed

3 files changed

+80
-14
lines changed

Diff for: README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Trashbhuwan
22

33
## Introduction:
4-
A simple CLI Application for trashing & restoring files & its written in pure C. It uses the default trashcan of KDE, GNOME, XFCE, Cinnamon, MATE, and others. `trashbhuwan` remembers the original path of the trashed files & directories, so you can restore them later where they were. It is minimalistic and easy to use.
4+
A simple CLI Application for trashing & restoring files & its written in pure C. It uses the default trashcan of every Linux distribution. `trashbhuwan` remembers the original path of the trashed files & directories, so you can restore them later where they were. It is created with developer productivity in mind.
55

66
Available options:
77

@@ -10,9 +10,10 @@ Available options:
1010
trashbhuwan --list : List all trashed files & directories with disk usage
1111
trashbhuwan --put [file/directory] : Put files & directories in trash
1212
trashbhuwan --restore [file/directory] : Restore trashed file or directory
13+
trashbhuwan --restore-dest [file/directory] : Restore trashed file or directory to specified destination
1314
trashbhuwan --delete [file/directory] : Delete trashed file or directory permanently
1415

15-
Pass multiple files or directories separating by double quotes or single quotes.
16+
Pass multiple files separating by double quotes or single quotes.
1617

1718
## Installation:
1819

Diff for: trashbhuwan

48 Bytes
Binary file not shown.

Diff for: trashbhuwan.c

+77-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//<------------------------------CLI Application for managing TRASH wrtitten in 'C'------------------------------------>
22
// author : @tribhuwan-kumar
3-
// email : freakybytes@duck.com
3+
// email : trashbhuwan@duck.com
44
//<-------------------------------------------------------------------------------------------------------------------->
55
#include <time.h>
66
#include <glob.h>
@@ -285,7 +285,7 @@ void emptyTrash(const char *trash_files_dir, const char *trash_info_dir) {
285285
void createTrashInfo(const char *filePath) {
286286
char *filePathCopy = strdup(filePath);
287287
const char* username = getUserName();
288-
char* trashInfoDir = getPath("/home/", "/.local/share/Trash/info");
288+
const char* trashInfoDir = getPath("/home/", "/.local/share/Trash/info");
289289

290290
if (filePathCopy == NULL || username == NULL || trashInfoDir == NULL) {
291291
perror("Something went wrong!");
@@ -349,8 +349,8 @@ void restoreTrashedfile(const char *fileName) {
349349
char originalDirectory[PATH_MAX];
350350
struct stat st = {0};
351351

352-
char* trashFilesDir = getPath("/home/", "/.local/share/Trash/files");
353-
char* trashInfoDir = getPath("/home/", "/.local/share/Trash/info");
352+
const char* trashFilesDir = getPath("/home/", "/.local/share/Trash/files");
353+
const char* trashInfoDir = getPath("/home/", "/.local/share/Trash/info");
354354

355355
snprintf(filePath, sizeof(filePath), "%s/%s", trashFilesDir, fileName);
356356
if (access(filePath, F_OK) != -1) {
@@ -408,14 +408,61 @@ void restoreTrashedfile(const char *fileName) {
408408
}
409409
}
410410
else {
411-
fprintf(stderr, "Couldn't find '%s' in trash\n", fileName);
411+
fprintf(stderr, "Couldn't find '%s' in trash!!\n", fileName);
412+
}
413+
}
414+
415+
// RESTORE FILE ON DIFF DESTINATION
416+
void restoreTrashedfileOnDest(const char *fileName, const char *destPath) {
417+
char filePath[PATH_MAX];
418+
char infoFilePath[PATH_MAX];
419+
char fileDestPath[PATH_MAX];
420+
421+
const char* trashFilesDir = getPath("/home/", "/.local/share/Trash/files");
422+
const char* trashInfoDir = getPath("/home/", "/.local/share/Trash/info");
423+
424+
snprintf(filePath, sizeof(filePath), "%s/%s", trashFilesDir, fileName);
425+
snprintf(infoFilePath, sizeof(infoFilePath), "%s/%s.trashinfo", trashInfoDir, fileName);
426+
427+
if (access(filePath, F_OK) != -1) {
428+
if (access(destPath, F_OK) != -1) {
429+
snprintf(fileDestPath, sizeof(fileDestPath), "%s/%s", destPath, fileName);
430+
if (strncmp(fileDestPath, "/mnt", 4) == 0) {
431+
char restoreFileCommand[PATH_MAX];
432+
snprintf(restoreFileCommand, sizeof(restoreFileCommand), "mv \"%s\" \"%s\"", filePath, fileDestPath);
433+
if (system(restoreFileCommand) == 0) {
434+
if (access(infoFilePath, F_OK) == 0) {
435+
remove(infoFilePath);
436+
}
437+
}
438+
else {
439+
fprintf(stderr,"Failed to restore '%s'\n", fileName);
440+
}
441+
}
442+
if (strncmp(fileDestPath, "/mnt", 4) != 0) {
443+
if (rename(filePath, fileDestPath) == 0) {
444+
if (access(infoFilePath, F_OK) == 0) {
445+
remove(infoFilePath);
446+
}
447+
}
448+
else {
449+
fprintf(stderr,"Failed to restore '%s'\n", fileName);
450+
}
451+
}
452+
}
453+
else {
454+
fprintf(stderr, "Not a valid destination '%s'\n", destPath);
455+
}
456+
}
457+
else {
458+
fprintf(stderr, "Couldn't find '%s' in trash!!\n", fileName);
412459
}
413460
}
414461

415462
// INDIVIDUALLY DELETE TRASHED FILE
416463
void deleteTrashedFile(const char *fileName){
417-
char* trashFilesDir = getPath("/home/", "/.local/share/Trash/files");
418-
char* trashInfoDir = getPath("/home/", "/.local/share/Trash/info");
464+
const char* trashFilesDir = getPath("/home/", "/.local/share/Trash/files");
465+
const char* trashInfoDir = getPath("/home/", "/.local/share/Trash/info");
419466

420467
char trashedFilePath[PATH_MAX];
421468
char trashInfoFilePath[PATH_MAX];
@@ -585,6 +632,23 @@ int main(int argc, char *argv[]) {
585632
restoreTrashedfile(fileNames);
586633
}
587634
}
635+
636+
// RESTORE IN SPECIFIED DESTINATION
637+
else if (argc >= 2 && (strcmp(argv[1], "--restore-dest") == 0 || strcmp(argv[1], "-rd") == 0 )) {
638+
if (argc == 2) {
639+
printf("No input files were provided!!\n");
640+
return 1;
641+
}
642+
else if (argc == 3) {
643+
printf("Please specify a destination path!!\n");
644+
return 1;
645+
}
646+
char *destPath = argv[argc - 1];
647+
for (int i = 2; i < argc - 1; i++) {
648+
char *fileNames = argv[i];
649+
restoreTrashedfileOnDest(fileNames, destPath);
650+
}
651+
}
588652

589653
// EMPTY TRASH
590654
else if (argc == 2 && strcmp(argv[1], "--empty") == 0 || strcmp(argv[1], "-em") == 0) {
@@ -602,11 +666,12 @@ int main(int argc, char *argv[]) {
602666
"Usage: trashbhuwan --put [FILE] [DIRECTORY]\n"
603667
"Puts file and directory in trash\n\n"
604668
"Available flags:\n"
605-
" -p, --put Put files & directories in trash\n"
606-
" -r, --restore Restore files & directories from the trash\n"
607-
" -ls, --list List trashed files & directories along with disk usage\n"
608-
" -dl, --delete Individually Delete trashed files & directories\n"
609-
" -em, --empty Empty the trash\n"
669+
" -p, --put Put files & directories in trash\n"
670+
" -r, --restore Restore files & directories from trash\n"
671+
" -rd, --restore-dest Restore files & directories to specific destination\n"
672+
" -ls, --list List trashed files & directories along with disk usage\n"
673+
" -dl, --delete Individually Delete trashed files & directories\n"
674+
" -em, --empty Empty the trash\n"
610675
"\nReport bugs to: <https://github.com/tribhuwan-kumar/trashbhuwan/issues>\n"
611676
"'trashbhuwan' home page: <https://github.com/tribhuwan-kumar/trashbhuwan>\n"
612677
"General more help: <https://github.com/tribhuwan-kumar/trashbhuwan#introduction>\n");

0 commit comments

Comments
 (0)