Greenbone Vulnerability Management Libraries 22.32.0
fileutils.h File Reference

Protos for file utility functions. More...

#include <glib.h>
Include dependency graph for fileutils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int gvm_file_exists (const char *name)
 Checks whether a file or directory exists.
int gvm_file_is_executable (const char *name)
 Checks whether a file or directory exists and is executable.
int gvm_file_is_readable (const char *name)
 Checks whether a file or directory exists and is readable.
int gvm_file_check_is_dir (const char *name)
 Checks whether a file is a directory or not.
int gvm_file_remove_recurse (const gchar *pathname)
 Recursively removes files and directories.
gboolean gvm_file_copy (const gchar *, const gchar *)
 Copies a source file into a destination file.
gboolean gvm_file_move (const gchar *, const gchar *)
 Moves a source file into a destination file.
char * gvm_file_as_base64 (const char *)
 Get the content of a file in base64 format.
gchar * gvm_export_file_name (const char *, const char *, const char *, const char *, const char *, const char *, const char *, const char *)
 Generates a file name for exporting.

Detailed Description

Protos for file utility functions.

This file contains the protos for fileutils.c

Definition in file fileutils.h.

Function Documentation

◆ 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.

Parameters
[in]fname_formatFormat string.
[in]usernameCurrent user name.
[in]typeType of resource.
[in]uuidUUID of resource.
[in]creation_iso_timeCreation time of resource in ISO format.
[in]modification_iso_timeModification time of resource (ISO).
[in]nameName of resource.
[in]format_nameName of format plugin.
Returns
The file name.

Definition at line 269 of file fileutils.c.

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}

Referenced by Ensure().

Here is the caller graph for this function:

◆ gvm_file_as_base64()

char * gvm_file_as_base64 ( const char * path)

Get the content of a file in base64 format.

Parameters
[in]pathPath to file.
Returns
Allocated nul-terminated string, NULL otherwise.

Definition at line 238 of file fileutils.c.

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}

Referenced by Ensure().

Here is the caller graph for this function:

◆ gvm_file_check_is_dir()

int gvm_file_check_is_dir ( const char * name)

Checks whether a file is a directory or not.

This is a replacement for the g_file_test functionality which is reported to be unreliable under certain circumstances, for example if this application and glib are compiled with a different libc.

Symbolic links are not followed.

Parameters
[in]nameName of file or directory.
Returns
1 if parameter is directory, 0 if it is not, -1 if it does not exist or could not be accessed.

Definition at line 45 of file fileutils.c.

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}

Referenced by Ensure(), Ensure(), Ensure(), Ensure(), and gvm_file_remove_recurse().

Here is the caller graph for this function:

◆ gvm_file_copy()

gboolean gvm_file_copy ( const gchar * source_file,
const gchar * dest_file )

Copies a source file into a destination file.

If the destination file does exist already, it will be overwritten.

Parameters
[in]source_fileSource file name.
[in]dest_fileDestination file name.
Returns
TRUE if successful, FALSE otherwise.

Definition at line 171 of file fileutils.c.

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}

Referenced by Ensure().

Here is the caller graph for this function:

◆ gvm_file_exists()

int gvm_file_exists ( const char * name)

Checks whether a file or directory exists.

Unlike g_file_test this checks the permissions based on the effective UID and GID instead of the real one.

Symbolic links are followed.

Parameters
[in]nameName of file or directory.
Returns
1 if file exists, 0 if it is not.

Definition at line 71 of file fileutils.c.

72{
73 return eaccess (name, F_OK) == 0;
74}

Referenced by Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), and Ensure().

Here is the caller graph for this function:

◆ gvm_file_is_executable()

int gvm_file_is_executable ( const char * name)

Checks whether a file or directory exists and is executable.

Unlike g_file_test this checks the permissions based on the effective UID and GID instead of the real one.

Symbolic links are followed.

Parameters
[in]nameName of file or directory.
Returns
1 if file is executable, 0 if it is not.

Definition at line 89 of file fileutils.c.

90{
91 return eaccess (name, X_OK) == 0;
92}

Referenced by Ensure(), and Ensure().

Here is the caller graph for this function:

◆ gvm_file_is_readable()

int gvm_file_is_readable ( const char * name)

Checks whether a file or directory exists and is readable.

Unlike g_file_test this checks the permissions based on the effective UID and GID instead of the real one.

Symbolic links are followed.

Parameters
[in]nameName of file or directory.
Returns
1 if file is readable, 0 if it is not.

Definition at line 107 of file fileutils.c.

108{
109 return eaccess (name, R_OK) == 0;
110}

Referenced by Ensure(), and Ensure().

Here is the caller graph for this function:

◆ gvm_file_move()

gboolean gvm_file_move ( const gchar * source_file,
const gchar * dest_file )

Moves a source file into a destination file.

If the destination file does exist already, it will be overwritten.

Parameters
[in]source_fileSource file name.
[in]dest_fileDestination file name.
Returns
TRUE if successful, FALSE otherwise.

Definition at line 206 of file fileutils.c.

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}

Referenced by Ensure().

Here is the caller graph for this function:

◆ gvm_file_remove_recurse()

int gvm_file_remove_recurse ( const gchar * pathname)

Recursively removes files and directories.

This function will recursively call itself to delete a path and any contents of this path.

Parameters
[in]pathnameThe name of the file to be deleted from the filesystem.
Returns
0 if the name was successfully deleted, -1 if an error occurred.

Definition at line 123 of file fileutils.c.

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}
int gvm_file_remove_recurse(const gchar *pathname)
Recursively removes files and directories.
Definition fileutils.c:123
int gvm_file_check_is_dir(const char *name)
Checks whether a file is a directory or not.
Definition fileutils.c:45

References gvm_file_check_is_dir(), and gvm_file_remove_recurse().

Referenced by encrypt_stream_internal(), and gvm_file_remove_recurse().

Here is the call graph for this function:
Here is the caller graph for this function: