OpenVAS Libraries  9.0.3
openvas_file.c
Go to the documentation of this file.
1 /* openvas-libraries/base
2  * $Id$
3  * Description: File utilities.
4  *
5  * Authors:
6  * Matthew Mundell <matthew.mundell@greenbone.net>
7  * Michael Wiegand <michael.wiegand@greenbone.net
8  * Felix Wolfsteller <felix.wolfsteller@greenbone.net>
9  *
10  * Copyright:
11  * Copyright (C) 2009,2010 Greenbone Networks GmbH
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26  */
27 
33 /* time.h in glibc2 needs this for strptime. */
34 #define _GNU_SOURCE
35 
36 #include "openvas_file.h"
37 
38 #include <string.h>
39 #include <time.h>
40 #include <errno.h>
41 #include <sys/stat.h>
42 
43 #include <glib/gstdio.h> /* for g_remove */
44 
59 int
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 }
72 
85 int
86 openvas_file_remove_recurse (const gchar * pathname)
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 }
123 
134 gboolean
135 openvas_file_copy (const gchar *source_file, const gchar *dest_file)
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 }
161 
172 gboolean
173 openvas_file_move (const gchar *source_file, const gchar *dest_file)
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 }
199 
207 char *
208 openvas_file_as_base64 (const char *path)
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 }
223 
238 gchar *
239 openvas_export_file_name (const char* fname_format, const char* username,
240  const char* type, const char* uuid,
241  const char* creation_iso_time,
242  const char* modification_iso_time,
243  const char* name, const char* format_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 }
int openvas_file_check_is_dir(const char *name)
Checks whether a file is a directory or not.
Definition: openvas_file.c:60
gboolean openvas_file_move(const gchar *source_file, const gchar *dest_file)
Moves a source file into a destination file.
Definition: openvas_file.c:173
gboolean openvas_file_copy(const gchar *source_file, const gchar *dest_file)
Copies a source file into a destination file.
Definition: openvas_file.c:135
char * openvas_file_as_base64(const char *path)
Get the content of a file in base64 format.
Definition: openvas_file.c:208
const char * name
Definition: nasl_init.c:524
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.
Definition: openvas_file.c:239
int openvas_file_remove_recurse(const gchar *pathname)
Recursively removes files and directories.
Definition: openvas_file.c:86