File: fileutils.c

package info (click to toggle)
gvm-libs 22.34.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,968 kB
  • sloc: ansic: 39,015; makefile: 26
file content (455 lines) | stat: -rw-r--r-- 13,373 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/* SPDX-FileCopyrightText: 2009-2023 Greenbone AG
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

/**
 * @file
 * @brief File utilities.
 */

/* time.h in glibc2 needs this for strptime. */
#define _GNU_SOURCE

#include "fileutils.h"

#include <errno.h>       /* for errno */
#include <gio/gio.h>     /* for g_file_new_for_path, GFile */
#include <glib/gstdio.h> /* for g_lstat, g_remove */
#include <glib/gtypes.h> /* for gsize */
#include <string.h>      /* for strlen, memset, strcmp */
#include <sys/stat.h>    /* for stat, S_ISDIR */
#include <time.h>        /* for tm, strptime, localtime, time, time_t */

#undef G_LOG_DOMAIN
/**
 * @brief GLib logging domain.
 */
#define G_LOG_DOMAIN "libgvm util"

/**
 * @brief 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.
 *
 * @param[in]  name  Name of file or directory.
 *
 * @return 1 if parameter is directory, 0 if it is not, -1 if it does not
 *         exist or could not be accessed.
 */
int
gvm_file_check_is_dir (const char *name)
{
  struct stat sb;

  if (g_lstat (name, &sb))
    {
      g_warning ("g_lstat(%s) failed - %s\n", name, g_strerror (errno));
      return -1;
    }

  return S_ISDIR (sb.st_mode);
}

/**
 * @brief 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.
 *
 * @param[in]  name  Name of file or directory.
 *
 * @return 1 if file exists, 0 if it is not.
 */
int
gvm_file_exists (const char *name)
{
  return eaccess (name, F_OK) == 0;
}

/**
 * @brief 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.
 *
 * @param[in]  name  Name of file or directory.
 *
 * @return 1 if file is executable, 0 if it is not.
 */
int
gvm_file_is_executable (const char *name)
{
  return eaccess (name, X_OK) == 0;
}

/**
 * @brief 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.
 *
 * @param[in]  name  Name of file or directory.
 *
 * @return 1 if file is readable, 0 if it is not.
 */
int
gvm_file_is_readable (const char *name)
{
  return eaccess (name, R_OK) == 0;
}

/**
 * @brief Recursively removes files and directories.
 *
 * This function will recursively call itself to delete a path and any
 * contents of this path.
 *
 * @param[in]  pathname  The name of the file to be deleted from the filesystem.
 *
 * @return 0 if the name was successfully deleted, -1 if an error occurred.
 */
int
gvm_file_remove_recurse (const gchar *pathname)
{
  if (gvm_file_check_is_dir (pathname) == 1)
    {
      GError *error = NULL;
      GDir *directory = g_dir_open (pathname, 0, &error);

      if (directory == NULL)
        {
          g_warning ("g_dir_open(%s) failed - %s\n", pathname, error->message);
          g_error_free (error);
          return -1;
        }
      else
        {
          int ret = 0;
          const gchar *entry = NULL;

          while ((entry = g_dir_read_name (directory)) && (ret == 0))
            {
              gchar *entry_path = g_build_filename (pathname, entry, NULL);
              ret = gvm_file_remove_recurse (entry_path);
              g_free (entry_path);
              if (ret != 0)
                {
                  g_warning ("Failed to remove %s from %s!", entry, pathname);
                  g_dir_close (directory);
                  return ret;
                }
            }
          g_dir_close (directory);
        }
    }

  return g_remove (pathname);
}

/**
 * @brief Copies a source file into a destination file.
 *
 * If the destination file does exist already, it will be overwritten.
 *
 * @param[in]  source_file  Source file name.
 * @param[in]  dest_file    Destination file name.
 *
 * @return TRUE if successful, FALSE otherwise.
 */
