Greenbone Vulnerability Management Libraries  11.0.1
fileutils.c File Reference

File utilities. More...

#include "fileutils.h"
#include <errno.h>
#include <gio/gio.h>
#include <glib/gstdio.h>
#include <glib/gtypes.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
Include dependency graph for fileutils.c:

Go to the source code of this file.

Macros

#define _GNU_SOURCE
 

Functions

int gvm_file_check_is_dir (const char *name)
 Checks whether a file is a directory or not. More...
 
int gvm_file_remove_recurse (const gchar *pathname)
 Recursively removes files and directories. More...
 
gboolean gvm_file_copy (const gchar *source_file, const gchar *dest_file)
 Copies a source file into a destination file. More...
 
gboolean gvm_file_move (const gchar *source_file, const gchar *dest_file)
 Moves a source file into a destination file. More...
 
char * gvm_file_as_base64 (const char *path)
 Get the content of a file in base64 format. More...
 
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. More...
 

Detailed Description

File utilities.

Definition in file fileutils.c.

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 26 of file fileutils.c.

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 223 of file fileutils.c.

228 {
229  time_t now;
230  struct tm *now_broken;
231  gchar *now_date_str, *creation_date_str, *modification_date_str;
232  gchar *now_time_str, *creation_time_str, *modification_time_str;
233  struct tm creation_time, modification_time;
234  gchar *creation_date_short, *modification_date_short;
235  gchar *fname_point;
236  GString *file_name_buf;
237  int format_state = 0;
238  char *ret;
239 
240  creation_date_str = NULL;
241  modification_date_str = NULL;
242  creation_time_str = NULL;
243  modification_time_str = NULL;
244 
245  now = time (NULL);
246  now_broken = localtime (&now);
247  now_date_str =
248  g_strdup_printf ("%04d%02d%02d", (now_broken->tm_year + 1900),
249  (now_broken->tm_mon + 1), now_broken->tm_mday);
250  now_time_str = g_strdup_printf ("%02d%02d%02d", now_broken->tm_hour,
251  now_broken->tm_min, now_broken->tm_sec);
252 
253  memset (&creation_time, 0, sizeof (struct tm));
254  memset (&modification_time, 0, sizeof (struct tm));
255  creation_date_short = NULL;
256  modification_date_short = NULL;
257 
258  if (creation_iso_time && (strlen (creation_iso_time) >= 19))
259  creation_date_short = g_strndup (creation_iso_time, 19);
260 
261  if (creation_date_short
262  && (((ret = strptime (creation_date_short, "%Y-%m-%dT%H:%M:%S",
263  &creation_time))
264  == NULL)
265  || (strlen (ret) == 0)))
266  {
267  creation_date_str =
268  g_strdup_printf ("%04d%02d%02d", (creation_time.tm_year + 1900),
269  (creation_time.tm_mon + 1), creation_time.tm_mday);
270  creation_time_str =
271  g_strdup_printf ("%02d%02d%02d", creation_time.tm_hour,
272  creation_time.tm_min, creation_time.tm_sec);
273  }
274 
275  if (modification_iso_time && (strlen (modification_iso_time) >= 19))
276  modification_date_short = g_strndup (modification_iso_time, 19);
277 
278  if (modification_date_short
279  && (((ret = strptime (modification_date_short, "%Y-%m-%dT%H:%M:%S",
280  &modification_time))
281  == NULL)
282  || (strlen (ret) == 0)))
283  {
284  modification_date_str = g_strdup_printf (
285  "%04d%02d%02d", (modification_time.tm_year + 1900),
286  (modification_time.tm_mon + 1), modification_time.tm_mday);
287 
288  modification_time_str =
289  g_strdup_printf ("%02d%02d%02d", modification_time.tm_hour,
290  modification_time.tm_min, modification_time.tm_sec);
291  }
292 
293  if (creation_date_str == NULL)
294  creation_date_str = g_strdup (now_date_str);
295  if (modification_date_str == NULL)
296  modification_date_str = g_strdup (creation_date_str);
297  if (creation_time_str == NULL)
298  creation_time_str = g_strdup (now_time_str);
299  if (modification_time_str == NULL)
300  modification_time_str = g_strdup (creation_time_str);
301 
302  file_name_buf = g_string_new ("");
303 
304  fname_point = (char *) fname_format;
305 
306  while (format_state >= 0 && *fname_point != '\0')
307  {
308  if (format_state == 0)
309  {
310  if (*fname_point == '%')
311  format_state = 1;
312  else if (*fname_point == '"')
313  g_string_append (file_name_buf, "\\\"");
314  else if (*fname_point <= ' ')
315  g_string_append_c (file_name_buf, '_');
316  else
317  g_string_append_c (file_name_buf, *fname_point);
318  }
319  else if (format_state == 1)
320  {
321  format_state = 0;
322  switch (*fname_point)
323  {
324  case 'C':
325  g_string_append (file_name_buf, creation_date_str);
326  break;
327  case 'c':
328  g_string_append (file_name_buf, creation_time_str);
329  break;
330  case 'd':
331  g_string_append_printf (file_name_buf, "%02d",
332  modification_time.tm_mday);
333  break;
334  case 'D':
335  g_string_append (file_name_buf, now_date_str);
336  break;
337  case 'F':
338  g_string_append (file_name_buf,
339  format_name ? format_name : "XML");
340  break;
341  case 'M':
342  g_string_append (file_name_buf, modification_date_str);
343  break;
344  case 'm':
345  g_string_append (file_name_buf, modification_time_str);
346  break;
347  case 'N':
348  g_string_append (file_name_buf,
349  name ? name : (type ? type : "unnamed"));
350  break;
351  case 'o':
352  g_string_append_printf (file_name_buf, "%02d",
353  modification_time.tm_mon + 1);
354  break;
355  case 'T':
356  g_string_append (file_name_buf, type ? type : "resource");
357  break;
358  case 't':
359  g_string_append (file_name_buf, now_time_str);
360  break;
361  case 'U':
362  g_string_append (file_name_buf, uuid ? uuid : "list");
363  break;
364  case 'u':
365  g_string_append (file_name_buf, username ? username : "");
366  break;
367  case 'Y':
368  g_string_append_printf (file_name_buf, "%04d",
369  modification_time.tm_year + 1900);
370  break;
371  case '%':
372  g_string_append_c (file_name_buf, '%');
373  break;
374  default:
375  g_warning ("%s : Unknown file name format placeholder: %%%c.",
376  __FUNCTION__, *fname_point);
377  format_state = -1;
378  }
379  }
380  fname_point += sizeof (char);
381  }
382 
383  if (format_state || strcmp (file_name_buf->str, "") == 0)
384  {
385  g_warning ("%s : Invalid file name format", __FUNCTION__);
386  g_string_free (file_name_buf, TRUE);
387  return NULL;
388  }
389 
390  fname_point = file_name_buf->str;
391  while (*fname_point != '\0')
392  {
393  if (*fname_point <= ' ')
394  *fname_point = '_';
395  fname_point++;
396  }
397 
398  g_free (now_date_str);
399  g_free (creation_date_str);
400  g_free (creation_time_str);
401  g_free (modification_date_str);
402  return g_string_free (file_name_buf, FALSE);
403 }

