* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+#include "ui-fileops.h"
+
#include <cstdio>
#include <cstdlib>
#include <cstring>
+#include <memory>
+
#include <unistd.h>
#include <utime.h>
-#include "main.h"
-#include "ui-fileops.h"
+#include <config.h>
-#include "md5-util.h"
+#include "compat.h"
+#include "debug.h"
#include "filefilter.h"
+#include "intl.h"
#include "layout.h"
-#include "utilops.h"
+#include "main-defines.h"
+#include "md5-util.h"
#include "secure-save.h"
+#include "utilops.h"
/*
*-----------------------------------------------------------------------------
lc = getenv("LANG");
bf = getenv("G_BROKEN_FILENAMES");
- string = g_string_new("");
- g_string_append(string, _("One or more filenames are not encoded with the preferred locale character set.\n"));
+ string = g_string_new(_("One or more filenames are not encoded with the preferred locale character set.\n"));
g_string_append_printf(string, _("Operations on, and display of these files with %s may not succeed.\n"), PACKAGE);
g_string_append(string, "\n");
g_string_append(string, _("If your filenames are not encoded in utf-8, try setting the environment variable G_BROKEN_FILENAMES=1\n"));
gd = generic_dialog_new(_("Filename encoding locale mismatch"),
"locale warning", nullptr, TRUE, nullptr, nullptr);
- generic_dialog_add_button(gd, GTK_STOCK_CLOSE, nullptr, nullptr, TRUE);
+ generic_dialog_add_button(gd, GQ_ICON_CLOSE, _("Close"), nullptr, TRUE);
- generic_dialog_add_message(gd, GTK_STOCK_DIALOG_WARNING,
+ generic_dialog_add_message(gd, GQ_ICON_DIALOG_WARNING,
_("Filename encoding locale mismatch"), string->str, TRUE);
gtk_widget_show(gd->dialog);
gboolean copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime)
{
struct stat st;
- gchar *sl, *tl;
+ gchar *sl;
+ gchar *tl;
gboolean ret = FALSE;
if (!s || !t) return FALSE;
sta.st_ino == stb.st_ino);
}
+static void fclose_safe(FILE *fp)
+{
+ if (fp) fclose(fp);
+}
+
gboolean copy_file(const gchar *s, const gchar *t)
{
- FILE *fi = nullptr;
- FILE *fo = nullptr;
- gchar *sl = nullptr;
- gchar *tl = nullptr;
- gchar *randname = nullptr;
gchar buf[16384];
size_t b;
- gint ret = FALSE;
gint fd = -1;
- sl = path_from_utf8(s);
- tl = path_from_utf8(t);
+ std::unique_ptr<gchar, decltype(&g_free)> sl{path_from_utf8(s), g_free};
+ std::unique_ptr<gchar, decltype(&g_free)> tl{path_from_utf8(t), g_free};
- if (hard_linked(sl, tl))
+ if (hard_linked(sl.get(), tl.get()))
{
- ret = TRUE;
- goto end;
+ return TRUE;
}
/* Do not dereference absolute symlinks, but copy them "as is".
* copied/moved to another dir to keep pointing it to same target as
* a relative symlink, so we turn it into absolute symlink using
* realpath() instead. */
- struct stat st;
- if (lstat_utf8(sl, &st) && S_ISLNK(st.st_mode))
+ auto copy_symlink = [](const gchar *sl, const gchar *tl) -> gboolean
{
+ struct stat st;
+ if (!lstat_utf8(sl, &st) && S_ISLNK(st.st_mode))
+ {
+ return FALSE;
+ }
+
gchar *link_target;
ssize_t i;
if (i<0)
{
g_free(link_target);
- goto orig_copy; // try a "normal" copy
+ return FALSE; // try a "normal" copy
}
link_target[st.st_size] = '\0';
{
gchar *absolute;
- gchar *lastslash = strrchr(sl, G_DIR_SEPARATOR);
+ const gchar *lastslash = strrchr(sl, G_DIR_SEPARATOR);
gint len = lastslash - sl + 1;
absolute = static_cast<gchar *>(g_malloc(len + st.st_size + 1));
else // could not get absolute path, got some error instead
{
g_free(link_target);
- goto orig_copy; // so try a "normal" copy
+ return FALSE; // so try a "normal" copy
}
}
gint success = (symlink(link_target, tl) == 0);
g_free(link_target);
- if (success)
- {
- ret = TRUE;
- goto end;
- }
- } // if symlink did not succeed, continue on to try a copy procedure
- orig_copy:
+ return success;
+ };
- fi = fopen(sl, "rb");
- if (!fi) goto end;
+ if (copy_symlink(sl.get(), tl.get()))
+ {
+ return TRUE;
+ }
+
+ // if symlink did not succeed, continue on to try a copy procedure
+ std::unique_ptr<FILE, decltype(&fclose_safe)> fi{fopen(sl.get(), "rb"), fclose_safe};
+ if (!fi)
+ {
+ return FALSE;
+ }
/* First we write to a temporary file, then we rename it on success,
and attributes from original file are copied */
- randname = g_strconcat(tl, ".tmp_XXXXXX", NULL);
- if (!randname) goto end;
+ std::unique_ptr<gchar, decltype(&g_free)> randname{g_strconcat(tl.get(), ".tmp_XXXXXX", NULL), g_free};
+ if (!randname)
+ {
+ return FALSE;
+ }
- fd = g_mkstemp(randname);
- if (fd == -1) goto end;
+ fd = g_mkstemp(randname.get());
+ if (fd == -1)
+ {
+ return FALSE;
+ }
- fo = fdopen(fd, "wb");
+ std::unique_ptr<FILE, decltype(&fclose_safe)> fo{fdopen(fd, "wb"), fclose_safe};
if (!fo) {
close(fd);
- goto end;
+ return FALSE;
}
- while ((b = fread(buf, sizeof(gchar), sizeof(buf), fi)) && b != 0)
+ while ((b = fread(buf, sizeof(gchar), sizeof(buf), fi.get())) && b != 0)
{
- if (fwrite(buf, sizeof(gchar), b, fo) != b)
+ if (fwrite(buf, sizeof(gchar), b, fo.get()) != b)
{
- unlink(randname);
- goto end;
+ unlink(randname.get());
+ return FALSE;
}
}
- fclose(fi); fi = nullptr;
- fclose(fo); fo = nullptr;
-
- if (rename(randname, tl) < 0) {
- unlink(randname);
- goto end;
+ if (rename(randname.get(), tl.get()) < 0) {
+ unlink(randname.get());
+ return FALSE;
}
- ret = copy_file_attributes(s, t, TRUE, TRUE);
-
-end:
- if (fi) fclose(fi);
- if (fo) fclose(fo);
- if (sl) g_free(sl);
- if (tl) g_free(tl);
- if (randname) g_free(randname);
- return ret;
+ return copy_file_attributes(s, t, TRUE, TRUE);
}
gboolean move_file(const gchar *s, const gchar *t)
{
- gchar *sl, *tl;
+ gchar *sl;
+ gchar *tl;
gboolean ret = TRUE;
if (!s || !t) return FALSE;
gboolean rename_file(const gchar *s, const gchar *t)
{
- gchar *sl, *tl;
+ gchar *sl;
+ gchar *tl;
gboolean ret;
if (!s || !t) return FALSE;
return path8;
}
-void list_free_wrapper(void *data, void *UNUSED(userdata))
-{
- g_free(data);
-}
-
void string_list_free(GList *list)
{
- g_list_foreach(list, static_cast<GFunc>(list_free_wrapper), nullptr);
- g_list_free(list);
+ g_list_free_full(list, g_free);
}
GList *string_list_copy(const GList *list)
const gchar *name;
const gchar *ext;
- if (!path) return NULL;
+ if (!path) return nullptr;
name = filename_from_path(path);
- if (!name) return NULL;
+ if (!name) return nullptr;
ext = registered_extension_from_path(name);
if (!ext)
{
- unique = unique_filename(path, NULL, "_", TRUE);
+ unique = unique_filename(path, nullptr, "_", TRUE);
}
else
{
void parse_out_relatives(gchar *path)
{
- gint s, t;
+ gint s;
+ gint t;
if (!path) return;
s = p;
continue;
}
- else if (path[p] == '.' && (path[p+1] == G_DIR_SEPARATOR || path[p+1] == '\0'))
+
+ if (path[p] == '.' && (path[p+1] == G_DIR_SEPARATOR || path[p+1] == '\0'))
{
/* /../ or /.., remove previous part, ie. /a/b/../ becomes /a/ */
s = p + 1;
{
gchar *path;
gchar *namel;
- gint p, l;
+ gint p;
+ gint l;
gboolean ret = FALSE;
if (!name) return FALSE;
gchar *md5_text_from_file_utf8(const gchar *path, const gchar *error_text)
{
- guchar digest[16];
+ std::unique_ptr<gchar, decltype(&g_free)> pathl{path_from_utf8(path), g_free};
- if (!md5_get_digest_from_file_utf8(path, digest)) return g_strdup(error_text);
+ auto md5_text = md5_get_string_from_file(pathl.get());
- return md5_digest_to_text(digest);
+ return md5_text ? md5_text : g_strdup(error_text);
}
/* Download web file
}
else
{
- file_util_warning_dialog(_("Web file download failed"), error->message, GTK_STOCK_DIALOG_ERROR, nullptr);
+ file_util_warning_dialog(_("Web file download failed"), error->message, GQ_ICON_DIALOG_ERROR, nullptr);
}
g_object_unref(web->tmp_g_file);
}
}
-static void download_web_file_cancel_button_cb(GenericDialog *UNUSED(gd), gpointer data)
+static void download_web_file_cancel_button_cb(GenericDialog *, gpointer data)
{
auto web = static_cast<WebData *>(data);
web->gd = generic_dialog_new(_("Download web file"), "download_web_file", nullptr, TRUE, download_web_file_cancel_button_cb, web);
message = g_strconcat(_("Downloading "), base, NULL);
- generic_dialog_add_message(web->gd, GTK_STOCK_DIALOG_INFO, message, nullptr, FALSE);
+ generic_dialog_add_message(web->gd, GQ_ICON_DIALOG_INFO, message, nullptr, FALSE);
web->progress = gtk_progress_bar_new();
- gtk_box_pack_start(GTK_BOX(web->gd->vbox), web->progress, FALSE, FALSE, 0);
+ gq_gtk_box_pack_start(GTK_BOX(web->gd->vbox), web->progress, FALSE, FALSE, 0);
gtk_widget_show(web->progress);
if (minimized)
{