32 #include <glib/gstdio.h> 33 #include <glib/gtypes.h> 57 if (g_lstat (name, &sb))
59 g_warning (
"g_lstat(%s) failed - %s\n", name, g_strerror (errno));
63 return (S_ISDIR (sb.st_mode));
82 GDir *directory = g_dir_open (pathname, 0, &error);
84 if (directory == NULL)
86 g_warning (
"g_dir_open(%s) failed - %s\n", pathname, error->message);
93 const gchar *entry = NULL;
95 while ((entry = g_dir_read_name (directory)) && (ret == 0))
97 gchar *entry_path = g_build_filename (pathname, entry, NULL);
102 g_warning (
"Failed to remove %s from %s!", entry, pathname);
103 g_dir_close (directory);
107 g_dir_close (directory);
111 return g_remove (pathname);
128 GFile *sfile, *dfile;
131 sfile = g_file_new_for_path (source_file);
132 dfile = g_file_new_for_path (dest_file);
136 g_file_copy (sfile, dfile, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error);
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);
144 g_object_unref (sfile);
145 g_object_unref (dfile);
163 GFile *sfile, *dfile;
166 sfile = g_file_new_for_path (source_file);
167 dfile = g_file_new_for_path (dest_file);
171 g_file_move (sfile, dfile, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error);
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);
179 g_object_unref (sfile);
180 g_object_unref (dfile);
194 GError *error = NULL;
195 char *content, *encoded;
198 if (!g_file_get_contents (path, &content, &len, &error))
200 g_error_free (error);
203 encoded = g_base64_encode ((guchar *) content, len);
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)
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;
236 GString *file_name_buf;
237 int format_state = 0;
240 creation_date_str = NULL;
241 modification_date_str = NULL;
242 creation_time_str = NULL;
243 modification_time_str = NULL;
246 now_broken = localtime (&now);
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);
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;
258 if (creation_iso_time && (strlen (creation_iso_time) >= 19))
259 creation_date_short = g_strndup (creation_iso_time, 19);
261 if (creation_date_short
262 && (((ret = strptime (creation_date_short,
"%Y-%m-%dT%H:%M:%S",
265 || (strlen (ret) == 0)))
268 g_strdup_printf (
"%04d%02d%02d", (creation_time.tm_year + 1900),
269 (creation_time.tm_mon + 1), creation_time.tm_mday);
271 g_strdup_printf (
"%02d%02d%02d", creation_time.tm_hour,
272 creation_time.tm_min, creation_time.tm_sec);
275 if (modification_iso_time && (strlen (modification_iso_time) >= 19))
276 modification_date_short = g_strndup (modification_iso_time, 19);
278 if (modification_date_short
279 && (((ret = strptime (modification_date_short,
"%Y-%m-%dT%H:%M:%S",
282 || (strlen (ret) == 0)))
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);
288 modification_time_str =
289 g_strdup_printf (
"%02d%02d%02d", modification_time.tm_hour,
290 modification_time.tm_min, modification_time.tm_sec);
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);
302 file_name_buf = g_string_new (
"");
304 fname_point = (
char *) fname_format;
306 while (format_state >= 0 && *fname_point !=
'\0')
308 if (format_state == 0)
310 if (*fname_point ==
'%')
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,
'_');
317 g_string_append_c (file_name_buf, *fname_point);
319 else if (format_state == 1)
322 switch (*fname_point)
325 g_string_append (file_name_buf, creation_date_str);
328 g_string_append (file_name_buf, creation_time_str);
331 g_string_append_printf (file_name_buf,
"%02d",
332 modification_time.tm_mday);
335 g_string_append (file_name_buf, now_date_str);
338 g_string_append (file_name_buf,
339 format_name ? format_name :
"XML");
342 g_string_append (file_name_buf, modification_date_str);
345 g_string_append (file_name_buf, modification_time_str);
348 g_string_append (file_name_buf,
349 name ? name : (type ? type :
"unnamed"));
352 g_string_append_printf (file_name_buf,
"%02d",
353 modification_time.tm_mon + 1);
356 g_string_append (file_name_buf, type ? type :
"resource");
359 g_string_append (file_name_buf, now_time_str);
362 g_string_append (file_name_buf, uuid ? uuid :
"list");
365 g_string_append (file_name_buf, username ? username :
"");
368 g_string_append_printf (file_name_buf,
"%04d",
369 modification_time.tm_year + 1900);
372 g_string_append_c (file_name_buf,
'%');
375 g_warning (
"%s : Unknown file name format placeholder: %%%c.",
376 __FUNCTION__, *fname_point);
380 fname_point +=
sizeof (char);
383 if (format_state || strcmp (file_name_buf->str,
"") == 0)
385 g_warning (
"%s : Invalid file name format", __FUNCTION__);
386 g_string_free (file_name_buf, TRUE);
390 fname_point = file_name_buf->str;
391 while (*fname_point !=
'\0')
393 if (*fname_point <=
' ')
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);
gboolean gvm_file_move(const gchar *source_file, const gchar *dest_file)
Moves a source file into a destination file.
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_remove_recurse(const gchar *pathname)
Recursively removes files and directories.
int gvm_file_check_is_dir(const char *name)
Checks whether a file is a directory or not.
char * gvm_file_as_base64(const char *path)
Get the content of a file in base64 format.
Protos for file utility functions.
gboolean gvm_file_copy(const gchar *source_file, const gchar *dest_file)
Copies a source file into a destination file.