[gvfs/nfs: 1/3] daemon: Move random string generation into shared lib
- From: Ross Lagerwall <rossl src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gvfs/nfs: 1/3] daemon: Move random string generation into shared lib
- Date: Tue, 21 Oct 2014 21:37:27 +0000 (UTC)
commit ca883e6a2d156c3a9cb46cc2064448ed2a06693e
Author: Ross Lagerwall <rosslagerwall gmail com>
Date: Sun Oct 19 09:03:51 2014 +0100
daemon: Move random string generation into shared lib
Random string generation is used in a few different places, so share the
implementation.
daemon/gvfsbackendafp.c | 13 ++-----------
daemon/gvfsbackendsftp.c | 33 +++------------------------------
daemon/gvfsbackendsmb.c | 13 ++-----------
daemon/gvfsdaemon.c | 18 ++++--------------
daemon/gvfsdaemonutils.c | 16 ++++++++++++++++
daemon/gvfsdaemonutils.h | 2 ++
6 files changed, 29 insertions(+), 66 deletions(-)
---
diff --git a/daemon/gvfsbackendafp.c b/daemon/gvfsbackendafp.c
index 3630c1c..f7cbd5b 100644
--- a/daemon/gvfsbackendafp.c
+++ b/daemon/gvfsbackendafp.c
@@ -53,6 +53,7 @@
#include "gvfsjobsetdisplayname.h"
#include "gvfsjobmove.h"
#include "gvfsjobcopy.h"
+#include "gvfsdaemonutils.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 75725e1..0134032 100644
--- a/daemon/gvfsbackendsftp.c
+++ b/daemon/gvfsbackendsftp.c
@@ -66,6 +66,7 @@
#include "gvfsjobpush.h"
#include "gvfsjobpull.h"
#include "gvfsdaemonprotocol.h"
+#include "gvfsdaemonutils.h"
#include "gvfskeyring.h"
#include "sftp.h"
#include "pty_open.h"
@@ -3366,34 +3367,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)
{
@@ -3418,7 +3391,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);
@@ -5353,7 +5326,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 7e5bab5..ac338d7 100644
--- a/daemon/gvfsbackendsmb.c
+++ b/daemon/gvfsbackendsmb.c
@@ -49,6 +49,7 @@
#include "gvfsjobqueryattributes.h"
#include "gvfsjobenumerate.h"
#include "gvfsdaemonprotocol.h"
+#include "gvfsdaemonutils.h"
#include "gvfskeyring.h"
#include <libsmbclient.h>
@@ -1034,16 +1035,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)
{
@@ -1077,7 +1068,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 0187223..cae2803 100644
--- a/daemon/gvfsdaemon.c
+++ b/daemon/gvfsdaemon.c
@@ -717,18 +717,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)
@@ -765,7 +753,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);
@@ -822,7 +811,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 660873c..a5f635f 100644
--- a/daemon/gvfsdaemonutils.c
+++ b/daemon/gvfsdaemonutils.c
@@ -216,3 +216,19 @@ gvfs_file_info_populate_content_types (GFileInfo *info,
g_free (free_mimetype);
}
+/**
+ * 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/daemon/gvfsdaemonutils.h b/daemon/gvfsdaemonutils.h
index 0000caa..b08d794 100644
--- a/daemon/gvfsdaemonutils.h
+++ b/daemon/gvfsdaemonutils.h
@@ -40,6 +40,8 @@ void gvfs_file_info_populate_content_types (GFileInfo *info,
const char *basename,
GFileType type);
+void gvfs_randomize_string (char *str, int len);
+
G_END_DECLS
#endif /* __G_VFS_DAEMON_UTILS_H__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]