]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw: generate random upload id
authorYehuda Sadeh <yehuda@hq.newdream.net>
Fri, 27 May 2011 21:22:55 +0000 (14:22 -0700)
committerYehuda Sadeh <yehuda@hq.newdream.net>
Fri, 27 May 2011 21:22:55 +0000 (14:22 -0700)
src/rgw/rgw_admin.cc
src/rgw/rgw_common.cc
src/rgw/rgw_common.h
src/rgw/rgw_op.cc

index ba40d0421cdfe841df9185ec3d6c0a1f41b82355..381a7cb6783944fa060b3607866decdc8a3b5ba1 100644 (file)
@@ -187,51 +187,6 @@ static int get_cmd(const char *cmd, const char *prev_cmd, bool *need_more)
   return -EINVAL;
 }
 
-int gen_rand_base64(char *dest, int size) /* size should be the required string size + 1 */
-{
-  char buf[size];
-  char tmp_dest[size + 4]; /* so that there's space for the extra '=' characters, and some */
-  int ret;
-
-  ret = get_random_bytes(buf, sizeof(buf));
-  if (ret < 0) {
-    cerr << "cannot get random bytes: " << cpp_strerror(-ret) << std::endl;
-    return -1;
-  }
-
-  ret = ceph_armor(tmp_dest, &tmp_dest[sizeof(tmp_dest)],
-                  (const char *)buf, ((const char *)buf) + ((size - 1) * 3 + 4 - 1) / 4);
-  if (ret < 0) {
-    cerr << "ceph_armor failed" << std::endl;
-    return -1;
-  }
-  tmp_dest[ret] = '\0';
-  memcpy(dest, tmp_dest, size);
-  dest[size] = '\0';
-
-  return 0;
-}
-
-static const char alphanum_table[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
-
-int gen_rand_alphanumeric(char *dest, int size) /* size should be the required string size + 1 */
-{
-  int ret = get_random_bytes(dest, size);
-  if (ret < 0) {
-    cerr << "cannot get random bytes: " << cpp_strerror(-ret) << std::endl;
-    return -1;
-  }
-
-  int i;
-  for (i=0; i<size - 1; i++) {
-    int pos = (unsigned)dest[i];
-    dest[i] = alphanum_table[pos % (sizeof(alphanum_table) - 1)];
-  }
-  dest[i] = '\0';
-
-  return 0;
-}
-
 string escape_str(string& src, char c)
 {
   int pos = 0;
@@ -497,7 +452,7 @@ int main(int argc, char **argv)
       RGWUserInfo duplicate_check;
       string duplicate_check_id;
       do {
-       ret = gen_rand_alphanumeric(public_id_buf, sizeof(public_id_buf));
+       ret = gen_rand_alphanumeric_upper(public_id_buf, sizeof(public_id_buf));
        if (ret < 0) {
          cerr << "aborting" << std::endl;
          exit(1);
index ba2640ab95756804715a39f5a510a02c5b5cb8eb..69cd5d2c8f9af9cccde94a876c539a7270808af5 100644 (file)
@@ -4,6 +4,9 @@
 #include "rgw_acl.h"
 
 #include "common/ceph_crypto.h"
+#include "common/armor.h"
+#include "common/errno.h"
+#include "auth/Crypto.h"
 
 using namespace ceph::crypto;
 
@@ -74,6 +77,73 @@ void calc_hmac_sha1(const char *key, int key_len,
   RGW_LOG(15) << "hmac=" << hex_str << dendl;
 }
 
+int gen_rand_base64(char *dest, int size) /* size should be the required string size + 1 */
+{
+  char buf[size];
+  char tmp_dest[size + 4]; /* so that there's space for the extra '=' characters, and some */
+  int ret;
+
+  ret = get_random_bytes(buf, sizeof(buf));
+  if (ret < 0) {
+    cerr << "cannot get random bytes: " << cpp_strerror(-ret) << std::endl;
+    return -1;
+  }
+
+  ret = ceph_armor(tmp_dest, &tmp_dest[sizeof(tmp_dest)],
+                  (const char *)buf, ((const char *)buf) + ((size - 1) * 3 + 4 - 1) / 4);
+  if (ret < 0) {
+    cerr << "ceph_armor failed" << std::endl;
+    return -1;
+  }
+  tmp_dest[ret] = '\0';
+  memcpy(dest, tmp_dest, size);
+  dest[size] = '\0';
+
+  return 0;
+}
+
+static const char alphanum_upper_table[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+
+int gen_rand_alphanumeric_upper(char *dest, int size) /* size should be the required string size + 1 */
+{
+  int ret = get_random_bytes(dest, size);
+  if (ret < 0) {
+    cerr << "cannot get random bytes: " << cpp_strerror(-ret) << std::endl;
+    return -1;
+  }
+
+  int i;
+  for (i=0; i<size - 1; i++) {
+    int pos = (unsigned)dest[i];
+    dest[i] = alphanum_upper_table[pos % (sizeof(alphanum_upper_table) - 1)];
+  }
+  dest[i] = '\0';
+
+  return 0;
+}
+
+
+// this is basically a modified base64 charset, url friendly
+static const char alphanum_table[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
+
+int gen_rand_alphanumeric(char *dest, int size) /* size should be the required string size + 1 */
+{
+  int ret = get_random_bytes(dest, size);
+  if (ret < 0) {
+    cerr << "cannot get random bytes: " << cpp_strerror(-ret) << std::endl;
+    return -1;
+  }
+
+  int i;
+  for (i=0; i<size - 1; i++) {
+    int pos = (unsigned)dest[i];
+    dest[i] = alphanum_table[pos & 63];
+  }
+  dest[i] = '\0';
+
+  return 0;
+}
+
 int NameVal::parse()
 {
   int delim_pos = str.find('=');
index 68749d481d60332f0ceed95c66d9843c8badee6d..dbecef0adace70df062eb944688e2501ced1a75f 100644 (file)
@@ -84,6 +84,11 @@ extern string rgw_root_bucket;
 
 typedef void *RGWAccessHandle;
 
+ /* size should be the required string size + 1 */
+extern int gen_rand_base64(char *dest, int size);
+extern int gen_rand_alphanumeric(char *dest, int size);
+extern int gen_rand_alphanumeric_upper(char *dest, int size);
+
 /** Store error returns for output at a different point in the program */
 struct rgw_err {
   rgw_err();
index b1ed9cf9f2735c9eccc1b76ab937663d741e5621..206de5fa1416f792adcfbc7cad92d784a86a40a5 100644 (file)
@@ -810,9 +810,17 @@ void RGWInitMultipart::execute()
 
   get_request_metadata(s, attrs);
 
-  upload_id="blabla";
+  do {
+    char buf[33];
+    gen_rand_alphanumeric(buf, sizeof(buf) - 1);
+    upload_id = buf;
 
-  ret = rgwstore->put_obj_meta(s->user.user_id, s->bucket_str, s->object_str, NULL, attrs, true);
+    string tmp_obj_name = s->object_str;
+    tmp_obj_name.append(".");
+    tmp_obj_name.append(upload_id);
+
+    ret = rgwstore->put_obj_meta(s->user.user_id, s->bucket_str, tmp_obj_name, NULL, attrs, true);
+  } while (ret == -EEXIST);
 done:
   send_response();
 }