]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
lfn: replace hash function
authorYehuda Sadeh <yehuda@hq.newdream.net>
Wed, 27 Apr 2011 23:32:52 +0000 (16:32 -0700)
committerYehuda Sadeh <yehuda@hq.newdream.net>
Wed, 27 Apr 2011 23:35:35 +0000 (16:35 -0700)
for some reason crashes when using libnss

src/Makefile.am
src/common/ceph_crypto.h
src/cosd.cc
src/os/FileStore.cc
src/os/FileStore.h

index 148e24c85ba72fe627f806c3cb1a43607699510b..cc270901db739b6f396aed782f6e968a1f936d91 100644 (file)
@@ -687,6 +687,8 @@ libos_a_SOURCES = \
        os/FileJournal.cc \
        os/FileStore.cc \
        os/JournalingObjectStore.cc
+libos_a_CXXFLAGS= ${CRYPTO_CXXFLAGS} ${AM_CXXFLAGS}
+
 
 libosd_a_SOURCES = \
        osd/PG.cc \
index ebe39b14e51c44e78e7d8cc342042c8fa6492dd6..823871962f6d63d779175c1367b60be4baa57103 100644 (file)
@@ -5,6 +5,7 @@
 
 #define CEPH_CRYPTO_MD5_DIGESTSIZE 16
 #define CEPH_CRYPTO_HMACSHA1_DIGESTSIZE 20
+#define CEPH_CRYPTO_SHA1_DIGESTSIZE 20
 
 #ifdef USE_CRYPTOPP
 # define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
@@ -18,6 +19,7 @@ namespace ceph {
       // nothing
     }
     using CryptoPP::Weak::MD5;
