43 #include <glib/gstdio.h> 64 if (g_lstat (
name, &sb))
66 g_warning (
"g_lstat(%s) failed - %s\n",
name, g_strerror (errno));
70 return (S_ISDIR (sb.st_mode));
92 GDir *directory = g_dir_open (pathname, 0, &error);
94 if (directory == NULL)
96 g_warning (
"g_dir_open(%s) failed - %s\n", pathname, error->message);
103 const gchar *entry = NULL;
105 while ((entry = g_dir_read_name (directory)) && (ret == 0))
107 gchar *entry_path = g_build_filename (pathname, entry, NULL);
112 g_warning (
"Failed to remove %s from %s!", entry, pathname);
113 g_dir_close (directory);
117 g_dir_close (directory);
121 return g_remove (pathname);
138 GFile *sfile, *dfile;
141 #if !GLIB_CHECK_VERSION(2, 35, 0) 144 sfile = g_file_new_for_path (source_file);
145 dfile = g_file_new_for_path (dest_file);
148 rc = g_file_copy (sfile, dfile, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL,
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);
157 g_object_unref (sfile);
158 g_object_unref (dfile);
176 GFile *sfile, *dfile;
179 #if !GLIB_CHECK_VERSION(2, 35, 0) 182 sfile = g_file_new_for_path (source_file);
183 dfile = g_file_new_for_path (dest_file);
186 rc = g_file_move (sfile, dfile, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL,
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);
195 g_object_unref (sfile);
196 g_object_unref (dfile);
210 GError *error = NULL;
211 char *content, *encoded;
214 if (!g_file_get_contents (path, &content, &len, &error))
216 g_error_free (error);
219 encoded = g_base64_encode ((guchar *) content, len);
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)
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;
252 GString *file_name_buf;
253 int format_state = 0;
256 creation_date_str = NULL;
257 modification_date_str = NULL;
258 creation_time_str = NULL;
259 modification_time_str = 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",
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;
277 if (creation_iso_time && (strlen (creation_iso_time) >= 19))
278 creation_date_short = g_strndup (creation_iso_time, 19);
280 if (creation_date_short
281 && (((ret = strptime (creation_date_short,
285 || (strlen (ret) == 0)))
288 = g_strdup_printf (
"%04d%02d%02d",
289 (creation_time.tm_year + 1900),
290 (creation_time.tm_mon + 1),
291 creation_time.tm_mday);
293 = g_strdup_printf (
"%02d%02d%02d",
294 creation_time.tm_hour,
295 creation_time.tm_min,
296 creation_time.tm_sec);
299 if (modification_iso_time && (strlen (modification_iso_time) >= 19))
300 modification_date_short = g_strndup (modification_iso_time, 19);
302 if (modification_date_short
303 && (((ret = strptime (modification_date_short,
307 || (strlen (ret) == 0)))
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);
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);
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);
331 file_name_buf = g_string_new (
"");
333 fname_point = (
char*) fname_format;
335 while (format_state >= 0 && *fname_point !=
'\0')
337 if (format_state == 0)
339 if (*fname_point ==
'%')
341 else if (*fname_point ==
'"')
342 g_string_append (file_name_buf,
"\\\"");
344 g_string_append_c (file_name_buf, *fname_point);
346 else if (format_state == 1)
349 switch (*fname_point)
352 g_string_append (file_name_buf, creation_date_str);
355 g_string_append (file_name_buf, creation_time_str);
358 g_string_append (file_name_buf, now_date_str);
361 g_string_append (file_name_buf,
362 format_name ? format_name :
"XML");
365 g_string_append (file_name_buf, modification_date_str);
368 g_string_append (file_name_buf, modification_time_str);
371 g_string_append (file_name_buf,
372 name ?
name : (type ? type :
"unnamed"));
375 g_string_append (file_name_buf, type ? type :
"resource");
378 g_string_append (file_name_buf, now_time_str);
381 g_string_append (file_name_buf, uuid ? uuid :
"list");
384 g_string_append (file_name_buf, username ? username :
"");
387 g_string_append_c (file_name_buf,
'%');
390 g_warning (
"%s : Unknown file name format placeholder: %%%c.",
391 __FUNCTION__, *fname_point);
395 fname_point +=
sizeof (char);
398 if (format_state || strcmp (file_name_buf->str,
"") == 0)
400 g_warning (
"%s : Invalid file name format", __FUNCTION__);
401 g_string_free (file_name_buf, TRUE);
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);
int openvas_file_check_is_dir(const char *name)
Checks whether a file is a directory or not.
gboolean openvas_file_move(const gchar *source_file, const gchar *dest_file)
Moves a source file into a destination file.
gboolean openvas_file_copy(const gchar *source_file, const gchar *dest_file)
Copies a source file into a destination file.
char * openvas_file_as_base64(const char *path)
Get the content of a file in base64 format.
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.
int openvas_file_remove_recurse(const gchar *pathname)
Recursively removes files and directories.