瀏覽代碼

Merge pull request #4127 from motwok/master

c code portability fixes: issue #2677 and 2 more
TW 6 年之前
父節點
當前提交
08a03126b7
共有 4 個文件被更改,包括 46 次插入46 次删除
  1. 14 14
      src/borg/_hashindex.c
  2. 1 1
      src/borg/cache_sync/unpack.h
  3. 1 1
      src/borg/crypto/low_level.pyx
  4. 30 30
      src/borg/hashindex.pyx

+ 14 - 14
src/borg/_hashindex.c

@@ -34,7 +34,7 @@ typedef struct {
 } __attribute__((__packed__)) HashHeader;
 
 typedef struct {
-    void *buckets;
+    unsigned char *buckets;
     int num_entries;
     int num_buckets;
     int num_empty;
@@ -103,10 +103,10 @@ static void hashindex_write(HashIndex *index, PyObject *file_py);
 
 static uint64_t hashindex_compact(HashIndex *index);
 static HashIndex *hashindex_init(int capacity, int key_size, int value_size);
-static const void *hashindex_get(HashIndex *index, const void *key);
-static int hashindex_set(HashIndex *index, const void *key, const void *value);
-static int hashindex_delete(HashIndex *index, const void *key);
-static void *hashindex_next_key(HashIndex *index, const void *key);
+static const unsigned char *hashindex_get(HashIndex *index, const unsigned char *key);
+static int hashindex_set(HashIndex *index, const unsigned char *key, const unsigned char *value);
+static int hashindex_delete(HashIndex *index, const unsigned char *key);
+static unsigned char *hashindex_next_key(HashIndex *index, const unsigned char *key);
 
 /* Private API */
 static void hashindex_free(HashIndex *index);
@@ -125,13 +125,13 @@ hashindex_free_buckets(HashIndex *index)
 }
 
 static int
-hashindex_index(HashIndex *index, const void *key)
+hashindex_index(HashIndex *index, const unsigned char *key)
 {
     return _le32toh(*((uint32_t *)key)) % index->num_buckets;
 }
 
 static int
