Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Towards gvfs-fuse independence #963

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion plugins/export.c
Expand Up @@ -325,7 +325,7 @@ static void on_file_save_dialog_response(GtkDialog *dialog, gint response, gpoin
utf8_filename = utils_get_utf8_from_locale(new_filename);

/* check if file exists and ask whether to overwrite or not */
if (g_file_test(new_filename, G_FILE_TEST_EXISTS))
if (utils_file_exists(new_filename))
{
if (dialogs_show_question(
_("The file '%s' already exists. Do you want to overwrite it?"),
Expand Down
6 changes: 3 additions & 3 deletions plugins/filebrowser.c
Expand Up @@ -230,7 +230,7 @@ static void add_item(const gchar *name)
/* root directory doesn't need separator */
sep = (utils_str_equal(current_dir, "/")) ? "" : G_DIR_SEPARATOR_S;
fname = g_strconcat(current_dir, sep, name, NULL);
dir = g_file_test(fname, G_FILE_TEST_IS_DIR);
dir = utils_file_is_dir(fname);
utf8_fullname = utils_get_utf8_from_locale(fname);
utf8_name = utils_get_utf8_from_locale(name);
g_free(fname);
Expand Down Expand Up @@ -319,7 +319,7 @@ static void refresh(void)
GSList *list, *node;

/* don't clear when the new path doesn't exist */
if (! g_file_test(current_dir, G_FILE_TEST_EXISTS))
if (! utils_file_exists(current_dir))
return;

clear();
Expand Down Expand Up @@ -1188,7 +1188,7 @@ static void save_settings(void)
g_key_file_set_boolean(config, "filebrowser", "fb_set_project_base_path",
fb_set_project_base_path);

if (! g_file_test(config_dir, G_FILE_TEST_IS_DIR) && utils_mkdir(config_dir, TRUE) != 0)
if (! utils_file_is_dir(config_dir) && utils_mkdir(config_dir, TRUE) != 0)
{
dialogs_show_msgbox(GTK_MESSAGE_ERROR,
_("Plugin configuration directory could not be created."));
Expand Down
2 changes: 1 addition & 1 deletion plugins/htmlchars.c
Expand Up @@ -360,7 +360,7 @@ static void set_status(gboolean new_status)
g_key_file_set_boolean(config, "general", "replacement_on_typing_active",
plugin_active);

if (!g_file_test(config_dir, G_FILE_TEST_IS_DIR)
if (!utils_file_is_dir(config_dir)
&& utils_mkdir(config_dir, TRUE) != 0)
{
dialogs_show_msgbox(GTK_MESSAGE_ERROR,
Expand Down
6 changes: 3 additions & 3 deletions plugins/saveactions.c
Expand Up @@ -103,8 +103,8 @@ static gboolean backupcopy_set_backup_dir(const gchar *utf8_dir)
tmp = utils_get_locale_from_utf8(utf8_dir);

if (! g_path_is_absolute(tmp) ||
! g_file_test(tmp, G_FILE_TEST_EXISTS) ||
! g_file_test(tmp, G_FILE_TEST_IS_DIR))
! utils_file_exists(tmp) ||
! utils_file_is_dir(tmp))
{
g_free(tmp);
return FALSE;
Expand Down Expand Up @@ -532,7 +532,7 @@ static void configure_response_cb(GtkDialog *dialog, gint response, G_GNUC_UNUSE
}


if (! g_file_test(config_dir, G_FILE_TEST_IS_DIR) && utils_mkdir(config_dir, TRUE) != 0)
if (! utils_file_is_dir(config_dir) && utils_mkdir(config_dir, TRUE) != 0)
{
dialogs_show_msgbox(GTK_MESSAGE_ERROR,
_("Plugin configuration directory could not be created."));
Expand Down
4 changes: 2 additions & 2 deletions src/build.c
Expand Up @@ -801,8 +801,8 @@ static gchar *prepare_run_cmd(GeanyDocument *doc, gchar **working_dir, guint cmd
working_dir_utf8 = build_replace_placeholder(doc, cmd_working_dir);
*working_dir = utils_get_locale_from_utf8(working_dir_utf8);

if (EMPTY(*working_dir) || ! g_file_test(*working_dir, G_FILE_TEST_EXISTS) ||
! g_file_test(*working_dir, G_FILE_TEST_IS_DIR))
if (EMPTY(*working_dir) || ! utils_file_exists(*working_dir) ||
! utils_file_is_dir(*working_dir))
{
ui_set_statusbar(TRUE, _("Invalid working directory \"%s\""),
!EMPTY(working_dir_utf8) ? working_dir_utf8 : "<NULL>" );
Expand Down
8 changes: 4 additions & 4 deletions src/callbacks.c
Expand Up @@ -1388,7 +1388,7 @@ void on_menu_open_selected_file1_activate(GtkMenuItem *menuitem, gpointer user_d

filename = g_build_path(G_DIR_SEPARATOR_S, path, sel, NULL);

if (! g_file_test(filename, G_FILE_TEST_EXISTS) &&
if (! utils_file_exists(filename) &&
app->project != NULL && !EMPTY(app->project->base_path))
{
/* try the project's base path */
Expand All @@ -1398,15 +1398,15 @@ void on_menu_open_selected_file1_activate(GtkMenuItem *menuitem, gpointer user_d
}
g_free(path);
#ifdef G_OS_UNIX
if (! g_file_test(filename, G_FILE_TEST_EXISTS))
if (! utils_file_exists(filename))
SETPTR(filename, g_build_path(G_DIR_SEPARATOR_S, "/usr/local/include", sel, NULL));

if (! g_file_test(filename, G_FILE_TEST_EXISTS))
if (! utils_file_exists(filename))
SETPTR(filename, g_build_path(G_DIR_SEPARATOR_S, "/usr/include", sel, NULL));
#endif
}

if (g_file_test(filename, G_FILE_TEST_EXISTS))
if (utils_file_exists(filename))
document_open_file(filename, FALSE, NULL, NULL);
else
{
Expand Down
8 changes: 4 additions & 4 deletions src/dialogs.c
Expand Up @@ -150,13 +150,13 @@ static gboolean open_file_dialog_handle_response(GtkWidget *dialog, gint respons
if (filesel_state.open.encoding_idx >= 0 && filesel_state.open.encoding_idx < GEANY_ENCODINGS_MAX)
charset = encodings[filesel_state.open.encoding_idx].charset;

filelist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
filelist = gtk_file_chooser_get_uris(GTK_FILE_CHOOSER(dialog));
if (filelist != NULL)
{
const gchar *first = filelist->data;

// When there's only one filename it may have been typed manually
if (!filelist->next && !g_file_test(first, G_FILE_TEST_EXISTS))
if (!filelist->next && !utils_file_exists(first))
{
dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("\"%s\" was not found."), first);
ret = FALSE;
Expand Down Expand Up @@ -519,7 +519,7 @@ static gboolean save_as_dialog_handle_response(GtkWidget *dialog, gint response)
{
gboolean rename_file = FALSE;
gboolean success = FALSE;
gchar *new_filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
gchar *new_filename = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog));

switch (response)
{
Expand All @@ -530,7 +530,7 @@ static gboolean save_as_dialog_handle_response(GtkWidget *dialog, gint response)
utils_beep();
break;
}
if (g_file_test(new_filename, G_FILE_TEST_EXISTS) &&
if (utils_file_exists(new_filename) &&
!dialogs_show_question_full(NULL, NULL, NULL,
_("Overwrite?"),
_("Filename already exists!")))
Expand Down
131 changes: 36 additions & 95 deletions src/document.c
Expand Up @@ -82,9 +82,6 @@
#include <gdk/gdkkeysyms.h>


#define USE_GIO_FILE_OPERATIONS (!file_prefs.use_safe_file_saving && file_prefs.use_gio_unsafe_file_saving)


GeanyFilePrefs file_prefs;


Expand Down Expand Up @@ -593,10 +590,10 @@ static void monitor_file_setup(GeanyDocument *doc)
document_stop_file_monitoring(doc);

locale_filename = utils_get_locale_from_utf8(doc->file_name);
if (locale_filename != NULL && g_file_test(locale_filename, G_FILE_TEST_EXISTS))
if (locale_filename != NULL && utils_file_exists(locale_filename))
{
/* get a file monitor and connect to the 'changed' signal */
GFile *file = g_file_new_for_path(locale_filename);
GFile *file = utils_gfile_create(locale_filename);
doc->priv->monitor = g_file_monitor_file(file, G_FILE_MONITOR_NONE, NULL, NULL);
g_signal_connect(doc->priv->monitor, "changed",
G_CALLBACK(monitor_file_changed_cb), doc);
Expand Down Expand Up @@ -946,7 +943,7 @@ static gboolean get_mtime(const gchar *locale_filename, time_t *time)

if (USE_GIO_FILE_OPERATIONS)
{
GFile *file = g_file_new_for_path(locale_filename);
GFile *file = utils_gfile_create(locale_filename);
GFileInfo *info = g_file_query_info(file, G_FILE_ATTRIBUTE_TIME_MODIFIED, G_FILE_QUERY_INFO_NONE, NULL, &error);

if (info)
Expand Down Expand Up @@ -1003,15 +1000,7 @@ static gboolean load_text_file(const gchar *locale_filename, const gchar *displa
if (!get_mtime(locale_filename, &filedata->mtime))
return FALSE;

if (USE_GIO_FILE_OPERATIONS)
{
GFile *file = g_file_new_for_path(locale_filename);

g_file_load_contents(file, NULL, &filedata->data, &filedata->len, NULL, &err);
g_object_unref(file);
}
else
g_file_get_contents(locale_filename, &filedata->data, &filedata->len, &err);
utils_read_file(locale_filename, &filedata->data, &filedata->len, &err);

if (err)
{
Expand Down Expand Up @@ -1329,6 +1318,10 @@ GeanyDocument *document_open_file_full(GeanyDocument *doc, const gchar *filename
#else
locale_filename = g_strdup(filename);
#endif
SETPTR(locale_filename, utils_get_path_from_uri(locale_filename));
if (!locale_filename)
return NULL;

/* remove relative junk */
utils_tidy_path(locale_filename);

Expand Down Expand Up @@ -1763,17 +1756,40 @@ void document_rename_file(GeanyDocument *doc, const gchar *new_filename)
gchar *old_locale_filename = utils_get_locale_from_utf8(doc->file_name);
gchar *new_locale_filename = utils_get_locale_from_utf8(new_filename);
gint result;
gchar *msg;

/* stop file monitoring to avoid getting events for deleting/creating files,
* it's re-setup in document_save_file_as() */
document_stop_file_monitoring(doc);

result = g_rename(old_locale_filename, new_locale_filename);
if (result != 0)
if (USE_GIO_FILE_OPERATIONS)
{
GError *error = NULL;
GFile *old_file = utils_gfile_create(old_locale_filename);
GFile *new_file = utils_gfile_create(new_locale_filename);

g_file_move(old_file, new_file, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error);
if (error)
{
msg = g_strdup(error->message);
g_error_free(error);
}

g_object_unref(old_file);
g_object_unref(new_file);
}
else
{
dialogs_show_msgbox_with_secondary(GTK_MESSAGE_ERROR,
_("Error renaming file."), g_strerror(errno));
gint result = g_rename(old_locale_filename, new_locale_filename);
if (result != 0)
msg = g_strdup(g_strerror(errno));
}

if (msg)
dialogs_show_msgbox_with_secondary(GTK_MESSAGE_ERROR,
_("Error renaming file."), msg);

g_free(msg);
g_free(old_locale_filename);
g_free(new_locale_filename);
}
Expand Down Expand Up @@ -1942,82 +1958,7 @@ static gchar *write_data_to_disk(const gchar *locale_filename,
{
GError *error = NULL;

if (file_prefs.use_safe_file_saving)
{
/* Use old GLib API for safe saving (GVFS-safe, but alters ownership and permissons).
* This is the only option that handles disk space exhaustion. */
if (g_file_set_contents(locale_filename, data, len, &error))
geany_debug("Wrote %s with g_file_set_contents().", locale_filename);
}
else if (USE_GIO_FILE_OPERATIONS)
{
GFile *fp;

/* Use GIO API to save file (GVFS-safe)
* It is best in most GVFS setups but don't seem to work correctly on some more complex
* setups (saving from some VM to their host, over some SMB shares, etc.) */
fp = g_file_new_for_path(locale_filename);
g_file_replace_contents(fp, data, len, NULL, file_prefs.gio_unsafe_save_backup,
G_FILE_CREATE_NONE, NULL, NULL, &error);
g_object_unref(fp);
}
else
{
FILE *fp;
int save_errno;
gchar *display_name = g_filename_display_name(locale_filename);

/* Use POSIX API for unsafe saving (GVFS-unsafe) */
/* The error handling is taken from glib-2.26.0 gfileutils.c */
errno = 0;
fp = g_fopen(locale_filename, "wb");
if (fp == NULL)
{
save_errno = errno;

g_set_error(&error,
G_FILE_ERROR,
g_file_error_from_errno(save_errno),
_("Failed to open file '%s' for writing: fopen() failed: %s"),
display_name,
g_strerror(save_errno));
}
else
{
gsize bytes_written;

errno = 0;
bytes_written = fwrite(data, sizeof(gchar), len, fp);

if (len != bytes_written)
{
save_errno = errno;

g_set_error(&error,
G_FILE_ERROR,
g_file_error_from_errno(save_errno),
_("Failed to write file '%s': fwrite() failed: %s"),
display_name,
g_strerror(save_errno));
}

errno = 0;
/* preserve the fwrite() error if any */
if (fclose(fp) != 0 && error == NULL)
{
save_errno = errno;

g_set_error(&error,
G_FILE_ERROR,
g_file_error_from_errno(save_errno),
_("Failed to close file '%s': fclose() failed: %s"),
display_name,
g_strerror(save_errno));
}
}

g_free(display_name);
}
utils_write_file_full(locale_filename, data, len, &error);
if (error != NULL)
{
gchar *msg = g_strdup(error->message);
Expand Down
2 changes: 1 addition & 1 deletion src/editor.c
Expand Up @@ -276,7 +276,7 @@ void editor_snippets_init(void)
userconfigfile = g_build_filename(app->configdir, "snippets.conf", NULL);

/* check for old autocomplete.conf files (backwards compatibility) */
if (! g_file_test(userconfigfile, G_FILE_TEST_IS_REGULAR))
if (! utils_file_is_regular(userconfigfile))
SETPTR(userconfigfile, g_build_filename(app->configdir, "autocomplete.conf", NULL));

/* load the actual config files */
Expand Down
6 changes: 3 additions & 3 deletions src/highlighting.c
Expand Up @@ -513,7 +513,7 @@ static void load_named_styles(GKeyFile *config, GKeyFile *config_home)
path = g_build_path(G_DIR_SEPARATOR_S, app->datadir, GEANY_COLORSCHEMES_SUBDIR, scheme, NULL);
path_home = g_build_path(G_DIR_SEPARATOR_S, app->configdir, GEANY_COLORSCHEMES_SUBDIR, scheme, NULL);

if (g_file_test(path, G_FILE_TEST_EXISTS) || g_file_test(path_home, G_FILE_TEST_EXISTS))
if (utils_file_exists(path) || utils_file_exists(path_home))
{
config = utils_key_file_new(path);
config_home = utils_key_file_new(path_home);
Expand Down Expand Up @@ -1208,13 +1208,13 @@ static void on_color_scheme_changed(GtkTreeSelection *treesel, gpointer dummy)

/* fname is just the basename from the menu item, so prepend the custom files path */
path = g_build_path(G_DIR_SEPARATOR_S, app->configdir, GEANY_COLORSCHEMES_SUBDIR, fname, NULL);
if (!g_file_test(path, G_FILE_TEST_EXISTS))
if (!utils_file_exists(path))
{
/* try the system path */
g_free(path);
path = g_build_path(G_DIR_SEPARATOR_S, app->datadir, GEANY_COLORSCHEMES_SUBDIR, fname, NULL);
}
if (g_file_test(path, G_FILE_TEST_EXISTS))
if (utils_file_exists(path))
{
SETPTR(editor_prefs.color_scheme, fname);
fname = NULL;
Expand Down
4 changes: 2 additions & 2 deletions src/keybindings.c
Expand Up @@ -781,7 +781,7 @@ static void load_user_kb(void)
GKeyFile *config = g_key_file_new();

/* backwards compatibility with Geany 0.21 defaults */
if (!g_file_test(configfile, G_FILE_TEST_EXISTS))
if (!utils_file_exists(configfile))
{
gchar *geanyconf = g_build_filename(app->configdir, "geany.conf", NULL);
const gchar data[] = "[Bindings]\n"
Expand All @@ -792,7 +792,7 @@ static void load_user_kb(void)
"move_tableft=<Alt>Page_Up\n"
"move_tabright=<Alt>Page_Down\n";

utils_write_file(configfile, g_file_test(geanyconf, G_FILE_TEST_EXISTS) ?
utils_write_file(configfile, utils_file_exists(geanyconf) ?
data : "");
g_free(geanyconf);
}
Expand Down
4 changes: 2 additions & 2 deletions src/keyfile.c
Expand Up @@ -1123,7 +1123,7 @@ gboolean configuration_load(void)
gchar *configfile = g_build_filename(app->configdir, "geany.conf", NULL);
GKeyFile *config = g_key_file_new();

if (! g_file_test(configfile, G_FILE_TEST_IS_REGULAR))
if (! utils_file_is_regular(configfile))
{ /* config file does not (yet) exist, so try to load a global config file which may be */
/* created by distributors */
geany_debug("No user config file found, trying to use global configuration.");
Expand Down Expand Up @@ -1179,7 +1179,7 @@ static gboolean open_session_file(gchar **tmp, guint len)
if (len > 8)
line_breaking = atoi(tmp[8]);

if (g_file_test(locale_filename, G_FILE_TEST_IS_REGULAR))
if (utils_file_is_regular(locale_filename))
{
GeanyFiletype *ft = filetypes_lookup_by_name(ft_name);
GeanyDocument *doc = document_open_file_full(
Expand Down