18#include <glib/gstdio.h>
19#include <glib/gtypes.h>
28#define G_LOG_DOMAIN "libgvm util"
49 if (g_lstat (name, &sb))
51 g_warning (
"g_lstat(%s) failed - %s\n", name, g_strerror (errno));
55 return S_ISDIR (sb.st_mode);
73 return eaccess (name, F_OK) == 0;
91 return eaccess (name, X_OK) == 0;
109 return eaccess (name, R_OK) == 0;
127 GError *error = NULL;
128 GDir *directory = g_dir_open (pathname, 0, &error);
130 if (directory == NULL)
132 g_warning (
"g_dir_open(%s) failed - %s\n", pathname, error->message);
133 g_error_free (error);
139 const gchar *entry = NULL;
141 while ((entry = g_dir_read_name (directory)) && (ret == 0))
143 gchar *entry_path = g_build_filename (pathname, entry, NULL);
148 g_warning (
"Failed to remove %s from %s!", entry, pathname);
149 g_dir_close (directory);
153 g_dir_close (directory);
157 return g_remove (pathname);
174 GFile *sfile, *dfile;
177 sfile = g_file_new_for_path (source_file);
178 dfile = g_file_new_for_path (dest_file);
182 g_file_copy (sfile, dfile, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error);
185 g_warning (
"%s: g_file_copy(%s, %s) failed - %s\n", __func__, source_file,
186 dest_file, error->message);
187 g_error_free (error);
190 g_object_unref (sfile);
191 g_object_unref (dfile);
209 GFile *sfile, *dfile;
212 sfile = g_file_new_for_path (source_file);
213 dfile = g_file_new_for_path (dest_file);
217 g_file_move (sfile, dfile, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error);
220 g_warning (
"%s: g_file_move(%s, %s) failed - %s\n", __func__, source_file,
221 dest_file, error->message);
222 g_error_free (error);
225 g_object_unref (sfile);
226 g_object_unref (dfile);
240 GError *error = NULL;
241 char *content, *encoded;
244 if (!g_file_get_contents (path, &content, &len, &error))
246 g_error_free (error);
249 encoded = g_base64_encode ((guchar *) content, len);
270 const char *type,
const char *uuid,
271 const char *creation_iso_time,
272 const char *modification_iso_time,
const char *name,
273 const char *format_name)
276 struct tm now_broken;
277 gchar *now_date_str, *creation_date_str, *modification_date_str;
278 gchar *now_time_str, *creation_time_str, *modification_time_str;
279 struct tm creation_time, modification_time;
280 gchar *creation_date_short, *modification_date_short;
282 GString *file_name_buf;
283 int format_state = 0;
286 creation_date_str = NULL;
287 modification_date_str = NULL;
288 creation_time_str = NULL;
289 modification_time_str = NULL;
292 if (localtime_r (&now, &now_broken) == NULL)
294 g_warning (
"%s: localtime failed", __func__);
296 now_date_str = g_strdup_printf (
"%04d%02d%02d", (now_broken.tm_year + 1900),
297 (now_broken.tm_mon + 1), now_broken.tm_mday);
298 now_time_str = g_strdup_printf (
"%02d%02d%02d", now_broken.tm_hour,
299 now_broken.tm_min, now_broken.tm_sec);
301 memset (&creation_time, 0,
sizeof (
struct tm));
302 memset (&modification_time, 0,
sizeof (
struct tm));
303 creation_date_short = NULL;
304 modification_date_short = NULL;
306 if (creation_iso_time && (strlen (creation_iso_time) >= 19))
307 creation_date_short = g_strndup (creation_iso_time, 19);
309 if (creation_date_short
310 && (((ret = strptime (creation_date_short,
"%Y-%m-%dT%H:%M:%S",
313 || (strlen (ret) == 0)))
316 g_strdup_printf (
"%04d%02d%02d", (creation_time.tm_year + 1900),
317 (creation_time.tm_mon + 1), creation_time.tm_mday);
319 g_strdup_printf (
"%02d%02d%02d", creation_time.tm_hour,
320 creation_time.tm_min, creation_time.tm_sec);
323 if (modification_iso_time && (strlen (modification_iso_time) >= 19))
324 modification_date_short = g_strndup (modification_iso_time, 19);
326 if (modification_date_short
327 && (((ret = strptime (modification_date_short,
"%Y-%m-%dT%H:%M:%S",
330 || (strlen (ret) == 0)))
332 modification_date_str = g_strdup_printf (
333 "%04d%02d%02d", (modification_time.tm_year + 1900),
334 (modification_time.tm_mon + 1), modification_time.tm_mday);
336 modification_time_str =
337 g_strdup_printf (
"%02d%02d%02d", modification_time.tm_hour,
338 modification_time.tm_min, modification_time.tm_sec);
341 if (creation_date_str == NULL)
342 creation_date_str = g_strdup (now_date_str);
343 if (modification_date_str == NULL)
344 modification_date_str = g_strdup (creation_date_str);
345 if (creation_time_str == NULL)
346 creation_time_str = g_strdup (now_time_str);
347 if (modification_time_str == NULL)
348 modification_time_str = g_strdup (creation_time_str);
350 file_name_buf = g_string_new (
"");
352 fname_point = (
char *) fname_format;
354 while (format_state >= 0 && *fname_point !=
'\0')
356 if (format_state == 0)
358 if (*fname_point ==
'%')
360 else if (*fname_point ==
'"')
361 g_string_append (file_name_buf,
"\\\"");
362 else if (*fname_point <=
' ')
363 g_string_append_c (file_name_buf,
'_');
365 g_string_append_c (file_name_buf, *fname_point);
367 else if (format_state == 1)
370 switch (*fname_point)
373 g_string_append (file_name_buf, creation_date_str);
376 g_string_append (file_name_buf, creation_time_str);
379 g_string_append_printf (file_name_buf,
"%02d",
380 modification_time.tm_mday);
383 g_string_append (file_name_buf, now_date_str);
386 g_string_append (file_name_buf,
387 format_name ? format_name :
"XML");
390 g_string_append (file_name_buf, modification_date_str);
393 g_string_append (file_name_buf, modification_time_str);
396 g_string_append (file_name_buf,
397 name ? name : (type ? type :
"unnamed"));
400 g_string_append_printf (file_name_buf,
"%02d",
401 modification_time.tm_mon + 1);
404 g_string_append (file_name_buf, type ? type :
"resource");
407 g_string_append (file_name_buf, now_time_str);
410 g_string_append (file_name_buf, uuid ? uuid :
"list");
413 g_string_append (file_name_buf, username ? username :
"");
416 g_string_append_printf (file_name_buf,
"%04d",
417 modification_time.tm_year + 1900);
420 g_string_append_c (file_name_buf,
'%');
423 g_warning (
"%s : Unknown file name format placeholder: %%%c.",
424 __func__, *fname_point);
428 fname_point +=
sizeof (char);
431 if (format_state || strcmp (file_name_buf->str,
"") == 0)
433 g_warning (
"%s : Invalid file name format", __func__);
434 g_string_free (file_name_buf, TRUE);
438 fname_point = file_name_buf->str;
439 while (*fname_point !=
'\0')
441 if (*fname_point <=
' ')
446 g_free (now_date_str);
447 g_free (creation_date_str);
448 g_free (creation_time_str);
449 g_free (modification_date_str);
450 return g_string_free (file_name_buf, FALSE);
int gvm_file_remove_recurse(const gchar *pathname)
Recursively removes files and directories.
int gvm_file_exists(const char *name)
Checks whether a file or directory exists.
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.
int gvm_file_check_is_dir(const char *name)
Checks whether a file is a directory or not.
gboolean gvm_file_move(const gchar *source_file, const gchar *dest_file)
Moves a source file into a destination file.
int gvm_file_is_readable(const char *name)
Checks whether a file or directory exists and is readable.
int gvm_file_is_executable(const char *name)
Checks whether a file or directory exists and is executable.
char * gvm_file_as_base64(const char *path)
Get the content of a file in base64 format.
gboolean gvm_file_copy(const gchar *source_file, const gchar *dest_file)
Copies a source file into a destination file.
Protos for file utility functions.