◆ 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 192 of file fileutils.c.

193 {
194  GError *error = NULL;
195  char *content, *encoded;
196  gsize len;
197 
198  if (!g_file_get_contents (path, &content, &len, &error))
199  {
200  g_error_free (error);
201  return NULL;
202  }
203  encoded = g_base64_encode ((guchar *) content, len);
204  g_free (content);
205  return encoded;
206 }

◆ 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 53 of file fileutils.c.

54 {
55  struct stat sb;
56 
57  if (g_lstat (name, &sb))
58  {
59  g_warning ("g_lstat(%s) failed - %s\n", name, g_strerror (errno));
60  return -1;
61  }
62 
63  return (S_ISDIR (sb.st_mode));
64 }

Referenced by 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 125 of file fileutils.c.

126 {
127  gboolean rc;
128  GFile *sfile, *dfile;
129  GError *error;
130 
131  sfile = g_file_new_for_path (source_file);
132  dfile = g_file_new_for_path (dest_file);
133  error = NULL;
134 
135  rc =
136  g_file_copy (sfile, dfile, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error);
137  if (!rc)
138  {
139  g_warning ("%s: g_file_copy(%s, %s) failed - %s\n", __FUNCTION__,
140  source_file, dest_file, error->message);
141  g_error_free (error);
142  }
143 
144  g_object_unref (sfile);
145  g_object_unref (dfile);
146  return rc;
147 }

◆ 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 160 of file fileutils.c.

161 {
162  gboolean rc;
163  GFile *sfile, *dfile;
164  GError *error;
165 
166  sfile = g_file_new_for_path (source_file);
167  dfile = g_file_new_for_path (dest_file);
168  error = NULL;
169 
170  rc =
171  g_file_move (sfile, dfile, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error);
172  if (!rc)
173  {
174  g_warning ("%s: g_file_move(%s, %s) failed - %s\n", __FUNCTION__,
175  source_file, dest_file, error->message);
176  g_error_free (error);
177  }
178 
179  g_object_unref (sfile);
180  g_object_unref (dfile);
181  return rc;
182 }

◆ 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 77 of file fileutils.c.

78 {
79  if (gvm_file_check_is_dir (pathname) == 1)
80  {
81  GError *error = NULL;
82  GDir *directory = g_dir_open (pathname, 0, &error);
83 
84  if (directory == NULL)
85  {
86  g_warning ("g_dir_open(%s) failed - %s\n", pathname, error->message);
87  g_error_free (error);
88  return -1;
89  }
90  else
91  {
92  int ret = 0;
93  const gchar *entry = NULL;
94 
95  while ((entry = g_dir_read_name (directory)) && (ret == 0))
96  {
97  gchar *entry_path = g_build_filename (pathname, entry, NULL);
98  ret = gvm_file_remove_recurse (entry_path);
99  g_free (entry_path);
100  if (ret != 0)
101  {
102  g_warning ("Failed to remove %s from %s!", entry, pathname);
103  g_dir_close (directory);
104  return ret;
105  }
106  }
107  g_dir_close (directory);
108  }
109  }
110 
111  return g_remove (pathname);
112 }

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:
gvm_file_remove_recurse
int gvm_file_remove_recurse(const gchar *pathname)
Recursively removes files and directories.
Definition: fileutils.c:77
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:53