-hashindex_lookup(HashIndex *index, const void *key, int *start_idx)
+hashindex_lookup(HashIndex *index, const unsigned char *key, int *start_idx)
 {
     int didx = -1;
     int start = hashindex_index(index, key);
@@ -174,7 +174,7 @@ static int
 hashindex_resize(HashIndex *index, int capacity)
 {
     HashIndex *new;
-    void *key = NULL;
+    unsigned char *key = NULL;
     int32_t key_size = index->key_size;
 
     if(!(new = hashindex_init(capacity, key_size, index->value_size))) {
@@ -534,8 +534,8 @@ hashindex_write(HashIndex *index, PyObject *file_py)
 }
 #endif
 
-static const void *
-hashindex_get(HashIndex *index, const void *key)
+static const unsigned char *
+hashindex_get(HashIndex *index, const unsigned char *key)
 {
     int idx = hashindex_lookup(index, key, NULL);
     if(idx < 0) {
@@ -545,7 +545,7 @@ hashindex_get(HashIndex *index, const void *key)
 }
 
 static int
-hashindex_set(HashIndex *index, const void *key, const void *value)
+hashindex_set(HashIndex *index, const unsigned char *key, const unsigned char *value)
 {
     int start_idx;
     int idx = hashindex_lookup(index, key, &start_idx);
@@ -587,7 +587,7 @@ hashindex_set(HashIndex *index, const void *key, const void *value)
 }
 
 static int
-hashindex_delete(HashIndex *index, const void *key)
+hashindex_delete(HashIndex *index, const unsigned char *key)
 {
     int idx = hashindex_lookup(index, key, NULL);
     if (idx < 0) {
@@ -603,8 +603,8 @@ hashindex_delete(HashIndex *index, const void *key)
     return 1;
 }
 
-static void *
-hashindex_next_key(HashIndex *index, const void *key)
+static unsigned char *
+hashindex_next_key(HashIndex *index, const unsigned char *key)
 {
     int idx = 0;
     if(key) {

+ 1 - 1
src/borg/cache_sync/unpack.h

@@ -116,7 +116,7 @@ static inline void unpack_init_user_state(unpack_user *u)
 {
     u->last_error = NULL;
     u->level = 0;
-    u->inside_chunks = false;
+    u->inside_chunks = 0;
     u->expect = expect_item_begin;
 }
 

+ 1 - 1
src/borg/crypto/low_level.pyx

@@ -599,7 +599,7 @@ cdef class _AEAD_BASE:
                 raise CryptoError('EVP_CIPHER_CTX_ctrl SET IVLEN failed')
             if not EVP_DecryptInit_ex(self.ctx, NULL, NULL, self.enc_key, iv):
                 raise CryptoError('EVP_DecryptInit_ex failed')
-            if not EVP_CIPHER_CTX_ctrl(self.ctx, EVP_CTRL_GCM_SET_TAG, self.mac_len, <void *> idata.buf+hlen):
+            if not EVP_CIPHER_CTX_ctrl(self.ctx, EVP_CTRL_GCM_SET_TAG, self.mac_len, <const unsigned char *> idata.buf+hlen):
                 raise CryptoError('EVP_CIPHER_CTX_ctrl SET TAG failed')
             rc = EVP_DecryptUpdate(self.ctx, NULL, &olen, <const unsigned char*> idata.buf+aoffset, alen)
             if not rc:

+ 30 - 30
src/borg/hashindex.pyx

@@ -28,10 +28,10 @@ cdef extern from "_hashindex.c":
     int hashindex_len(HashIndex *index)
     int hashindex_size(HashIndex *index)
     void hashindex_write(HashIndex *index, object file_py) except *
-    void *hashindex_get(HashIndex *index, void *key)
-    void *hashindex_next_key(HashIndex *index, void *key)
-    int hashindex_delete(HashIndex *index, void *key)
-    int hashindex_set(HashIndex *index, void *key, void *value)
+    unsigned char *hashindex_get(HashIndex *index, unsigned char *key)
+    unsigned char *hashindex_next_key(HashIndex *index, unsigned char *key)
+    int hashindex_delete(HashIndex *index, unsigned char *key)
+    int hashindex_set(HashIndex *index, unsigned char *key, void *value)
     uint64_t hashindex_compact(HashIndex *index)
     uint32_t _htole32(uint32_t v)
     uint32_t _le32toh(uint32_t v)
@@ -127,7 +127,7 @@ cdef class IndexBase:
 
     def __delitem__(self, key):
         assert len(key) == self.key_size
-        rc = hashindex_delete(self.index, <char *>key)
+        rc = hashindex_delete(self.index, <unsigned char *>key)
         if rc == 1:
             return  # success
         if rc == -1:
@@ -170,7 +170,7 @@ cdef class FuseVersionsIndex(IndexBase):
     def __getitem__(self, key):
         cdef FuseVersionsElement *data
         assert len(key) == self.key_size
-        data = <FuseVersionsElement *>hashindex_get(self.index, <char *>key)
+        data = <FuseVersionsElement *>hashindex_get(self.index, <unsigned char *>key)
         if data == NULL:
             raise KeyError(key)
         return _le32toh(data.version), PyBytes_FromStringAndSize(data.hash, 16)
@@ -184,12 +184,12 @@ cdef class FuseVersionsIndex(IndexBase):
             raise TypeError("Expected bytes of length 16 for second value")
         memcpy(data.hash, PyBytes_AS_STRING(value[1]), 16)
         data.version = _htole32(data.version)
-        if not hashindex_set(self.index, <char *>key, <void *> &data):
+        if not hashindex_set(self.index, <unsigned char *>key, <void *> &data):
             raise Exception('hashindex_set failed')
 
     def __contains__(self, key):
         assert len(key) == self.key_size
-        return hashindex_get(self.index, <char *>key) != NULL
+        return hashindex_get(self.index, <unsigned char *>key) != NULL
 
 
 cdef class NSIndex(IndexBase):
@@ -198,7 +198,7 @@ cdef class NSIndex(IndexBase):
 
     def __getitem__(self, key):
         assert len(key) == self.key_size
-        data = <uint32_t *>hashindex_get(self.index, <char *>key)
+        data = <uint32_t *>hashindex_get(self.index, <unsigned char *>key)
         if not data:
             raise KeyError(key)
         cdef uint32_t segment = _le32toh(data[0])
@@ -212,25 +212,25 @@ cdef class NSIndex(IndexBase):
         assert segment <= _MAX_VALUE, "maximum number of segments reached"
         data[0] = _htole32(segment)
         data[1] = _htole32(value[1])
-        if not hashindex_set(self.index, <char *>key, data):
+        if not hashindex_set(self.index, <unsigned char *>key, data):
             raise Exception('hashindex_set failed')
 
     def __contains__(self, key):
         cdef uint32_t segment
         assert len(key) == self.key_size
-        data = <uint32_t *>hashindex_get(self.index, <char *>key)
+        data = <uint32_t *>hashindex_get(self.index, <unsigned char *>key)
         if data != NULL:
             segment = _le32toh(data[0])
             assert segment <= _MAX_VALUE, "maximum number of segments reached"
         return data != NULL
 
     def iteritems(self, marker=None):
-        cdef const void *key
+        cdef const unsigned char *key
         iter = NSKeyIterator(self.key_size)
         iter.idx = self
         iter.index = self.index
         if marker:
-            key = hashindex_get(self.index, <char *>marker)
+            key = hashindex_get(self.index, <unsigned char *>marker)
             if marker is None:
                 raise IndexError
             iter.key = key - self.key_size
@@ -240,7 +240,7 @@ cdef class NSIndex(IndexBase):
 cdef class NSKeyIterator:
     cdef NSIndex idx
     cdef HashIndex *index
-    cdef const void *key
+    cdef const unsigned char *key
     cdef int key_size
     cdef int exhausted
 
@@ -255,7 +255,7 @@ cdef class NSKeyIterator:
     def __next__(self):
         if self.exhausted:
             raise StopIteration
-        self.key = hashindex_next_key(self.index, <char *>self.key)
+        self.key = hashindex_next_key(self.index, <unsigned char *>self.key)
         if not self.key:
             self.exhausted = 1
             raise StopIteration
@@ -289,7 +289,7 @@ cdef class ChunkIndex(IndexBase):
 
     def __getitem__(self, key):
         assert len(key) == self.key_size
-        data = <uint32_t *>hashindex_get(self.index, <char *>key)
+        data = <uint32_t *>hashindex_get(self.index, <unsigned char *>key)
         if not data:
             raise KeyError(key)
         cdef uint32_t refcount = _le32toh(data[0])
@@ -304,12 +304,12 @@ cdef class ChunkIndex(IndexBase):
         data[0] = _htole32(refcount)
         data[1] = _htole32(value[1])
         data[2] = _htole32(value[2])
-        if not hashindex_set(self.index, <char *>key, data):
+        if not hashindex_set(self.index, <unsigned char *>key, data):
             raise Exception('hashindex_set failed')
 
     def __contains__(self, key):
         assert len(key) == self.key_size
-        data = <uint32_t *>hashindex_get(self.index, <char *>key)
+        data = <uint32_t *>hashindex_get(self.index, <unsigned char *>key)
         if data != NULL:
             assert _le32toh(data[0]) <= _MAX_VALUE, "invalid reference count"
         return data != NULL
@@ -317,7 +317,7 @@ cdef class ChunkIndex(IndexBase):
     def incref(self, key):
         """Increase refcount for 'key', return (refcount, size, csize)"""
         assert len(key) == self.key_size
-        data = <uint32_t *>hashindex_get(self.index, <char *>key)
+        data = <uint32_t *>hashindex_get(self.index, <unsigned char *>key)
         if not data:
             raise KeyError(key)
         cdef uint32_t refcount = _le32toh(data[0])
@@ -330,7 +330,7 @@ cdef class ChunkIndex(IndexBase):
     def decref(self, key):
         """Decrease refcount for 'key', return (refcount, size, csize)"""
         assert len(key) == self.key_size
-        data = <uint32_t *>hashindex_get(self.index, <char *>key)
+        data = <uint32_t *>hashindex_get(self.index, <unsigned char *>key)
         if not data:
             raise KeyError(key)
         cdef uint32_t refcount = _le32toh(data[0])
@@ -342,12 +342,12 @@ cdef class ChunkIndex(IndexBase):
         return refcount, _le32toh(data[1]), _le32toh(data[2])
 
     def iteritems(self, marker=None):
-        cdef const void *key
+        cdef const unsigned char *key
         iter = ChunkKeyIterator(self.key_size)
         iter.idx = self
         iter.index = self.index
         if marker:
-            key = hashindex_get(self.index, <char *>marker)
+            key = hashindex_get(self.index, <unsigned char *>marker)
             if marker is None:
                 raise IndexError
             iter.key = key - self.key_size
@@ -357,7 +357,7 @@ cdef class ChunkIndex(IndexBase):
         cdef uint64_t size = 0, csize = 0, unique_size = 0, unique_csize = 0, chunks = 0, unique_chunks = 0
         cdef uint32_t *values
         cdef uint32_t refcount
-        cdef void *key = NULL
+        cdef unsigned char *key = NULL
 
         while True:
             key = hashindex_next_key(self.index, key)
@@ -391,7 +391,7 @@ cdef class ChunkIndex(IndexBase):
         cdef uint32_t our_refcount, chunk_size, chunk_csize
         cdef const uint32_t *our_values
         cdef const uint32_t *master_values
-        cdef const void *key = NULL
+        cdef const unsigned char *key = NULL
         cdef HashIndex *master = master_index.index
 
         while True:
@@ -423,9 +423,9 @@ cdef class ChunkIndex(IndexBase):
         data[0] = _htole32(refs)
         data[1] = _htole32(size)
         data[2] = _htole32(csize)
-        self._add(<char*> key, data)
+        self._add(<unsigned char*> key, data)
 
-    cdef _add(self, void *key, uint32_t *data):
+    cdef _add(self, unsigned char *key, uint32_t *data):
         cdef uint64_t refcount1, refcount2, result64
         values = <uint32_t*> hashindex_get(self.index, key)
         if values:
@@ -442,7 +442,7 @@ cdef class ChunkIndex(IndexBase):
                 raise Exception('hashindex_set failed')
 
     def merge(self, ChunkIndex other):
-        cdef void *key = NULL
+        cdef unsigned char *key = NULL
 
         while True:
             key = hashindex_next_key(other.index, key)
@@ -451,7 +451,7 @@ cdef class ChunkIndex(IndexBase):
             self._add(key, <uint32_t*> (key + self.key_size))
 
     def zero_csize_ids(self):
-        cdef void *key = NULL
+        cdef unsigned char *key = NULL
         cdef uint32_t *values
         entries = []
         while True:
@@ -470,7 +470,7 @@ cdef class ChunkIndex(IndexBase):
 cdef class ChunkKeyIterator:
     cdef ChunkIndex idx
     cdef HashIndex *index
-    cdef const void *key
+    cdef const unsigned char *key
     cdef int key_size
     cdef int exhausted
 
@@ -485,7 +485,7 @@ cdef class ChunkKeyIterator:
     def __next__(self):
         if self.exhausted:
             raise StopIteration
-        self.key = hashindex_next_key(self.index, <char *>self.key)
+        self.key = hashindex_next_key(self.index, <unsigned char *>self.key)
         if not self.key:
             self.exhausted = 1
             raise StopIteration