gboolean
gvm_file_copy (const gchar *source_file, const gchar *dest_file)
{
  gboolean rc;
  GFile *sfile, *dfile;
  GError *error;

  sfile = g_file_new_for_path (source_file);
  dfile = g_file_new_for_path (dest_file);
  error = NULL;

  rc =
    g_file_copy (sfile, dfile, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error);
  if (!rc)
    {
      g_warning ("%s: g_file_copy(%s, %s) failed - %s\n", __func__, source_file,
                 dest_file, error->message);
      g_error_free (error);
    }

  g_object_unref (sfile);
  g_object_unref (dfile);
  return rc;
}

/**
 * @brief Moves a source file into a destination file.
 *
 * If the destination file does exist already, it will be overwritten.
 *
 * @param[in]  source_file  Source file name.
 * @param[in]  dest_file    Destination file name.
 *
 * @return TRUE if successful, FALSE otherwise.
 */
gboolean
gvm_file_move (const gchar *source_file, const gchar *dest_file)
{
  gboolean rc;
  GFile *sfile, *dfile;
  GError *error;

  sfile = g_file_new_for_path (source_file);
  dfile = g_file_new_for_path (dest_file);
  error = NULL;

  rc =
    g_file_move (sfile, dfile, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error);
  if (!rc)
    {
      g_warning ("%s: g_file_move(%s, %s) failed - %s\n", __func__, source_file,
                 dest_file, error->message);
      g_error_free (error);
    }

  g_object_unref (sfile);
  g_object_unref (dfile);
  return rc;
}

/**
 * @brief Get the content of a file in base64 format.
 *
 * @param[in]  path     Path to file.
 *
 * @return Allocated nul-terminated string, NULL otherwise.
 */
char *
gvm_file_as_base64 (const char *path)
{
  GError *error = NULL;
  char *content, *encoded;
  gsize len;

  if (!g_file_get_contents (path, &content, &len, &error))
    {
      g_error_free (error);
      return NULL;
    }
  encoded = g_base64_encode ((guchar *) content, len);
  g_free (content);
  return encoded;
}

