summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--TODO2
-rw-r--r--trashsys.c96
2 files changed, 86 insertions, 12 deletions
diff --git a/TODO b/TODO
index 502734a..b4c0a89 100644
--- a/TODO
+++ b/TODO
@@ -1,4 +1,4 @@
-1. Implement -l and -L. Maybe redo the function. Its not working as well as i thought. Maybe not use get line.
+1. Implement -l and -L. Maybe redo the function.
2. Just a note: When implementing the -L -l functions we need to make it clear what's in tmp and whats not. Not sure exactly how ill do this but i have some ideas. One way of dealing with files in tmp is to force the user to pass the -t argument if they want to deal with tmp stuff. Like if i say: 'tsr -t -l' it will only list files in the tmp trashcan. Maybe that IS the best approach to this?
3. Implement function to read log files? And maybe act upon the read contents? I'll have to think more about this.
4. Handle case where we pass a bunch of options but no filename
diff --git a/trashsys.c b/trashsys.c
index 4dc4f9a..4331e30 100644
--- a/trashsys.c
+++ b/trashsys.c
@@ -374,11 +374,13 @@ int64_t find_highest_id (const struct initial_path_info *ipi) { // Find highest
if(concat_str(stat_fullpath, PATH_MAX, ipi->ts_path_log_withslash) == NULL) {
fprintf(stderr, "Path is too long\n"); // rare case but at least its handled
+ closedir(dir);
return FUNCTION_FAILURE;
}
if(concat_str(stat_fullpath, REM_SZ(PATH_MAX, stat_fullpath), ddd->d_name) == NULL) {
fprintf(stderr, "Path is too long\n"); // rare case but at least its handled
+ closedir(dir);
return FUNCTION_FAILURE;
}
@@ -527,8 +529,67 @@ int write_log_file(struct dynamic_paths *dp, struct trashsys_log_info *tli, cons
char tmp[2];
};
*/
-
-int list_files (struct initial_path_info *ipi, const bool t_used, const bool L_used) {
+
+char *rawtime_to_readable(time_t rawtime) {
+
+ struct tm *tmp;
+ char *pretty_time = malloc(sizeof(char) * 512);
+ tmp = localtime(&rawtime);
+ if(strftime(pretty_time, 512, "%F", tmp) == 0) {
+ free(pretty_time);
+ return NULL;
+ }
+
+ return pretty_time;
+}
+/*
+char *bytes_to_readable(size_t bytes) {
+
+ by
+}
+*/
+int lfc_formatted (struct list_file_content *lfc, const bool L_used) {
+
+ time_t rawtime;
+ char *endptr;
+ char *pretty_time;
+
+ rawtime = (time_t)strtoll(lfc->time, &endptr, 10);
+ if (errno == ERANGE || lfc->time == endptr) {
+ fprintf(stdout, "strtoll fail\n");
+ return FUNCTION_FAILURE;
+ }
+ pretty_time = rawtime_to_readable(rawtime);
+ if(pretty_time == NULL){
+ fprintf(stdout, "Cannot convert time to readable\n");
+ return FUNCTION_FAILURE;
+ }
+
+
+ if (L_used == true) {
+ fprintf(stdout, "ID: %s %s %s (temporary) MiB %s bytes Trashed at: %s (unixtime: %s) originalpath: %s\n"
+ , lfc->ID
+ , lfc->filename
+ , lfc->filesize
+ , lfc->filesize
+ , pretty_time
+ , lfc->time
+ , lfc->originalpath
+ );
+ free(pretty_time);
+ return FUNCTION_SUCCESS;
+ }
+ fprintf(stdout, "ID: %s %s %s bytes Trashed at: %s\n"
+ , lfc->ID
+ , lfc->filename
+ , lfc->filesize
+ , pretty_time
+ );
+ free(pretty_time);
+ return FUNCTION_SUCCESS;
+}
+
+int fill_lfc (struct initial_path_info *ipi, const bool L_used) {
// ID: 11 | ts_file3.txt | 1 MiB | trashed at: 2024-07-02
// ID: 11 | ts_file3.txt | 1 MiB | 1112731 bytes | trashed at 2024-07-02 (17287368) | originalpath: /home/oskar/code/ts_file3.txt
@@ -546,12 +607,14 @@ int list_files (struct initial_path_info *ipi, const bool t_used, const bool L_u
stat_fullpath[0] = '\0';
if(concat_str(stat_fullpath, PATH_MAX, ipi->ts_path_log_withslash) == NULL) {
+ closedir(dir);
fprintf(stderr, "Path is too long\n"); // rare case but at least its handled
return FUNCTION_FAILURE;
}
if(concat_str(stat_fullpath, REM_SZ(PATH_MAX, stat_fullpath), ddd->d_name) == NULL) {
fprintf(stderr, "Path is too long\n"); // rare case but at least its handled
+ closedir(dir);
return FUNCTION_FAILURE;
}
@@ -563,11 +626,10 @@ int list_files (struct initial_path_info *ipi, const bool t_used, const bool L_u
FILE *file = fopen(stat_fullpath, "r");
if (file == NULL) {
+ closedir(dir);
return FUNCTION_FAILURE;
}
- if (t_used == true || L_used == true) {
- return 0;
- }
+
struct list_file_content lfc;
char *lfc_a[7];
lfc.ID[0] = '\0';
@@ -589,14 +651,22 @@ int list_files (struct initial_path_info *ipi, const bool t_used, const bool L_u
for ( ; i < 7 ; i++, linenum++) {
char *line;
size_t start;
- if(get_line(stat_fullpath, linenum, &line, &start) == FUNCTION_FAILURE) { return FUNCTION_FAILURE; }
- if(concat_str(lfc_a[i], PATH_MAX, line) == NULL) { free(line); return FUNCTION_FAILURE; }
+ if(get_line(stat_fullpath, linenum, &line, &start) == FUNCTION_FAILURE) { closedir(dir); return FUNCTION_FAILURE; }
+ if(concat_str(lfc_a[i], PATH_MAX, line) == NULL) { free(line); closedir(dir); return FUNCTION_FAILURE; }
+ for(int si = 0 ;; si++) {
+ if(lfc_a[i][si] == '\n') {
+ lfc_a[i][si] = '\0';
+ break;
+ }
+ }
free(line);
}
fclose(file);
- fprintf(stdout, "%s%s%s%s%s%s%s", lfc.ID, lfc.filename, lfc.trashed_filename, lfc.filesize, lfc.time, lfc.originalpath, lfc.tmp);
-
+ //fprintf(stdout, "%s%s%s%s%s%s%s", lfc.ID, lfc.filename, lfc.trashed_filename, lfc.filesize, lfc.time, lfc.originalpath, lfc.tmp);
+ bool L_used_n = L_used;
+ if(L_used == false) { L_used_n = false; }
+ lfc_formatted(&lfc, L_used_n);
}
}
@@ -739,8 +809,12 @@ int main (int argc, char *argv[]) {
return EXIT_FAILURE;
}
- if(l_used == true) {
- list_files(&ipi_m, t_used, L_used);
+ if(l_used == true && L_used == true) {
+ fill_lfc(&ipi_m, L_used);
+ return EXIT_SUCCESS;
+ } else {
+ fill_lfc(&ipi_m, false);
+ return EXIT_SUCCESS;
}
int index;