Greenbone Vulnerability Management Libraries: util/fileutils.c Source File

Greenbone Vulnerability Management Libraries 23.1.2

fileutils.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009-2023 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
10
11/* time.h in glibc2 needs this for strptime. */
12#define _GNU_SOURCE
13
14#include "fileutils.h"
15
16#include <errno.h> /* for errno */
17#include <gio/gio.h> /* for g_file_new_for_path, GFile */
18#include <glib/gstdio.h> /* for g_lstat, g_remove */
19#include <glib/gtypes.h> /* for gsize */
20#include <string.h> /* for strlen, memset, strcmp */
21#include <sys/stat.h> /* for stat, S_ISDIR */
22#include <time.h> /* for tm, strptime, localtime, time, time_t */
23
24#undef G_LOG_DOMAIN
28#define G_LOG_DOMAIN "libgvm util"
29
44int
45gvm_file_check_is_dir (const char *name)
46{
47 struct stat sb;
48
49 if (g_lstat (name, &sb))
50 {
51 g_warning ("g_lstat(%s) failed - %s\n", name, g_strerror (errno));
52 return -1;
53 }
54
55 return S_ISDIR (sb.st_mode);
56}
57
70int
71gvm_file_exists (const char *name)
72{
73 return eaccess (name, F_OK) == 0;
74}
75
88int
89gvm_file_is_executable (const char *name)
90{
91 return eaccess (name, X_OK) == 0;
92}
93
106int
107gvm_file_is_readable (const char *name)
108{
109 return eaccess (name, R_OK) == 0;
110}
111
122int
123gvm_file_remove_recurse (const gchar *pathname)
124{
125 if (gvm_file_check_is_dir (pathname) == 1)
126 {
127 GError *error = NULL;
128 GDir *directory = g_dir_open (pathname, 0, &error);
129
130 if (directory == NULL)
131 {
132 g_warning ("g_dir_open(%s) failed - %s\n", pathname, error->message);
133 g_error_free (error);
134 return -1;
135 }
136 else
137 {
138 int ret = 0;
139 const gchar *entry = NULL;
140
141 while ((entry = g_dir_read_name (directory)) && (ret == 0))
142 {
143 gchar *entry_path = g_build_filename (pathname, entry, NULL);
144 ret = gvm_file_remove_recurse (entry_path);
145 g_free (entry_path);
146 if (ret != 0)
147 {
148 g_warning ("Failed to remove %s from %s!", entry, pathname);
149 g_dir_close (directory);
150 return ret;
151 }
152 }
153 g_dir_close (directory);
154 }
155 }
156
157 return g_remove (pathname);
158}
159
170gboolean
171gvm_file_copy (const gchar *source_file, const gchar *dest_file)
172{
173 gboolean rc;
174 GFile *sfile, *dfile;
175 GError *error;
176
177 sfile = g_file_new_for_path (source_file);
178 dfile = g_file_new_for_path (dest_file);
179 error = NULL;
180
181 rc =
182 g_file_copy (sfile, dfile, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error);
183 if (!rc)
184 {
185 g_warning ("%s: g_file_copy(%s, %s) failed - %s\n", __func__, source_file,
186 dest_file, error->message);
187 g_error_free (error);
188 }
189
190 g_object_unref (sfile);
191 g_object_unref (dfile);
192 return rc;
193}
194
205gboolean
206gvm_file_move (const gchar *source_file, const gchar *dest_file)
207{
208 gboolean rc;
209 GFile *sfile, *dfile;
210 GError *error;
211
212 sfile = g_file_new_for_path (source_file);
213 dfile = g_file_new_for_path (dest_file);
214 error = NULL;
215
216 rc =
217 g_file_move (sfile, dfile, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error);
218 if (!rc)
219 {
220 g_warning ("%s: g_file_move(%s, %s) failed - %s\n", __func__, source_file,
221 dest_file, error->message);
222 g_error_free (error);
223 }
224
225 g_object_unref (sfile);
226 g_object_unref (dfile);
227 return rc;
228}
229
237char *
238gvm_file_as_base64 (const char *path)
239{
240 GError *error = NULL;
241 char *content, *encoded;
242 gsize len;
243
244 if (!g_file_get_contents (path, &content, &len, &error))
245 {
246 g_error_free (error);
247 return NULL;
248 }
249 encoded = g_base64_encode ((guchar *) content, len);
250 g_free (content);
251 return encoded;
252}
253
268gchar *
269gvm_export_file_name (const char *fname_format, const char *username,
270 const char *type, const char *uuid,
271 const char *creation_iso_time,
272 const char *modification_iso_time, const char *name,
273 const char *format_name)
274{
275 time_t now;
276 struct tm now_broken;
277 gchar *now_date_str, *creation_date_str, *modification_date_str;
278 gchar *now_time_str, *creation_time_str, *modification_time_str;
279 struct tm creation_time, modification_time;
280 gchar *creation_date_short, *modification_date_short;
281 gchar *fname_point;
282 GString *file_name_buf;
283 int format_state = 0;
284 char *ret;
285
286 creation_date_str = NULL;
287 modification_date_str = NULL;
288 creation_time_str = NULL;
289 modification_time_str = NULL;
290
291 now = time (NULL);
292 if (localtime_r (&now, &now_broken) == NULL)
293 {
294 g_warning ("%s: localtime failed", __func__);
295 }
296 now_date_str = g_strdup_printf ("%04d%02d%02d", (now_broken.tm_year + 1900),
297 (now_broken.tm_mon + 1), now_broken.tm_mday);
298 now_time_str = g_strdup_printf ("%02d%02d%02d", now_broken.tm_hour,
299 now_broken.tm_min, now_broken.tm_sec);
300
301 memset (&creation_time, 0, sizeof (struct tm));
302 memset (&modification_time, 0, sizeof (struct tm));
303 creation_date_short = NULL;
304 modification_date_short = NULL;
305
306 if (creation_iso_time && (strlen (creation_iso_time) >= 19))
307 creation_date_short = g_strndup (creation_iso_time, 19);
308
309 if (creation_date_short
310 && (((ret = strptime (creation_date_short, "%Y-%m-%dT%H:%M:%S",
311 &creation_time))
312 == NULL)
313 || (strlen (ret) == 0)))
314 {
315 creation_date_str =
316 g_strdup_printf ("%04d%02d%02d", (creation_time.tm_year + 1900),
317 (creation_time.tm_mon + 1), creation_time.tm_mday);
318 creation_time_str =
319 g_strdup_printf ("%02d%02d%02d", creation_time.tm_hour,
320 creation_time.tm_min, creation_time.tm_sec);
321 }
322 g_free (creation_date_short);
323
324 if (modification_iso_time && (strlen (modification_iso_time) >= 19))
325 modification_date_short = g_strndup (modification_iso_time, 19);
326
327 if (modification_date_short
328 && (((ret = strptime (modification_date_short, "%Y-%m-%dT%H:%M:%S",
329 &modification_time))
330 == NULL)
331 || (strlen (ret) == 0)))
332 {
333 modification_date_str = g_strdup_printf (
334 "%04d%02d%02d", (modification_time.tm_year + 1900),
335 (modification_time.tm_mon + 1), modification_time.tm_mday);
336
337 modification_time_str =
338 g_strdup_printf ("%02d%02d%02d", modification_time.tm_hour,
339 modification_time.tm_min, modification_time.tm_sec);
340 }
341 g_free (modification_date_short);
342
343 if (creation_date_str == NULL)
344 creation_date_str = g_strdup (now_date_str);
345 if (modification_date_str == NULL)
346 modification_date_str = g_strdup (creation_date_str);
347 if (creation_time_str == NULL)
348 creation_time_str = g_strdup (now_time_str);
349 if (modification_time_str == NULL)
350 modification_time_str = g_strdup (creation_time_str);
351
352 file_name_buf = g_string_new ("");
353
354 fname_point = (char *) fname_format;
355
356 while (format_state >= 0 && *fname_point != '\0')
357 {
358 if (format_state == 0)
359 {
360 if (*fname_point == '%')
361 format_state = 1;
362 else if (*fname_point == '"')
363 g_string_append (file_name_buf, "\\\"");
364 else if (*fname_point <= ' ')
365 g_string_append_c (file_name_buf, '_');
366 else
367 g_string_append_c (file_name_buf, *fname_point);
368 }
369 else if (format_state == 1)
370 {
371 format_state = 0;
372 switch (*fname_point)
373 {
374 case 'C':
375 g_string_append (file_name_buf, creation_date_str);
376 break;
377 case 'c':
378 g_string_append (file_name_buf, creation_time_str);
379 break;
380 case 'd':
381 g_string_append_printf (file_name_buf, "%02d",
382 modification_time.tm_mday);
383 break;
384 case 'D':
385 g_string_append (file_name_buf, now_date_str);
386 break;
387 case 'F':
388 g_string_append (file_name_buf,
389 format_name ? format_name : "XML");
390 break;
391 case 'M':
392 g_string_append (file_name_buf, modification_date_str);
393 break;
394 case 'm':
395 g_string_append (file_name_buf, modification_time_str);
396 break;
397 case 'N':
398 g_string_append (file_name_buf,
399 name ? name : (type ? type : "unnamed"));
400 break;
401 case 'o':
402 g_string_append_printf (file_name_buf, "%02d",
403 modification_time.tm_mon + 1);
404 break;
405 case 'T':
406 g_string_append (file_name_buf, type ? type : "resource");
407 break;
408 case 't':
409 g_string_append (file_name_buf, now_time_str);
410 break;
411 case 'U':
412 g_string_append (file_name_buf, uuid ? uuid : "list");
413 break;
414 case 'u':
415 g_string_append (file_name_buf, username ? username : "");
416 break;
417 case 'Y':
418 g_string_append_printf (file_name_buf, "%04d",
419 modification_time.tm_year + 1900);
420 break;
421 case '%':
422 g_string_append_c (file_name_buf, '%');
423 break;
424 default:
425 g_warning ("%s : Unknown file name format placeholder: %%%c.",
426 __func__, *fname_point);
427 format_state = -1;
428 }
429 }
430 fname_point += sizeof (char);
431 }
432
433 if (format_state || strcmp (file_name_buf->str, "") == 0)
434 {
435 g_warning ("%s : Invalid file name format", __func__);
436 g_string_free (file_name_buf, TRUE);
437 return NULL;
438 }
439
440 fname_point = file_name_buf->str;
441 while (*fname_point != '\0')
442 {
443 if (*fname_point <= ' ')
444 *fname_point = '_';
445 fname_point++;
446 }
447
448 g_free (now_date_str);
449 g_free (now_time_str);
450 g_free (creation_date_str);
451 g_free (creation_time_str);
452 g_free (modification_date_str);
453 g_free (modification_time_str);
454 return g_string_free (file_name_buf, FALSE);
455}
gvm_file_remove_recurse
int gvm_file_remove_recurse(const gchar *pathname)
Recursively removes files and directories.
Definition fileutils.c:123
gvm_file_exists
int gvm_file_exists(const char *name)
Checks whether a file or directory exists.
Definition fileutils.c:71
gvm_export_file_name
gchar * gvm_export_file_name(const char *fname_format, const char *username, const char *type, const char *uuid, const char *creation_iso_time, const char *modification_iso_time, const char *name, const char *format_name)
Generates a file name for exporting.
Definition fileutils.c:269
gvm_file_check_is_dir
int gvm_file_check_is_dir(const char *name)
Checks whether a file is a directory or not.
Definition fileutils.c:45
gvm_file_move
gboolean gvm_file_move(const gchar *source_file, const gchar *dest_file)
Moves a source file into a destination file.
Definition fileutils.c:206
gvm_file_is_readable
int gvm_file_is_readable(const char *name)
Checks whether a file or directory exists and is readable.
Definition fileutils.c:107
gvm_file_is_executable
int gvm_file_is_executable(const char *name)
Checks whether a file or directory exists and is executable.
Definition fileutils.c:89
gvm_file_as_base64
char * gvm_file_as_base64(const char *path)
Get the content of a file in base64 format.
Definition fileutils.c:238
gvm_file_copy
gboolean gvm_file_copy(const gchar *source_file, const gchar *dest_file)
Copies a source file into a destination file.
Definition fileutils.c:171
fileutils.h
Protos for file utility functions.