Greenbone Vulnerability Management Libraries  11.0.1
fileutils.c
Go to the documentation of this file.
1 /* Copyright (C) 2009-2019 Greenbone Networks GmbH
2  *
3  * SPDX-License-Identifier: GPL-2.0-or-later
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
25 /* time.h in glibc2 needs this for strptime. */
26 #define _GNU_SOURCE
27 
28 #include "fileutils.h"
29 
30 #include <errno.h> /* for errno */
31 #include <gio/gio.h> /* for g_file_new_for_path, GFile */
32 #include <glib/gstdio.h> /* for g_lstat, g_remove */
33 #include <glib/gtypes.h> /* for gsize */
34 #include <string.h> /* for strlen, memset, strcmp */
35 #include <sys/stat.h> /* for stat, S_ISDIR */
36 #include <time.h> /* for tm, strptime, localtime, time, time_t */
37 
52 int
53 gvm_file_check_is_dir (const char *name)
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 }
65 
76 int
77 gvm_file_remove_recurse (const gchar *pathname)
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 }
113 
124 gboolean
125 gvm_file_copy (const gchar *source_file, const gchar *dest_file)
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 }
148 
159 gboolean
160 gvm_file_move (const gchar *source_file, const gchar *dest_file)
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 }
183 
191 char *
192 gvm_file_as_base64 (const char *path)
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 }
207 
222 gchar *
223 gvm_export_file_name (const char *fname_format, const char *username,
224  const char *type, const char *uuid,
225  const char *creation_iso_time,
226  const char *modification_iso_time, const char *name,
227  const char *format_name)
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_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
fileutils.h
Protos for file utility functions.
gvm_file_as_base64
char * gvm_file_as_base64(const char *path)
Get the content of a file in base64 format.
Definition: fileutils.c:192
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:160
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:125
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:223