/**
 * @brief Generates a file name for exporting.
 *
 * @param[in]   fname_format      Format string.
 * @param[in]   username          Current user name.
 * @param[in]   type              Type of resource.
 * @param[in]   uuid              UUID of resource.
 * @param[in]   creation_iso_time     Creation time of resource in ISO format.
 * @param[in]   modification_iso_time Modification time of resource (ISO).
 * @param[in]   name              Name of resource.
 * @param[in]   format_name       Name of format plugin.
 *
 * @return The 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)
{
  time_t now;
  struct tm now_broken;
  gchar *now_date_str, *creation_date_str, *modification_date_str;
  gchar *now_time_str, *creation_time_str, *modification_time_str;
  struct tm creation_time, modification_time;
  gchar *creation_date_short, *modification_date_short;
  gchar *fname_point;
  GString *file_name_buf;
  int format_state = 0;
  char *ret;

  creation_date_str = NULL;
  modification_date_str = NULL;
  creation_time_str = NULL;
  modification_time_str = NULL;

  now = time (NULL);
  if (localtime_r (&now, &now_broken) == NULL)
    {
      g_warning ("%s: localtime failed", __func__);
    }
  now_date_str = g_strdup_printf ("%04d%02d%02d", (now_broken.tm_year + 1900),
                                  (now_broken.tm_mon + 1), now_broken.tm_mday);
  now_time_str = g_strdup_printf ("%02d%02d%02d", now_broken.tm_hour,
                                  now_broken.tm_min, now_broken.tm_sec);

  memset (&creation_time, 0, sizeof (struct tm));
  memset (&modification_time, 0, sizeof (struct tm));
  creation_date_short = NULL;
  modification_date_short = NULL;

  if (creation_iso_time && (strlen (creation_iso_time) >= 19))
    creation_date_short = g_strndup (creation_iso_time, 19);

  if (creation_date_short
      && (((ret = strptime (creation_date_short, "%Y-%m-%dT%H:%M:%S",
                            &creation_time))
           == NULL)
          || (strlen (ret) == 0)))
    {
      creation_date_str =
        g_strdup_printf ("%04d%02d%02d", (creation_time.tm_year + 1900),
                         (creation_time.tm_mon + 1), creation_time.tm_mday);
      creation_time_str =
        g_strdup_printf ("%02d%02d%02d", creation_time.tm_hour,
                         creation_time.tm_min, creation_time.tm_sec);
    }
  g_free (creation_date_short);

  if (modification_iso_time && (strlen (modification_iso_time) >= 19))
    modification_date_short = g_strndup (modification_iso_time, 19);

  if (modification_date_short
      && (((ret = strptime (modification_date_short, "%Y-%m-%dT%H:%M:%S",
                            &modification_time))
           == NULL)
          || (strlen (ret) == 0)))
    {
      modification_date_str = g_strdup_printf (
        "%04d%02d%02d", (modification_time.tm_year + 1900),
        (modification_time.tm_mon + 1), modification_time.tm_mday);

      modification_time_str =
        g_strdup_printf ("%02d%02d%02d", modification_time.tm_hour,
                         modification_time.tm_min, modification_time.tm_sec);
    }
  g_free (modification_date_short);

  if (creation_date_str == NULL)
    creation_date_str = g_strdup (now_date_str);
  if (modification_date_str == NULL)
    modification_date_str = g_strdup (creation_date_str);
  if (creation_time_str == NULL)
    creation_time_str = g_strdup (now_time_str);
  if (modification_time_str == NULL)
    modification_time_str = g_strdup (creation_time_str);

  file_name_buf = g_string_new ("");

  fname_point = (char *) fname_format;

  while (format_state >= 0 && *fname_point != '\0')
    {
      if (format_state == 0)
        {
          if (*fname_point == '%')
            format_state = 1;
          else if (*fname_point == '"')
            g_string_append (file_name_buf, "\\\"");
          else if (*fname_point <= ' ')
            g_string_append_c (file_name_buf, '_');
          else
            g_string_append_c (file_name_buf, *fname_point);
        }
      else if (format_state == 1)
        {
          format_state = 0;
          switch (*fname_point)
            {
            case 'C':
              g_string_append (file_name_buf, creation_date_str);
              break;
            case 'c':
              g_string_append (file_name_buf, creation_time_str);
              break;
            case 'd':
              g_string_append_printf (file_name_buf, "%02d",
                                      modification_time.tm_mday);
              break;
            case 'D':
              g_string_append (file_name_buf, now_date_str);
              break;
            case 'F':
              g_string_append (file_name_buf,
                               format_name ? format_name : "XML");
              break;
            case 'M':
              g_string_append (file_name_buf, modification_date_str);
              break;
            case 'm':
              g_string_append (file_name_buf, modification_time_str);
              break;
            case 'N':
              g_string_append (file_name_buf,
                               name ? name : (type ? type : "unnamed"));
              break;
            case 'o':
              g_string_append_printf (file_name_buf, "%02d",
                                      modification_time.tm_mon + 1);
              break;
            case 'T':
              g_string_append (file_name_buf, type ? type : "resource");
              break;
            case 't':
              g_string_append (file_name_buf, now_time_str);
              break;
            case 'U':
              g_string_append (file_name_buf, uuid ? uuid : "list");
              break;
            case 'u':
              g_string_append (file_name_buf, username ? username : "");
              break;
            case 'Y':
              g_string_append_printf (file_name_buf, "%04d",
                                      modification_time.tm_year + 1900);
              break;
            case '%':
              g_string_append_c (file_name_buf, '%');
              break;
            default:
              g_warning ("%s : Unknown file name format placeholder: %%%c.",
                         __func__, *fname_point);
              format_state = -1;
            }
        }
      fname_point += sizeof (char);
    }

  if (format_state || strcmp (file_name_buf->str, "") == 0)
    {
      g_warning ("%s : Invalid file name format", __func__);
      g_string_free (file_name_buf, TRUE);
      return NULL;
    }

  fname_point = file_name_buf->str;
  while (*fname_point != '\0')
    {
      if (*fname_point <= ' ')
        *fname_point = '_';
      fname_point++;
    }

  g_free (now_date_str);
  g_free (now_time_str);
  g_free (creation_date_str);
  g_free (creation_time_str);
  g_free (modification_date_str);
  g_free (modification_time_str);
  return g_string_free (file_name_buf, FALSE);
}