static void tvio_set_g_error_with_paths (GError **error, const gchar *format, ThunarVfsPath *source_path, ThunarVfsPath *target_path, GError *err) { gchar *source_display_name; gchar *target_display_name; gchar *s; /* determine a displayable variant of the source_path */ s = _thunar_vfs_path_is_local (source_path) ? thunar_vfs_path_dup_string (source_path) : thunar_vfs_path_dup_uri (source_path); source_display_name = g_filename_display_name (s); g_free (s); /* determine a displayable variant of the target_path */ s = _thunar_vfs_path_is_local (target_path) ? thunar_vfs_path_dup_string (target_path) : thunar_vfs_path_dup_uri (target_path); target_display_name = g_filename_display_name (s); g_free (s); /* generate a useful error message */ s = g_strdup_printf (format, source_display_name, target_display_name); g_set_error (error, err->domain, err->code, "%s: %s", s, err->message); g_free (s); /* cleanup */ g_free (target_display_name); g_free (source_display_name); }
static gboolean rename_file (const char *old_name, const char *new_name, GError **err) { errno = 0; if (g_rename (old_name, new_name) == -1) { int save_errno = errno; gchar *display_old_name = g_filename_display_name (old_name); gchar *display_new_name = g_filename_display_name (new_name); g_set_error (err, G_FILE_ERROR, g_file_error_from_errno (save_errno), _("Failed to rename file '%s' to '%s': g_rename() failed: %s"), display_old_name, display_new_name, g_strerror (save_errno)); g_free (display_old_name); g_free (display_new_name); return FALSE; } return TRUE; }
gboolean tilem_animation_save(TilemAnimation *anim, const char *fname, const char *type, char **option_keys, char **option_values, GError **err) { FILE *fp; char *dname; int errnum; GdkPixbuf *pb; gboolean status; byte palette[768]; int i; g_return_val_if_fail(TILEM_IS_ANIMATION(anim), FALSE); g_return_val_if_fail(fname != NULL, FALSE); g_return_val_if_fail(type != NULL, FALSE); g_return_val_if_fail(err == NULL || *err == NULL, FALSE); if (strcmp(type, "gif") != 0) { pb = gdk_pixbuf_animation_get_static_image (GDK_PIXBUF_ANIMATION(anim)); status = gdk_pixbuf_savev(pb, fname, type, option_keys, option_values, err); return status; } fp = g_fopen(fname, "wb"); if (!fp) { errnum = errno; dname = g_filename_display_name(fname); g_set_error(err, G_FILE_ERROR, g_file_error_from_errno(errnum), _("Failed to open '%s' for writing: %s"), dname, g_strerror(errnum)); g_free(dname); return FALSE; } for (i = 0; i < 256; i++) { palette[3 * i] = anim->palette[i] >> 16; palette[3 * i + 1] = anim->palette[i] >> 8; palette[3 * i + 2] = anim->palette[i]; } tilem_animation_write_gif(anim, palette, 256, fp); if (fclose(fp)) { errnum = errno; dname = g_filename_display_name(fname); g_set_error(err, G_FILE_ERROR, g_file_error_from_errno(errnum), _("Error while closing '%s': %s"), dname, g_strerror(errnum)); g_free(dname); return FALSE; } return TRUE; }
/* * ev_dir_ensure_exists: * @dir: the directory name * @mode: permissions to use when creating the directory * @error: a location to store a #GError * * Create @dir recursively with permissions @mode. * * Returns: %TRUE on success, or %FALSE on error with @error filled in */ static gboolean _ev_dir_ensure_exists (const gchar *dir, int mode, GError **error) { int errsv; char *display_name; g_return_val_if_fail (dir != NULL, FALSE); errno = 0; if (g_mkdir_with_parents (dir, mode) == 0) return TRUE; errsv = errno; if (errsv == EEXIST && g_file_test (dir, G_FILE_TEST_IS_DIR)) return TRUE; display_name = g_filename_display_name (dir); g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv), "Failed to create directory '%s': %s", display_name, g_strerror (errsv)); g_free (display_name); return FALSE; }
static FmBookmarkItem* new_item(char* line) { FmBookmarkItem* item = g_slice_new0(FmBookmarkItem); char* sep; sep = strchr(line, '\n'); if(sep) *sep = '\0'; sep = strchr(line, ' '); if(sep) *sep = '\0'; /* FIXME: this is no longer needed once fm_path_new can convert file:/// to / */ if(g_str_has_prefix(line, "file:/")) { char* fpath = g_filename_from_uri(line, NULL, NULL); item->path = fm_path_new(fpath); g_free(fpath); } else { /* FIXME: is unescape needed? */ item->path = fm_path_new(line); } if(sep) item->name = g_strdup(sep+1); else item->name = g_filename_display_name(item->path->name); return item; }
static gboolean get_contents_win32 (const gchar *filename, gchar **contents, gsize *length, GError **error) { FILE *f; gboolean retval; wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL); gchar *display_filename = g_filename_display_name (filename); f = _wfopen (wfilename, L"rb"); g_free (wfilename); if (f == NULL) { g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno), _("Failed to open file '%s': %s"), display_filename, g_strerror (errno)); g_free (display_filename); return FALSE; } retval = get_contents_stdio (display_filename, f, contents, length, error); g_free (display_filename); return retval; }
/** * gimp_filename_to_utf8: * @filename: The filename to be converted to UTF-8. * * Convert a filename in the filesystem's encoding to UTF-8 * temporarily. The return value is a pointer to a string that is * guaranteed to be valid only during the current iteration of the * main loop or until the next call to gimp_filename_to_utf8(). * * The only purpose of this function is to provide an easy way to pass * a filename in the filesystem encoding to a function that expects an * UTF-8 encoded filename. * * Return value: A temporarily valid UTF-8 representation of @filename. * This string must not be changed or freed. **/ const gchar * gimp_filename_to_utf8 (const gchar *filename) { /* Simpleminded implementation, but at least allocates just one copy * of each translation. Could check if already UTF-8, and if so * return filename as is. Could perhaps (re)use a suitably large * cyclic buffer, but then would have to verify that all calls * really need the return value just for a "short" time. */ static GHashTable *ht = NULL; gchar *filename_utf8; if (! filename) return NULL; if (! ht) ht = g_hash_table_new (g_str_hash, g_str_equal); filename_utf8 = g_hash_table_lookup (ht, filename); if (! filename_utf8) { filename_utf8 = g_filename_display_name (filename); g_hash_table_insert (ht, g_strdup (filename), filename_utf8); } return filename_utf8; }
static char * shell_util_get_file_display_name (GFile *file, gboolean use_fallback) { GFileInfo *info; char *ret; ret = NULL; info = g_file_query_info (file, "standard::display-name", G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, NULL); if (info) { ret = g_strdup (g_file_info_get_display_name (info)); g_object_unref (info); } if (!ret && use_fallback) { /* can happen with URI schemes non supported by gvfs */ char *basename; basename = g_file_get_basename (file); ret = g_filename_display_name (basename); g_free (basename); } return ret; }
gboolean vol_set_filename_text(gpointer data) { volume_t * vol = (volume_t *)data; AQUALUNG_MUTEX_LOCK(vol->wait_mutex); if (vol->slot) { char * utf8; AQUALUNG_MUTEX_LOCK(vol->thread_mutex); utf8 = g_filename_display_name(vol->item->file); AQUALUNG_MUTEX_UNLOCK(vol->thread_mutex); gtk_entry_set_text(GTK_ENTRY(vol->file_entry), utf8); gtk_editable_set_position(GTK_EDITABLE(vol->file_entry), -1); g_free(utf8); } AQUALUNG_COND_SIGNAL(vol->thread_wait); AQUALUNG_MUTEX_UNLOCK(vol->wait_mutex); return FALSE; }
static struct filename_representations *filename_representations_new(const char *raw, int type) { struct filename_representations *fr; char *display_pre; char *real_path; #ifdef G_OS_WIN32 DWORD buffer_length; gunichar2 *raw_utf16, *real_path_utf16; #endif fr = g_malloc(sizeof(struct filename_representations)); fr->type = type; fr->raw = g_strdup(raw); #ifdef G_OS_WIN32 raw_utf16 = g_utf8_to_utf16(fr->raw, -1, NULL, NULL, NULL); buffer_length = GetFullPathNameW(raw_utf16, 0, NULL, NULL); real_path_utf16 = g_malloc(buffer_length * sizeof(gunichar2)); GetFullPathNameW(raw_utf16, buffer_length, real_path_utf16, NULL); real_path = g_utf16_to_utf8(raw_utf16, -1, NULL, NULL, NULL); g_free(real_path_utf16); g_free(raw_utf16); #else real_path = realpath(fr->raw, NULL); #endif display_pre = g_filename_display_name(real_path); fr->collate_key = g_utf8_collate_key_for_filename(display_pre, -1); fr->display = g_strescape(display_pre, (const gchar *) strescape_exceptions); g_free(display_pre); g_free(real_path); return fr; }
void vfs_dir_load( VFSDir* dir ) { if ( G_LIKELY(dir->path) ) { dir->disp_path = g_filename_display_name( dir->path ); dir->flags = 0; /* FIXME: We should check access here! */ if( G_UNLIKELY( strcmp( dir->path, vfs_get_desktop_dir() ) == 0 ) ) dir->is_desktop = TRUE; else if( G_UNLIKELY( strcmp( dir->path, g_get_home_dir() ) == 0 ) ) dir->is_home = TRUE; if( G_UNLIKELY(is_dir_trash(dir->path)) ) { // g_free( dir->disp_path ); // dir->disp_path = g_strdup( _("Trash") ); dir->is_trash = TRUE; } if( G_UNLIKELY( is_dir_mount_point(dir->path)) ) dir->is_mount_point = TRUE; if( G_UNLIKELY( is_dir_remote(dir->path)) ) dir->is_remote = TRUE; if( G_UNLIKELY( is_dir_virtual(dir->path)) ) dir->is_virtual = TRUE; dir->task = vfs_async_task_new( (VFSAsyncFunc)vfs_dir_load_thread, dir ); g_signal_connect( dir->task, "finish", G_CALLBACK(on_list_task_finished), dir ); vfs_async_task_execute( dir->task ); } }
int main (int argc, char *argv[]) { int ret; #ifndef G_OS_WIN32 sigset_t sig_mask, old_mask; sigemptyset (&sig_mask); sigaddset (&sig_mask, SIGUSR1); if (sigprocmask (SIG_UNBLOCK, &sig_mask, &old_mask) == 0) { if (sigismember (&old_mask, SIGUSR1)) g_message ("SIGUSR1 was blocked, unblocking it"); } #endif dir = g_get_current_dir (); filename = g_build_filename (dir, "maptest", NULL); displayname = g_filename_display_name (filename); childname = g_build_filename (dir, "mapchild", NULL); if (argc > 1) ret = child_main (argc, argv); else ret = parent_main (argc, argv); g_free (childname); g_free (filename); g_free (displayname); g_free (dir); return ret; }
/** * g_mapped_file_new: * @filename: The path of the file to load, in the GLib filename encoding * @writable: whether the mapping should be writable * @error: return location for a #GError, or %NULL * * Maps a file into memory. On UNIX, this is using the mmap() function. * * If @writable is %TRUE, the mapped buffer may be modified, otherwise * it is an error to modify the mapped buffer. Modifications to the buffer * are not visible to other processes mapping the same file, and are not * written back to the file. * * Note that modifications of the underlying file might affect the contents * of the #GMappedFile. Therefore, mapping should only be used if the file * will not be modified, or if all modifications of the file are done * atomically (e.g. using g_file_set_contents()). * * If @filename is the name of an empty, regular file, the function * will successfully return an empty #GMappedFile. In other cases of * size 0 (e.g. device files such as /dev/null), @error will be set * to the #GFileError value #G_FILE_ERROR_INVAL. * * Return value: a newly allocated #GMappedFile which must be unref'd * with g_mapped_file_unref(), or %NULL if the mapping failed. * * Since: 2.8 */ GMappedFile * g_mapped_file_new (const gchar *filename, gboolean writable, GError **error) { GMappedFile *file; int fd; g_return_val_if_fail (filename != NULL, NULL); g_return_val_if_fail (!error || *error == NULL, NULL); fd = g_open (filename, (writable ? O_RDWR : O_RDONLY) | _O_BINARY, 0); if (fd == -1) { int save_errno = errno; gchar *display_filename = g_filename_display_name (filename); g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (save_errno), _("Failed to open file '%s': open() failed: %s"), display_filename, g_strerror (save_errno)); g_free (display_filename); return NULL; } file = mapped_file_new_from_fd (fd, writable, filename, error); close (fd); return file; }
char* _moo_file_view_create_folder_dialog (GtkWidget *parent, MooFolder *folder) { CreateFolderXml *xml; GtkWidget *dialog; char *text, *path, *new_folder_name = NULL; g_return_val_if_fail (MOO_IS_FOLDER (folder), NULL); xml = create_folder_xml_new (); dialog = GTK_WIDGET (xml->CreateFolder); moo_window_set_parent (dialog, parent); path = g_filename_display_name (_moo_folder_get_path (folder)); text = g_strdup_printf ("Create new folder in %s", path); gtk_label_set_text (xml->label, text); g_free (path); g_free (text); gtk_entry_set_text (GTK_ENTRY (xml->entry), "New Folder"); moo_entry_clear_undo (xml->entry); gtk_widget_show_all (dialog); gtk_widget_grab_focus (GTK_WIDGET (xml->entry)); gtk_editable_select_region (GTK_EDITABLE (xml->entry), 0, -1); if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK) new_folder_name = g_strdup (gtk_entry_get_text (GTK_ENTRY (xml->entry))); else new_folder_name = NULL; gtk_widget_destroy (dialog); return new_folder_name; }
/* * Note: filename is fs encoded, not UTF-8. */ char * dialog_get_password (GtkWindow *parent, const char *filename) { char *res = NULL; char *str; char *dispname; char *primary; char *secondary; GtkWidget *d, *hb, *vb, *pwb, *image, *label, *entry; dispname = g_filename_display_name (filename); primary = g_strdup_printf (_("%s is encrypted"), dispname); g_free (dispname); secondary = _("Encrypted files require a password\nbefore they can be opened."); label = gtk_label_new (NULL); str = g_strdup_printf ("<span weight=\"bold\" size=\"larger\">" "%s</span>\n\n%s", primary, secondary); gtk_label_set_markup (GTK_LABEL (label), str); g_free (primary); g_free (str); gtk_label_set_selectable (GTK_LABEL (label), TRUE); d = gtk_dialog_new_with_buttons ("", parent, GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL); gtk_window_set_resizable (GTK_WINDOW (d), FALSE); hb = gtk_hbox_new (FALSE, 0); gtk_box_pack_start (GTK_BOX (GTK_DIALOG (d)->vbox), hb, TRUE, TRUE, 8); image = gtk_image_new_from_stock ("Gnumeric_Protection_Yes_Dialog", GTK_ICON_SIZE_DIALOG); gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0.0); gtk_box_pack_start (GTK_BOX (hb), image, FALSE, FALSE, 0); vb = gtk_vbox_new (FALSE, 8); gtk_box_pack_start (GTK_BOX (hb), vb, TRUE, TRUE, 6); gtk_box_pack_start (GTK_BOX (vb), label, TRUE, TRUE, 0); pwb = gtk_hbox_new (FALSE, 8); /* Strange width so that width of primary/secondary text will win. */ entry = g_object_new (GTK_TYPE_ENTRY, "visibility", FALSE, "width-request", 1, NULL); gtk_box_pack_start (GTK_BOX (pwb), gtk_label_new (_("Password :"******"activate", G_CALLBACK (cb_accept_password), d); if (gtk_dialog_run (GTK_DIALOG (d)) == GTK_RESPONSE_ACCEPT) res = g_strdup (gtk_entry_get_text (GTK_ENTRY (entry))); gtk_widget_destroy (d); return res; }
// FIXME_pcm: maybe we can support different encoding for different mount points? char *fm_path_display_basename (FmPath *path) { if (G_UNLIKELY (!path->parent)) // root_path element { if (!fm_path_is_native (path) && fm_path_is_virtual (path)) { if (fm_path_is_root (path) && fm_path_is_trash (path)) return g_strdup (_("Trash Can")); //~ if (g_str_has_prefix (path->name, "computer:/")) //~ return g_strdup (_("My Computer")); if (fm_path_is_computer (path)) return g_strdup (_("My Computer")); if (g_str_has_prefix (path->name, "menu:/")) { // FIXME_pcm: this should be more flexible const char *p = path->name + 5; while (p[0] == '/') ++p; if (g_str_has_prefix (p, "applications.menu")) return g_strdup (_("Applications")); } if (g_str_has_prefix (path->name, "network:/")) return g_strdup (_("Network")); } } return g_filename_display_name (path->name); }
static gboolean get_contents_win32 (const gchar *filename, gchar **contents, gsize *length, GError **error) { FILE *f; gboolean retval; gchar *display_filename = g_filename_display_name (filename); int save_errno; f = g_fopen (filename, "rb"); save_errno = errno; if (f == NULL) { g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (save_errno), _("Failed to open file '%s': %s"), display_filename, g_strerror (save_errno)); g_free (display_filename); return FALSE; } retval = get_contents_stdio (display_filename, f, contents, length, error); g_free (display_filename); return retval; }
static int gsf_list (int argc, char **argv) { int i; for (i = 0; i < argc; i++) { char const *filename = argv[i]; char *display_name; GsfInfile *infile = open_archive (filename); if (!infile) return 1; if (i > 0) g_print ("\n"); display_name = g_filename_display_name (filename); g_print ("%s:\n", display_name); g_free (display_name); ls_R (GSF_INPUT (infile), NULL); g_object_unref (infile); } return 0; }
static MMPlugin * load_plugin (const gchar *path) { MMPlugin *plugin = NULL; GModule *module; MMPluginCreateFunc plugin_create_func; gint *major_plugin_version; gint *minor_plugin_version; gchar *path_display; /* Get printable UTF-8 string of the path */ path_display = g_filename_display_name (path); module = g_module_open (path, G_MODULE_BIND_LAZY); if (!module) { g_warning ("Could not load plugin '%s': %s", path_display, g_module_error ()); goto out; } if (!g_module_symbol (module, "mm_plugin_major_version", (gpointer *) &major_plugin_version)) { g_warning ("Could not load plugin '%s': Missing major version info", path_display); goto out; } if (*major_plugin_version != MM_PLUGIN_MAJOR_VERSION) { g_warning ("Could not load plugin '%s': Plugin major version %d, %d is required", path_display, *major_plugin_version, MM_PLUGIN_MAJOR_VERSION); goto out; } if (!g_module_symbol (module, "mm_plugin_minor_version", (gpointer *) &minor_plugin_version)) { g_warning ("Could not load plugin '%s': Missing minor version info", path_display); goto out; } if (*minor_plugin_version != MM_PLUGIN_MINOR_VERSION) { g_warning ("Could not load plugin '%s': Plugin minor version %d, %d is required", path_display, *minor_plugin_version, MM_PLUGIN_MINOR_VERSION); goto out; } if (!g_module_symbol (module, "mm_plugin_create", (gpointer *) &plugin_create_func)) { g_warning ("Could not load plugin '%s': %s", path_display, g_module_error ()); goto out; } plugin = (*plugin_create_func) (); if (plugin) { g_object_weak_ref (G_OBJECT (plugin), (GWeakNotify) g_module_close, module); } else mm_warn ("Could not load plugin '%s': initialization failed", path_display); out: if (module && !plugin) g_module_close (module); g_free (path_display); return plugin; }
static void test_child_private (gchar *argv0) { GError *error = NULL; GMappedFile *map; gchar *buffer; gsize len; gchar *child_argv[3]; GPid child_pid; #ifdef G_OS_WIN32 g_remove ("STOP"); g_assert (!g_file_test ("STOP", G_FILE_TEST_EXISTS)); #endif write_or_die (filename, "ABC", -1); map = map_or_die (filename, TRUE); child_argv[0] = argv0; child_argv[1] = "mapchild"; child_argv[2] = NULL; if (!g_spawn_async (dir, child_argv, NULL, 0, NULL, NULL, &child_pid, &error)) { g_print ("failed to spawn child: %s\n", error->message); exit (1); } /* give the child some time to set up its mapping */ g_usleep (2000000); buffer = (gchar *)g_mapped_file_get_contents (map); buffer[0] = '1'; buffer[1] = '2'; buffer[2] = '3'; g_mapped_file_free (map); #ifndef G_OS_WIN32 kill (child_pid, SIGUSR1); #else g_file_set_contents ("STOP", "Hey there\n", -1, NULL); #endif /* give the child some time to write the file */ g_usleep (2000000); if (!g_file_get_contents (childname, &buffer, &len, &error)) { gchar *name; name = g_filename_display_name (childname); g_print ("failed to read '%s': %s\n", name, error->message); exit (1); } g_assert (len == 3); g_assert (strcmp (buffer, "ABC") == 0); g_free (buffer); }
static void _refresh_name(GtkWidget * widget, char const * filename) { gchar * gfilename; gfilename = g_filename_display_name(filename); gtk_entry_set_text(GTK_ENTRY(widget), gfilename); g_free(gfilename); }
/** * This function absorbs/freeś path, so this is no longer available afterwards. */ static void read_desktop_file ( DRunModePrivateData *pd, char *path, const char *filename ) { for ( unsigned int index = 0; index < pd->history_length; index++ ) { if ( g_strcmp0 ( path, pd->entry_list[index].path ) == 0 ) { g_free ( path ); return; } } GKeyFile *kf = g_key_file_new (); GError *error = NULL; g_key_file_load_from_file ( kf, path, 0, &error ); // If error, skip to next entry if ( error != NULL ) { g_error_free ( error ); g_key_file_free ( kf ); g_free ( path ); return; } // Skip hidden entries. if ( g_key_file_has_key ( kf, "Desktop Entry", "Hidden", NULL ) ) { if ( g_key_file_get_boolean ( kf, "Desktop Entry", "Hidden", NULL ) ) { g_key_file_free ( kf ); g_free ( path ); return; } } // Skip entries that have NoDisplay set. if ( g_key_file_has_key ( kf, "Desktop Entry", "NoDisplay", NULL ) ) { if ( g_key_file_get_boolean ( kf, "Desktop Entry", "NoDisplay", NULL ) ) { g_key_file_free ( kf ); g_free ( path ); return; } } if ( g_key_file_has_key ( kf, "Desktop Entry", "Exec", NULL ) ) { size_t nl = ( ( pd->cmd_list_length ) + 1 ); pd->entry_list = g_realloc ( pd->entry_list, nl * sizeof ( *( pd->entry_list ) ) ); pd->entry_list[pd->cmd_list_length].path = path; pd->entry_list[pd->cmd_list_length].terminal = FALSE; if ( g_key_file_has_key ( kf, "Desktop Entry", "Name", NULL ) ) { gchar *n = g_key_file_get_locale_string ( kf, "Desktop Entry", "Name", NULL, NULL ); gchar *gn = g_key_file_get_locale_string ( kf, "Desktop Entry", "GenericName", NULL, NULL ); pd->entry_list[pd->cmd_list_length].name = n; pd->entry_list[pd->cmd_list_length].generic_name = gn; } else { pd->entry_list[pd->cmd_list_length].name = g_filename_display_name ( filename ); pd->entry_list[pd->cmd_list_length].generic_name = NULL; } pd->entry_list[pd->cmd_list_length].exec = g_key_file_get_string ( kf, "Desktop Entry", "Exec", NULL ); if ( g_key_file_has_key ( kf, "Desktop Entry", "Terminal", NULL ) ) { pd->entry_list[pd->cmd_list_length].terminal = g_key_file_get_boolean ( kf, "Desktop Entry", "Terminal", NULL ); } ( pd->cmd_list_length )++; } g_key_file_free ( kf ); }
/** * file_copy: * @name: a pointer to a %NULL-terminated string, that names * the file to be copied, in the GLib file name encoding * @dest_name: a pointer to a %NULL-terminated string, that is the * name for the destination file, in the GLib file name encoding * @error: return location for a #GError, or %NULL * * Copies file @name to @dest_name. * * If the call was successful, it returns %TRUE. If the call was not * successful, it returns %FALSE and sets @error. The error domain * is #G_FILE_ERROR. Possible error * codes are those in the #GFileError enumeration. * * Return value: %TRUE on success, %FALSE otherwise. */ static gboolean file_copy (const gchar *name, const gchar *dest_name, GError **error) { gchar *contents; gsize length; gchar *dest_dir; /* FIXME - Paolo (Aug. 13, 2007): * Since the style scheme files are relatively small, we can implement * file copy getting all the content of the source file in a buffer and * then write the content to the destination file. In this way we * can use the g_file_get_contents and g_file_set_contents and avoid to * write custom code to copy the file (with sane error management). * If needed we can improve this code later. */ g_return_val_if_fail (name != NULL, FALSE); g_return_val_if_fail (dest_name != NULL, FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); /* Note: we allow to copy a file to itself since this is not a problem * in our use case */ /* Ensure the destination directory exists */ dest_dir = g_path_get_dirname (dest_name); errno = 0; if (g_mkdir_with_parents (dest_dir, 0755) != 0) { gint save_errno = errno; gchar *display_filename = g_filename_display_name (dest_dir); g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (save_errno), _("Directory '%s' could not be created: g_mkdir_with_parents() failed: %s"), display_filename, g_strerror (save_errno)); g_free (dest_dir); g_free (display_filename); return FALSE; } g_free (dest_dir); if (!g_file_get_contents (name, &contents, &length, error)) return FALSE; if (!g_file_set_contents (dest_name, contents, length, error)) return FALSE; g_free (contents); return TRUE; }
/** * epc_tls_certificate_save: * @certificate: a X.509 certificate * @filename: name of a file to write the certificate to, in the GLib file name encoding * @error: return location for a #GError, or %NULL * * Writes a PEM encoded X.509 certificate. * * If the call was successful, it returns %TRUE. If the call was not * successful, it returns %FALSE and sets @error. The error domain is * #EPC_TLS_ERROR. Error codes are taken from the <citetitle>GNU * TLS</citetitle> library. * * Returns: %TRUE on successful, %FALSE if an error occurred */ gboolean epc_tls_certificate_save (gnutls_x509_crt_t certificate, const gchar *filename, GError **error) { gint rc = GNUTLS_E_SUCCESS; gchar *display_name = NULL; guchar *contents = NULL; gchar *dirname = NULL; gsize length = 0; g_return_val_if_fail (NULL != certificate, FALSE); g_return_val_if_fail (NULL != filename, FALSE); if (EPC_DEBUG_LEVEL (1)) g_debug ("%s: Writing server certificate `%s'", G_STRLOC, filename); rc = gnutls_x509_crt_export (certificate, GNUTLS_X509_FMT_PEM, NULL, &length); g_assert (GNUTLS_E_SHORT_MEMORY_BUFFER == rc); contents = g_malloc (length); if (GNUTLS_E_SUCCESS != (rc = gnutls_x509_crt_export (certificate, GNUTLS_X509_FMT_PEM, contents, &length))) { g_set_error (error, EPC_TLS_ERROR, rc, _("Cannot export server certificate to PEM format: %s"), gnutls_strerror (rc)); goto out; } if (g_mkdir_with_parents (dirname = g_path_get_dirname (filename), 0700)) { display_name = g_filename_display_name (dirname); rc = GNUTLS_E_FILE_ERROR; g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno), _("Failed to create server certificate folder '%s': %s"), display_name, g_strerror (errno)); goto out; } if (!g_file_set_contents (filename, (gchar*)contents, length, error)) rc = GNUTLS_E_FILE_ERROR; out: g_free (display_name); g_free (contents); g_free (dirname); return (GNUTLS_E_SUCCESS == rc); }
void _gtk_label_set_filename_text (GtkLabel *label, const char *text) { char *utf8_text; utf8_text = g_filename_display_name (text); gtk_label_set_text (label, utf8_text); g_free (utf8_text); }
static void aod_update_option_list (LoadOptionsDialogData *aod_data) { GtkListStore *list_store = GTK_LIST_STORE (aod_data->aod_model); GFile *options_dir; GFileEnumerator *file_enum; GFileInfo *info; GError *err = NULL; gtk_list_store_clear (list_store); options_dir = get_user_config_subdirectory (ADD_FOLDER_OPTIONS_DIR, TRUE); make_directory_tree (options_dir, 0700, NULL); file_enum = g_file_enumerate_children (options_dir, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &err); if (err != NULL) { g_warning ("Failed to enumerate children: %s", err->message); g_clear_error (&err); g_object_unref (options_dir); return; } while ((info = g_file_enumerator_next_file (file_enum, NULL, &err)) != NULL) { const char *name; char *display_name; GtkTreeIter iter; if (err != NULL) { g_warning ("Failed to get info while enumerating: %s", err->message); g_clear_error (&err); continue; } name = g_file_info_get_name (info); display_name = g_filename_display_name (name); gtk_list_store_append (GTK_LIST_STORE (aod_data->aod_model), &iter); gtk_list_store_set (GTK_LIST_STORE (aod_data->aod_model), &iter, 0, name, 1, display_name, -1); g_free (display_name); g_object_unref (info); } if (err != NULL) { g_warning ("Failed to get info after enumeration: %s", err->message); g_clear_error (&err); } g_object_unref (options_dir); }
static int gsf_dump (int argc, char **argv, gboolean hex) { char const *filename; GsfInfile *infile; int i; int res = 0; if (argc < 2) return 1; filename = argv[0]; infile = open_archive (filename); if (!infile) return 1; for (i = 1; i < argc; i++) { char const *name = argv[i]; GsfInput *member = find_member (infile, name); if (!member) { char *display_name = g_filename_display_name (name); g_print ("%s: archive has no member %s\n", g_get_prgname (), display_name); g_free (display_name); res = 1; break; } if (hex) { char *display_name = g_filename_display_name (name); g_print ("%s:\n", display_name); g_free (display_name); } gsf_input_dump (member, hex); g_object_unref (member); } g_object_unref (infile); return res; }
GFileOutputStream * _g_local_file_output_stream_create (const char *filename, gboolean readable, GFileCreateFlags flags, GCancellable *cancellable, GError **error) { GLocalFileOutputStream *stream; int mode; int fd; int open_flags; if (g_cancellable_set_error_if_cancelled (cancellable, error)) return NULL; if (flags & G_FILE_CREATE_PRIVATE) mode = 0600; else mode = 0666; open_flags = O_CREAT | O_EXCL | O_BINARY; if (readable) open_flags |= O_RDWR; else open_flags |= O_WRONLY; fd = g_open (filename, open_flags, mode); if (fd == -1) { int errsv = errno; if (errsv == EINVAL) /* This must be an invalid filename, on e.g. FAT */ g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_FILENAME, _("Invalid filename")); else { char *display_name = g_filename_display_name (filename); g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv), _("Error opening file '%s': %s"), display_name, g_strerror (errsv)); g_free (display_name); } return NULL; } stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL); stream->priv->fd = fd; return G_FILE_OUTPUT_STREAM (stream); }
static gboolean remove_share (const char *path, GError **error) { ShareInfo *old_info; char *argv[2]; GError *real_error; /* g_message ("remove_share() start"); */ if (throw_error_on_remove) { g_set_error (error, SHARES_ERROR, SHARES_ERROR_FAILED, "Failed"); g_message ("remove_share() end FAIL"); return FALSE; } old_info = lookup_share_by_path (path); if (!old_info) { char *display_name; display_name = g_filename_display_name (path); g_set_error (error, SHARES_ERROR, SHARES_ERROR_NONEXISTENT, _("Cannot remove the share for path %s: that path is not shared"), display_name); g_free (display_name); g_message ("remove_share() end FAIL: path %s was not in our hashes", path); return FALSE; } argv[0] = "delete"; argv[1] = old_info->share_name; real_error = NULL; if (!net_usershare_run (G_N_ELEMENTS (argv), argv, NULL, &real_error)) { g_message ("Called \"net usershare delete\" but it failed: %s", real_error->message); g_propagate_error (error, real_error); g_message ("remove_share() end FAIL"); return FALSE; } remove_share_info_from_hashes (old_info); shares_free_share_info (old_info); /* g_message ("remove_share() end SUCCESS"); */ return TRUE; }
static void clipboard_get_data ( GtkClipboard *clipboard, GtkSelectionData *selection_data, guint info, gpointer user_data ) { GdkAtom uri_list_target = gdk_atom_intern( "text/uri-list", FALSE ); GdkAtom gnome_target = gdk_atom_intern( "x-special/gnome-copied-files", FALSE ); GList* l; gchar* file_name; gchar* action; gboolean use_uri = FALSE; GString* list; if ( ! clipboard_file_list ) return ; list = g_string_sized_new( 8192 ); if ( selection_data->target == gnome_target ) { action = clipboard_action == GDK_ACTION_MOVE ? "cut\n" : "copy\n"; g_string_append( list, action ); use_uri = TRUE; } else if ( selection_data->target == uri_list_target ) use_uri = TRUE; for ( l = clipboard_file_list; l; l = l->next ) { if ( use_uri ) { file_name = g_filename_to_uri( ( char* ) l->data, NULL, NULL ); } else { file_name = g_filename_display_name( ( char* ) l->data ); } g_string_append( list, file_name ); g_free( file_name ); if ( selection_data->target != uri_list_target ) g_string_append_c( list, '\n' ); else g_string_append( list, "\r\n" ); } gtk_selection_data_set ( selection_data, selection_data->target, 8, ( guchar* ) list->str, list->len + 1 ); /* g_debug( "clipboard data:\n%s\n\n", list->str ); */ g_string_free( list, TRUE ); }