+    using CryptoPP::SHA1;
 
     class HMACSHA1: public CryptoPP::HMAC<CryptoPP::SHA1> {
     public:
@@ -47,7 +49,6 @@ typedef unsigned char byte;
 namespace ceph {
   namespace crypto {
     void init();
-
     class MD5 {
     private:
       PK11Context *ctx;
@@ -79,7 +80,51 @@ namespace ceph {
        Restart();
       }
     };
+#if 0
+    class Digest {
+    private:
+      PK11Context *ctx;
+      SECOidTag sec_type;
+      size_t digest_size;
+    public:
+      Digest (SECOidTag _type, size_t _digest_size) : sec_type(_type), digest_size(_digest_size) {
+       ctx = PK11_CreateDigestContext(_type);
+       assert(ctx);
+       Restart();
+      }
+      ~Digest () {
+       PK11_DestroyContext(ctx, PR_TRUE);
+      }
+      void Restart() {
+       SECStatus s;
+       s = PK11_DigestBegin(ctx);
+       assert(s == SECSuccess);
+      }
+      void Update (const byte *input, size_t length) {
+       SECStatus s;
+       s = PK11_DigestOp(ctx, input, length);
+       assert(s == SECSuccess);
+      }
+      void Final (byte *digest) {
+       SECStatus s;
+       unsigned int dummy;
+       s = PK11_DigestFinal(ctx, digest, &dummy, digest_size);
+       assert(s == SECSuccess);
+       assert(dummy == digest_size);
+       Restart();
+      }
+    };
+    class MD5 : public Digest {
+    public:
+      MD5 () : Digest(SEC_OID_MD5, CEPH_CRYPTO_MD5_DIGESTSIZE) { }
+    };
 
+   class SHA1 : public Digest {
+    public:
+      SHA1 () : Digest(SEC_OID_SHA1, CEPH_CRYPTO_SHA1_DIGESTSIZE) { }
+    };
+
+#endif
     class HMACSHA1 {
     private:
       PK11SlotInfo *slot;
index 4e6fd77632ed9bb6527b5b2578855b13e2b83713..1287079baa397b5e5f468db8bde4a0401877b15b 100644 (file)
@@ -56,7 +56,7 @@ int main(int argc, const char **argv)
   vector<const char *>::iterator args_iter;
 
   common_init(args, CEPH_ENTITY_TYPE_OSD, CODE_ENVIRONMENT_DAEMON, 0);
-  
+
   ceph_heap_profiler_init();
 
   // osd specific args
index f6d3045b06e08d0647c25eddd8ae9e35a18ac05a..af5c9adb04eb3dffcdb3cf3c8f1014103ef4e657 100644 (file)
 
 #include <map>
 
+#include "common/ceph_crypto.h"
+
+using ceph::crypto::MD5;
+// using ceph::crypto::SHA1;
+
 /*
  * long file names will have the following format:
  *
@@ -157,11 +162,29 @@ int sys_listxattr(const char *fn, char *names, size_t len)
 }
 #endif
 
+static inline void buf_to_hex(const unsigned char *buf, int len, char *str)
+{
+  int i;
+  str[0] = '\0';
+  for (i = 0; i < len; i++) {
+    sprintf(&str[i*2], "%02x", (int)buf[i]);
+  }
+}
+
 static int hash_filename(const char *filename, char *hash, int buf_len)
 {
   if (buf_len < FILENAME_HASH_LEN + 1)
     return -EINVAL;
-  sprintf(hash, "zzz");
+
+  char buf[CEPH_CRYPTO_MD5_DIGESTSIZE];
+  char hex[CEPH_CRYPTO_MD5_DIGESTSIZE * 2];
+  MD5 h;
+  h.Update((const byte *)filename, strlen(filename));
+  h.Final((byte *)buf);
+
+  buf_to_hex((byte *)buf, (FILENAME_HASH_LEN + 1) / 2, hex);
+  strncpy(hash, hex, FILENAME_HASH_LEN);
+  hash[FILENAME_HASH_LEN] = '\0';
   return 0;
 }
 
@@ -217,7 +240,7 @@ static void lfn_translate(const char *path, const char *name, char *new_name, in
   // 012345678901234567890123456789012345678901234567890123456789
   // yyyyyyyyyyyyyyyy.zzzzzzzz.a_s
 
-void FileStore::append_oname(const sobject_t &oid, char *s, int len)
+int FileStore::append_oname(const sobject_t &oid, char *s, int len)
 {
   //assert(sizeof(oid) == 28);
   char *end = s + len;
@@ -239,13 +262,16 @@ void FileStore::append_oname(const sobject_t &oid, char *s, int len)
     i++;
   }
 
+  int size = t - s;
+
   if (oid.snap == CEPH_NOSNAP)
-    snprintf(t, end - t, "_head");
+    size += snprintf(t, end - t, "_head");
   else if (oid.snap == CEPH_SNAPDIR)
-    snprintf(t, end - t, "_snapdir");
+    size += snprintf(t, end - t, "_snapdir");
   else
-    snprintf(t, end - t, "_%llx", (long long unsigned)oid.snap);
-  //parse_object(t+1);
+    size += snprintf(t, end - t, "_%llx", (long long unsigned)oid.snap);
+
+  return size;
 }
 
 bool FileStore::parse_object(char *s, sobject_t& o)
@@ -306,9 +332,9 @@ int FileStore::lfn_get(coll_t cid, const sobject_t& oid, char *pathname, int len
   filename = pathname + path_len;
 
   *lfn = '\0';
-  append_oname(oid, lfn, lfn_len);
+  int actual_len = append_oname(oid, lfn, lfn_len);
 
-  if (oid.oid.name.size() < FILENAME_PREFIX_LEN) {
+  if (actual_len < (int)FILENAME_PREFIX_LEN) {
     /* not a long file name, just build it as it is */
     strncpy(filename, lfn, len - path_len);
     *is_lfn = 0;
index 59c37e26e0921385dfc689d234ce4ff146b311db..663849fc5b6cfe5fcdaf06260567126b9c66771e 100644 (file)
@@ -66,10 +66,8 @@ class FileStore : public JournalingObjectStore {
   Finisher ondisk_finisher;
 
   // helper fns
-  void append_oname(const sobject_t &oid, char *s, int len);
-  //void get_oname(sobject_t oid, char *s);
+  int append_oname(const sobject_t &oid, char *s, int len);
   int get_cdir(coll_t cid, char *s, int len);
-  void get_coname(coll_t cid, const sobject_t& oid, char *s, int len);
   bool parse_object(char *s, sobject_t& o);
   
   int lock_fsid();