Skip to content

Commit

Permalink
feat: add --restore-dest
Browse files Browse the repository at this point in the history
  • Loading branch information
tribhuwan-kumar committed May 28, 2024
1 parent 23f57d4 commit 8999131
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 14 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Trashbhuwan

## Introduction:
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.
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.

Available options:

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

Pass multiple files or directories separating by double quotes or single quotes.
Pass multiple files separating by double quotes or single quotes.

## Installation:

Expand Down
Binary file modified trashbhuwan
Binary file not shown.
89 changes: 77 additions & 12 deletions trashbhuwan.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//<------------------------------CLI Application for managing TRASH wrtitten in 'C'------------------------------------>
// author : @tribhuwan-kumar
// email : freakybytes@duck.com
// email : trashbhuwan@duck.com
//<-------------------------------------------------------------------------------------------------------------------->
#include <time.h>
#include <glob.h>
Expand Down Expand Up @@ -285,7 +285,7 @@ void emptyTrash(const char *trash_files_dir, const char *trash_info_dir) {
void createTrashInfo(const char *filePath) {
char *filePathCopy = strdup(filePath);
const char* username = getUserName();
char* trashInfoDir = getPath("/home/", "/.local/share/Trash/info");
const char* trashInfoDir = getPath("/home/", "/.local/share/Trash/info");

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

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

snprintf(filePath, sizeof(filePath), "%s/%s", trashFilesDir, fileName);
if (access(filePath, F_OK) != -1) {
Expand Down Expand Up @@ -408,14 +408,61 @@ void restoreTrashedfile(const char *fileName) {
}
}
else {
fprintf(stderr, "Couldn't find '%s' in trash\n", fileName);
fprintf(stderr, "Couldn't find '%s' in trash!!\n", fileName);
}
}

// RESTORE FILE ON DIFF DESTINATION
void restoreTrashedfileOnDest(const char *fileName, const char *destPath) {
char filePath[PATH_MAX];
char infoFilePath[PATH_MAX];
char fileDestPath[PATH_MAX];

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

snprintf(filePath, sizeof(filePath), "%s/%s", trashFilesDir, fileName);
snprintf(infoFilePath, sizeof(infoFilePath), "%s/%s.trashinfo", trashInfoDir, fileName);

if (access(filePath, F_OK) != -1) {
if (access(destPath, F_OK) != -1) {
snprintf(fileDestPath, sizeof(fileDestPath), "%s/%s", destPath, fileName);
if (strncmp(fileDestPath, "/mnt", 4) == 0) {
char restoreFileCommand[PATH_MAX];
snprintf(restoreFileCommand, sizeof(restoreFileCommand), "mv \"%s\" \"%s\"", filePath, fileDestPath);
if (system(restoreFileCommand) == 0) {
if (access(infoFilePath, F_OK) == 0) {
remove(infoFilePath);
}
}
else {
fprintf(stderr,"Failed to restore '%s'\n", fileName);
}
}
if (strncmp(fileDestPath, "/mnt", 4) != 0) {
if (rename(filePath, fileDestPath) == 0) {
if (access(infoFilePath, F_OK) == 0) {
remove(infoFilePath);
}
}
else {
fprintf(stderr,"Failed to restore '%s'\n", fileName);
}
}
}
else {
fprintf(stderr, "Not a valid destination '%s'\n", destPath);
}
}
else {
fprintf(stderr, "Couldn't find '%s' in trash!!\n", fileName);
}
}

// INDIVIDUALLY DELETE TRASHED FILE
void deleteTrashedFile(const char *fileName){
char* trashFilesDir = getPath("/home/", "/.local/share/Trash/files");
char* trashInfoDir = getPath("/home/", "/.local/share/Trash/info");
const char* trashFilesDir = getPath("/home/", "/.local/share/Trash/files");
const char* trashInfoDir = getPath("/home/", "/.local/share/Trash/info");

char trashedFilePath[PATH_MAX];
char trashInfoFilePath[PATH_MAX];
Expand Down Expand Up @@ -585,6 +632,23 @@ int main(int argc, char *argv[]) {
restoreTrashedfile(fileNames);
}
}

// RESTORE IN SPECIFIED DESTINATION
else if (argc >= 2 && (strcmp(argv[1], "--restore-dest") == 0 || strcmp(argv[1], "-rd") == 0 )) {
if (argc == 2) {
printf("No input files were provided!!\n");
return 1;
}
else if (argc == 3) {
printf("Please specify a destination path!!\n");
return 1;
}
char *destPath = argv[argc - 1];
for (int i = 2; i < argc - 1; i++) {
char *fileNames = argv[i];
restoreTrashedfileOnDest(fileNames, destPath);
}
}

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

0 comments on commit 8999131

Please sign in to comment.