OpenVAS Libraries  9.0.3
openvas_file.c File Reference

File utilities. More...

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

Go to the source code of this file.

Macros

#define _GNU_SOURCE
 

Functions

int openvas_file_check_is_dir (const char *name)
 Checks whether a file is a directory or not. More...
 
int openvas_file_remove_recurse (const gchar *pathname)
 Recursively removes files and directories. More...
 
gboolean openvas_file_copy (const gchar *source_file, const gchar *dest_file)
 Copies a source file into a destination file. More...
 
gboolean openvas_file_move (const gchar *source_file, const gchar *dest_file)
 Moves a source file into a destination file. More...
 
char * openvas_file_as_base64 (const char *path)
 Get the content of a file in base64 format. More...
 
gchar * openvas_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 openvas_file.c.

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 34 of file openvas_file.c.

Function Documentation

◆ openvas_export_file_name()

gchar* openvas_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 239 of file openvas_file.c.

References name.

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

◆ openvas_file_as_base64()

char* openvas_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 208 of file openvas_file.c.

209 {
210  GError *error = NULL;
211  char *content, *encoded;
212  gsize len;
213 
214  if (!g_file_get_contents (path, &content, &len, &error))
215  {
216  g_error_free (error);
217  return NULL;
218  }
219  encoded = g_base64_encode ((guchar *) content, len);
220  g_free (content);
221  return encoded;
222 }

◆ openvas_file_check_is_dir()

int openvas_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 60 of file openvas_file.c.

References name.

Referenced by openvas_file_remove_recurse().

61 {
62  struct stat sb;
63 
64  if (g_lstat (name, &sb))
65  {
66  g_warning ("g_lstat(%s) failed - %s\n", name, g_strerror (errno));
67  return -1;
68  }
69 
70  return (S_ISDIR (sb.st_mode));
71 }
const char * name
Definition: nasl_init.c:524
Here is the caller graph for this function:

◆ openvas_file_copy()

gboolean openvas_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 135 of file openvas_file.c.

136 {
137  gboolean rc;
138  GFile *sfile, *dfile;
139  GError *error;
140 
141 #if !GLIB_CHECK_VERSION(2, 35, 0)
142  g_type_init ();
143 #endif
144  sfile = g_file_new_for_path (source_file);
145  dfile = g_file_new_for_path (dest_file);
146  error = NULL;
147 
148  rc = g_file_copy (sfile, dfile, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL,
149  &error);
150  if (!rc)
151  {
152  g_warning ("%s: g_file_copy(%s, %s) failed - %s\n", __FUNCTION__,
153  source_file, dest_file, error->message);
154  g_error_free (error);
155  }
156 
157  g_object_unref (sfile);
158  g_object_unref (dfile);
159  return rc;
160 }

◆ openvas_file_move()

gboolean openvas_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 173 of file openvas_file.c.

174 {
175  gboolean rc;
176  GFile *sfile, *dfile;
177  GError *error;
178 
179 #if !GLIB_CHECK_VERSION(2, 35, 0)
180  g_type_init ();
181 #endif
182  sfile = g_file_new_for_path (source_file);
183  dfile = g_file_new_for_path (dest_file);
184  error = NULL;
185 
186  rc = g_file_move (sfile, dfile, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL,
187  &error);
188  if (!rc)
189  {
190  g_warning ("%s: g_file_move(%s, %s) failed - %s\n", __FUNCTION__,
191  source_file, dest_file, error->message);
192  g_error_free (error);
193  }
194 
195  g_object_unref (sfile);
196  g_object_unref (dfile);
197  return rc;
198 }

◆ openvas_file_remove_recurse()

int openvas_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. Please note that errno is currently not guaranteed to contain the correct value if -1 is returned.
Todo:
Set errno when we return -1 to maintain remove() compatibility.

Definition at line 86 of file openvas_file.c.

References openvas_file_check_is_dir(), and openvas_file_remove_recurse().

Referenced by openvas_file_remove_recurse().

87 {
89  if (openvas_file_check_is_dir (pathname) == 1)
90  {
91  GError *error = NULL;
92  GDir *directory = g_dir_open (pathname, 0, &error);
93 
94  if (directory == NULL)
95  {
96  g_warning ("g_dir_open(%s) failed - %s\n", pathname, error->message);
97  g_error_free (error);
98  return -1;
99  }
100  else
101  {
102  int ret = 0;
103  const gchar *entry = NULL;
104 
105  while ((entry = g_dir_read_name (directory)) && (ret == 0))
106  {
107  gchar *entry_path = g_build_filename (pathname, entry, NULL);
108  ret = openvas_file_remove_recurse (entry_path);
109  g_free (entry_path);
110  if (ret != 0)
111  {
112  g_warning ("Failed to remove %s from %s!", entry, pathname);
113  g_dir_close (directory);
114  return ret;
115  }
116  }
117  g_dir_close (directory);
118  }
119  }
120 
121  return g_remove (pathname);
122 }
int openvas_file_check_is_dir(const char *name)
Checks whether a file is a directory or not.
Definition: openvas_file.c:60
int openvas_file_remove_recurse(const gchar *pathname)
Recursively removes files and directories.
Definition: openvas_file.c:86
Here is the call graph for this function:
Here is the caller graph for this function: