diff options
-rw-r--r-- | common/Makefile.am | 1 | ||||
-rw-r--r-- | common/gvfsutils.c | 42 | ||||
-rw-r--r-- | common/gvfsutils.h | 30 | ||||
-rw-r--r-- | daemon/gvfsbackendafp.c | 13 | ||||
-rw-r--r-- | daemon/gvfsbackendsftp.c | 33 | ||||
-rw-r--r-- | daemon/gvfsbackendsmb.c | 14 | ||||
-rw-r--r-- | daemon/gvfsdaemon.c | 19 | ||||
-rw-r--r-- | daemon/gvfsdaemonutils.c | 1 | ||||
-rw-r--r-- | metadata/Makefile.am | 6 | ||||
-rw-r--r-- | metadata/metatree.c | 29 |
10 files changed, 92 insertions, 96 deletions
diff --git a/common/Makefile.am b/common/Makefile.am index 0577d677..ac860ce7 100644 --- a/common/Makefile.am +++ b/common/Makefile.am @@ -28,6 +28,7 @@ libgvfscommon_la_SOURCES = \ gvfsdaemonprotocol.c gvfsdaemonprotocol.h \ gvfsicon.h gvfsicon.c \ gvfsfileinfo.c gvfsfileinfo.h \ + gvfsutils.c gvfsutils.h \ $(dbus_built_sources) \ $(NULL) diff --git a/common/gvfsutils.c b/common/gvfsutils.c new file mode 100644 index 00000000..87718f30 --- /dev/null +++ b/common/gvfsutils.c @@ -0,0 +1,42 @@ +/* GIO - GLib Input, Output and Streaming Library + * + * Copyright (C) 2014 Ross Lagerwall + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, see + * <http://www.gnu.org/licenses/>. + */ + +#include <config.h> + +#include <string.h> +#include <glib.h> +#include "gvfsutils.h" + +/** + * gvfs_randomize_string: + * @str: the string to randomize + * @len: the length of the string + * + * Takes a string and fills it with @len random chars. + **/ +void +gvfs_randomize_string (char *str, + int len) +{ + int i; + const char chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + + for (i = 0; i < len; i++) + str[i] = chars[g_random_int_range (0, strlen(chars))]; +} diff --git a/common/gvfsutils.h b/common/gvfsutils.h new file mode 100644 index 00000000..aa7faf5e --- /dev/null +++ b/common/gvfsutils.h @@ -0,0 +1,30 @@ +/* GIO - GLib Input, Output and Streaming Library + * + * Copyright (C) 2014 Ross Lagerwall + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, see + * <http://www.gnu.org/licenses/>. + */ + +#ifndef __G_VFS_UTILS_H__ +#define __G_VFS_UTILS_H__ + +G_BEGIN_DECLS + +void gvfs_randomize_string (char *str, + int len); + +G_END_DECLS + +#endif /* __G_VFS_UTILS_H__ */ diff --git a/daemon/gvfsbackendafp.c b/daemon/gvfsbackendafp.c index 3630c1c4..090d1de9 100644 --- a/daemon/gvfsbackendafp.c +++ b/daemon/gvfsbackendafp.c @@ -53,6 +53,7 @@ #include "gvfsjobsetdisplayname.h" #include "gvfsjobmove.h" #include "gvfsjobcopy.h" +#include "gvfsutils.h" #include "gvfsafpserver.h" #include "gvfsafpvolume.h" @@ -1293,22 +1294,12 @@ replace_create_tmp_file_cb (GObject *source_object, GAsyncResult *res, gpointer } static void -random_chars (char *str, int len) -{ - int i; - const char chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - - for (i = 0; i < len; i++) - str[i] = chars[g_random_int_range (0, strlen(chars))]; -} - -static void replace_create_tmp_file (GVfsAfpVolume *volume, GVfsJobOpenForWrite *job) { char basename[] = "~gvfXXXX.tmp"; char *dir, *tmp_filename; - random_chars (basename + 4, 4); + gvfs_randomize_string (basename + 4, 4); dir = g_path_get_dirname (job->filename); tmp_filename = g_build_filename (dir, basename, NULL); diff --git a/daemon/gvfsbackendsftp.c b/daemon/gvfsbackendsftp.c index 005a9eb5..576b74e6 100644 --- a/daemon/gvfsbackendsftp.c +++ b/daemon/gvfsbackendsftp.c @@ -66,6 +66,7 @@ #include "gvfsjobpush.h" #include "gvfsjobpull.h" #include "gvfsdaemonprotocol.h" +#include "gvfsutils.h" #include "gvfskeyring.h" #include "sftp.h" #include "pty_open.h" @@ -3408,34 +3409,6 @@ replace_create_temp_reply (GVfsBackendSftp *backend, } static void -random_text (char *s) -{ - static const char letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - static const int NLETTERS = sizeof (letters) - 1; - static int counter = 0; - - GTimeVal tv; - glong value; - - /* Get some more or less random data. */ - g_get_current_time (&tv); - value = (tv.tv_usec ^ tv.tv_sec) + counter++; - - /* Fill in the random bits. */ - s[0] = letters[value % NLETTERS]; - value /= NLETTERS; - s[1] = letters[value % NLETTERS]; - value /= NLETTERS; - s[2] = letters[value % NLETTERS]; - value /= NLETTERS; - s[3] = letters[value % NLETTERS]; - value /= NLETTERS; - s[4] = letters[value % NLETTERS]; - value /= NLETTERS; - s[5] = letters[value % NLETTERS]; -} - -static void replace_create_temp (GVfsBackendSftp *backend, GVfsJobOpenForWrite *job) { @@ -3460,7 +3433,7 @@ replace_create_temp (GVfsBackendSftp *backend, g_free (data->tempname); dirname = g_path_get_dirname (job->filename); - random_text (basename + 8); + gvfs_randomize_string (basename + 8, 6); data->tempname = g_build_filename (dirname, basename, NULL); g_free (dirname); @@ -5395,7 +5368,7 @@ push_create_temp (SftpPushHandle *handle) g_free (handle->tempname); dirname = g_path_get_dirname (handle->op_job->destination); - random_text (basename + 8); + gvfs_randomize_string (basename + 8, 6); handle->tempname = g_build_filename (dirname, basename, NULL); g_free (dirname); diff --git a/daemon/gvfsbackendsmb.c b/daemon/gvfsbackendsmb.c index bdfb3659..4b0df517 100644 --- a/daemon/gvfsbackendsmb.c +++ b/daemon/gvfsbackendsmb.c @@ -49,6 +49,8 @@ #include "gvfsjobqueryattributes.h" #include "gvfsjobenumerate.h" #include "gvfsdaemonprotocol.h" +#include "gvfsdaemonutils.h" +#include "gvfsutils.h" #include "gvfskeyring.h" #include <libsmbclient.h> @@ -1052,16 +1054,6 @@ do_append_to (GVfsBackend *backend, } -static void -random_chars (char *str, int len) -{ - int i; - const char chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - - for (i = 0; i < len; i++) - str[i] = chars[g_random_int_range (0, strlen(chars))]; -} - static char * get_dir_from_uri (const char *uri) { @@ -1095,7 +1087,7 @@ open_tmpfile (GVfsBackendSmb *backend, dir_uri = get_dir_from_uri (uri); do { - random_chars (filename + 4, 4); + gvfs_randomize_string (filename + 4, 4); tmp_uri = g_strconcat (dir_uri, filename, NULL); smbc_open = smbc_getFunctionOpen (backend->smb_context); diff --git a/daemon/gvfsdaemon.c b/daemon/gvfsdaemon.c index f0865b0a..7bc334b9 100644 --- a/daemon/gvfsdaemon.c +++ b/daemon/gvfsdaemon.c @@ -38,6 +38,7 @@ #include <gvfsdaemon.h> #include <gvfsdaemonprotocol.h> #include <gvfsdaemonutils.h> +#include <gvfsutils.h> #include <gvfsjobmount.h> #include <gvfsjobopenforread.h> #include <gvfsjobopenforwrite.h> @@ -722,18 +723,6 @@ daemon_peer_connection_setup (GVfsDaemon *daemon, #define USE_ABSTRACT_SOCKETS #endif -static void -randomize_string (char tmp[9]) -{ - int i; - const char chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - - for (i = 0; i < 8; i++) - tmp[i] = chars[g_random_int_range (0, strlen(chars))]; - - tmp[8] = '\0'; -} - #ifndef USE_ABSTRACT_SOCKETS static gboolean test_safe_socket_dir (const char *dirname) @@ -770,7 +759,8 @@ create_socket_dir (void) { g_free (safe_dir); - randomize_string (tmp); + gvfs_randomize_string (tmp, 8); + tmp[8] = '\0'; dirname = g_strdup_printf ("gvfs-%s-%s", g_get_user_name (), tmp); @@ -827,7 +817,8 @@ generate_address (char **address, { gchar tmp[9]; - randomize_string (tmp); + gvfs_randomize_string (tmp, 8); + tmp[8] = '\0'; *address = g_strdup_printf ("unix:abstract=/dbus-vfs-daemon/socket-%s", tmp); } #else diff --git a/daemon/gvfsdaemonutils.c b/daemon/gvfsdaemonutils.c index 660873cf..3c331271 100644 --- a/daemon/gvfsdaemonutils.c +++ b/daemon/gvfsdaemonutils.c @@ -215,4 +215,3 @@ gvfs_file_info_populate_content_types (GFileInfo *info, g_free (free_mimetype); } - diff --git a/metadata/Makefile.am b/metadata/Makefile.am index e86e1c92..767033e1 100644 --- a/metadata/Makefile.am +++ b/metadata/Makefile.am @@ -46,16 +46,16 @@ libmetadata_la_SOURCES = \ libmetadata_la_LIBADD = $(GLIB_LIBS) $(UDEV_LIBS) -meta_ls_LDADD = libmetadata.la +meta_ls_LDADD = libmetadata.la ../common/libgvfscommon.la meta_ls_SOURCES = meta-ls.c meta_set_LDADD = libmetadata.la ../common/libgvfscommon.la meta_set_SOURCES = meta-set.c -meta_get_LDADD = libmetadata.la +meta_get_LDADD = libmetadata.la ../common/libgvfscommon.la meta_get_SOURCES = meta-get.c -meta_get_tree_LDADD = libmetadata.la +meta_get_tree_LDADD = libmetadata.la ../common/libgvfscommon.la meta_get_tree_SOURCES = meta-get-tree.c convert_nautilus_metadata_LDADD = libmetadata.la $(LIBXML_LIBS) diff --git a/metadata/metatree.c b/metadata/metatree.c index 8875ae26..2983e5d5 100644 --- a/metadata/metatree.c +++ b/metadata/metatree.c @@ -13,6 +13,7 @@ #include "metabuilder.h" #include <glib.h> #include <glib/gstdio.h> +#include "gvfsutils.h" #include "crc32.h" #ifdef HAVE_LIBUDEV @@ -266,38 +267,14 @@ link_to_tmp (const char *source, char *tmpl) { char *XXXXXX; int count, res; - static const char letters[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - static const int NLETTERS = sizeof (letters) - 1; - glong value; - GTimeVal tv; - static int counter = 0; /* find the last occurrence of "XXXXXX" */ XXXXXX = g_strrstr (tmpl, "XXXXXX"); g_assert (XXXXXX != NULL); - /* Get some more or less random data. */ - g_get_current_time (&tv); - value = (tv.tv_usec ^ tv.tv_sec) + counter++; - - for (count = 0; count < 100; value += 7777, ++count) + for (count = 0; count < 100; ++count) { - glong v = value; - - /* Fill in the random bits. */ - XXXXXX[0] = letters[v % NLETTERS]; - v /= NLETTERS; - XXXXXX[1] = letters[v % NLETTERS]; - v /= NLETTERS; - XXXXXX[2] = letters[v % NLETTERS]; - v /= NLETTERS; - XXXXXX[3] = letters[v % NLETTERS]; - v /= NLETTERS; - XXXXXX[4] = letters[v % NLETTERS]; - v /= NLETTERS; - XXXXXX[5] = letters[v % NLETTERS]; - + gvfs_randomize_string (XXXXXX, 6); res = link (source, tmpl); if (res >= 0) |