1 /* Copyright (c) 2018 Adrian Lopez
3 This software is provided 'as-is', without any express or implied
4 warranty. In no event will the authors be held liable for any damages
5 arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it
9 freely, subject to the following restrictions:
11 1. The origin of this software must not be misrepresented; you must not
12 claim that you wrote the original software. If you use this software
13 in a product, an acknowledgment in the product documentation would be
14 appreciated but is not required.
15 2. Altered source versions must be plainly marked as such, and must not be
16 misrepresented as being the original software.
17 3. This notice may not be removed or altered from any source distribution. */
26 #define LOG_HEADER "[fdupes log]\n"
28 /* Open log file in append mode. If file exists, make sure it is a valid fdupes log file.
30 struct log_info *log_open(char *filename, int *error)
32 struct log_info *info;
36 info = (struct log_info*) malloc(sizeof(struct log_info));
40 *error = LOG_ERROR_OUT_OF_MEMORY;
45 info->file = fopen(filename, "a+");
49 *error = LOG_ERROR_FOPEN_FAILED;
55 fmatch(info->file, LOG_HEADER, &is_match, &read);
56 if (!is_match && read > 0)
59 *error = LOG_ERROR_NOT_A_LOG_FILE;
65 info->append = read > 0;
72 *error = LOG_ERROR_NONE;
77 /* Free linked lists holding set of deleted and remaining files.
79 void log_free_set(struct log_info *info)
82 struct log_file *next;
108 /* Signal beginning of duplicate set.
110 void log_begin_set(struct log_info *info)
115 /* Add deleted file to log.
117 int log_file_deleted(struct log_info *info, char *name)
119 struct log_file *file;
121 file = (struct log_file*) malloc(sizeof(struct log_file));
125 file->next = info->deleted;
126 file->filename = name;
128 info->deleted = file;
133 /* Add remaining file to log.
135 int log_file_remaining(struct log_info *info, char *name)
137 struct log_file *file;
139 file = (struct log_file*) malloc(sizeof(struct log_file));
143 file->next = info->remaining;
144 file->filename = name;
146 info->remaining = file;
151 /* Output log header.
153 void log_header(FILE *file)
155 fprintf(file, "%s\n", LOG_HEADER);
158 /* Output log timestamp.
160 void log_timestamp(FILE *file)
162 time_t t = time(NULL);
163 struct tm tm = *localtime(&t);
165 fprintf(file, "Log entry for %d-%02d-%02d %02d:%02d:%02d\n\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
168 /* Output current working directory.
170 void log_cwd(FILE *file)
172 char *cwd = getworkingdirectory();
174 fprintf(file, "working directory:\n %s\n\n", cwd);
179 /* Signal the end of a duplicate set.
181 void log_end_set(struct log_info *info)
185 if (info->deleted == 0)
191 fprintf(info->file, "---\n\n");
193 log_header(info->file);
195 log_timestamp(info->file);
204 fprintf(info->file, "deleted %s\n", f->filename);
211 fprintf(info->file, " left %s\n", f->filename);
215 fprintf(info->file, "\n");
220 /* Close log and free all memory.
222 void log_close(struct log_info *info)