Add the mdigest, mdsdb, and packdb modules.
PATCH 1/6 Add the mdigest, mdsdb, and packdb modules.
* The mdigest module manipulates and creates message digests. The files are mdigest.h and mdigest.c. These can be modified to include additional digests.
* The mdsdb module stores message digests for loose objects. The files are mdsdb.h and objd-mdsdb.c. The variable MDSDB in the Makefile selects the implementation (currently only objd-mdsdb.c, which keeps each digest in its own file).
* The packdb module stores digests in a space-efficient form. It is intended for unusual conditions (e.g., caching a digest for an object in an alternative object database that does not support digests). The implementation provided uses GDBM, however it is easy to add alternative implementation. The choice of implementations is determined by the value of the PACKDB variable in the Makefile (if undefined, packdb is not used).
These modules are mostly self-contained - there is little interaction with the rest of git beyond calling functions such as "die" for a few error conditions. Documentation for the functions in these modules is in the header files.
The Makefile changes will be in a subsequent commit.
--- gdbm-packdb.c | 249 +++++++++++++++++++++++++++++++++++++++++ mdigest.c | 221 +++++++++++++++++++++++++++++++++++++ mdigest.h | 334 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mdsdb.h | 192 ++++++++++++++++++++++++++++++++ objd-mdsdb.c | 340 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ packdb.h | 93 ++++++++++++++++ 6 files changed, 1429 insertions(+), 0 deletions(-) create mode 100644 gdbm-packdb.c create mode 100644 mdigest.c create mode 100644 mdigest.h create mode 100644 mdsdb.h create mode 100644 objd-mdsdb.c create mode 100644 packdb.h
gdbm-packdb.c new file +249 -0
@@ -0,0 +1,249 @@ +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/param.h> +#include <stdio.h> +#include <string.h> +#include <malloc.h> +#include <unistd.h> +#include <stdlib.h> +#include <fcntl.h> +#include <time.h> +#include <pthread.h> +#include <errno.h> +#include <gdbm.h> + +#include "cache.h" +#include "packdb.h" + +static void nsleep () { +#if _POSIX_C_SOURCE >=199309L +struct timespec ts; + ts.tv_sec =0 ; + ts.tv_nsec =100000 ; +nanosleep (&ts,NULL ); +#else +sleep (1 ); +#endif +} + +static int initialized =0 ; + +static GDBM_FILE dbf =NULL ; +char *dbf_name; +static int dbf_depth =0 ; + +pthread_mutex_t gdbm_mutex =PTHREAD_MUTEX_INITIALIZER ; + +static void packdb_close_nolock (void ); + +void packdb_init (void ) { +char *last; +pthread_mutex_lock (&gdbm_mutex); +if (initialized) { +pthread_mutex_unlock (&gdbm_mutex); +return ; + } + dbf_name =get_object_packdb_node (); + last =rindex (dbf_name,'/' ); + *last =0 ; +if (!access (dbf_name,R_OK |W_OK |X_OK )) { + initialized =1 ; + } + *last ='/' ; +pthread_mutex_unlock (&gdbm_mutex); +} + +int packdb_initialized (void ) { +return initialized; +} + +static void packdb_open_nolock (void ) { +if (dbf_depth ==0 ) { + AGAIN_W: + dbf =gdbm_open (dbf_name,0 ,GDBM_WRCREAT ,PERM_GROUP ,NULL ); +if (dbf ==NULL && gdbm_errno ==GDBM_CANT_BE_WRITER ) { +nsleep (); + goto AGAIN_W; + } + } + dbf_depth++; +} + +void packdb_open (void ) { +pthread_mutex_lock (&gdbm_mutex); +packdb_open_nolock (); +pthread_mutex_unlock (&gdbm_mutex); +} + + +int packdb_lookup (const unsigned char *sha1,mdigest_t *digestp) { +datum key; +datum ovalue; +pthread_mutex_lock (&gdbm_mutex); + +if (!initialized) { +pthread_mutex_unlock (&gdbm_mutex); +return -1 ; + } + + key.dptr = (char *)sha1; + key.dsize =20 ; + +packdb_open_nolock (); +if (dbf ==NULL ) { +packdb_close_nolock (); +pthread_mutex_unlock (&gdbm_mutex); +return -1 ; + } + ovalue =gdbm_fetch (dbf, key); +packdb_close_nolock (); +pthread_mutex_unlock (&gdbm_mutex); + +if (ovalue.dptr ==NULL )return 0 ; +if (digestp) { +int len; +int wsize = (int ) *(unsigned char *)(ovalue.dptr); +unsigned char *buffer = (unsigned char *)(ovalue.dptr +1 ); + len =get_mdigest_required_len (wsize); +if (len +1 > ovalue.dsize) +die ("existing db entry for %s corrupted [1], len = %d," +" ovalue.dsize = %d" , +sha1_to_hex (sha1), len, ovalue.dsize); +mdigest_load (digestp, wsize, buffer); + } +free (ovalue.dptr); +/* if (digestp) *digestp = (old_digest); */ +return 1 ; +} + +int packdb_remove (const unsigned char *sha1) { +datum key; +int result; +pthread_mutex_lock (&gdbm_mutex); +if ((!initialized) || dbf ==NULL ) { +pthread_mutex_unlock (&gdbm_mutex); +return -1 ; + } + key.dptr = (char *)sha1; + key.dsize =20 ; +packdb_open_nolock (); + result =gdbm_delete (dbf, key); +packdb_close_nolock (); +pthread_mutex_unlock (&gdbm_mutex); +return result; +} + + +int packdb_process (const unsigned char *sha1,mdigest_t *digestp) { +datum key; +datum nvalue; +datum ovalue; +mdigest_t newdigest = (*digestp); +mdigest_t old_digest; + newdigest.hdr.lhdr.wcode =get_mdigest_wcode (digestp); +pthread_mutex_lock (&gdbm_mutex); +if ((!initialized) || dbf ==NULL ) { +pthread_mutex_unlock (&gdbm_mutex); +return -1 ; + } + key.dptr = (char *)sha1; + key.dsize =20 ; + + nvalue.dptr = (char *)&(newdigest.hdr.lhdr.wcode); + nvalue.dsize =1 +get_mdigest_len (digestp); + +packdb_open_nolock (); + ovalue =gdbm_fetch (dbf, key); +if (dbf == dbf && ovalue.dptr ==NULL ) { +int status; + status =gdbm_store (dbf, key, nvalue,GDBM_INSERT ); +packdb_close_nolock (); +pthread_mutex_unlock (&gdbm_mutex); +switch (status) { +case 0 : +return 0 ; +case -1 : +error ("could not enter crc into database - key = %s" , +sha1_to_hex (sha1)); +return -1 ; +case 1 : +return 1 ; + } +return -1 ;/* should not occur */ + }else if (ovalue.dptr ==NULL ) { +packdb_close_nolock (); +pthread_mutex_unlock (&gdbm_mutex); +return 0 ; + }else { +int wcode, len; +unsigned char *buffer; +packdb_close_nolock (); +pthread_mutex_unlock (&gdbm_mutex); + wcode = (int ) *(unsigned char *)ovalue.dptr; + len =get_mdigest_required_len (wcode); +if (len +1 > ovalue.dsize) +die ("existing db entry for %s corrupted [2]" , +sha1_to_hex (sha1)); + buffer = (unsigned char *) ovalue.dptr +1 ; +mdigest_load (&old_digest, wcode, buffer); +free (ovalue.dptr); +/* +* Both old_digest and newdigest are in network byte order. +*/ +if (mdigest_tst (&old_digest, digestp)) { +die ("SHA1 COLLISION WHEN INSERTING OBJECT %s" , +sha1_to_hex (sha1)); +return -1 ; + } +return 1 ; + } +} + +int packdb_reorganize () { +int status; +pthread_mutex_lock (&gdbm_mutex); +if ((!initialized) || dbf ==NULL ) { +pthread_mutex_unlock (&gdbm_mutex); +return -1 ; + } +packdb_open_nolock (); + status =gdbm_reorganize (dbf); +packdb_close_nolock (); +pthread_mutex_unlock (&gdbm_mutex); +return status; +} + + +static void packdb_close_nolock (void ) { +if (!initialized) { +return ; + } + dbf_depth--; +if (dbf_depth ==0 && dbf !=NULL ) { +gdbm_close (dbf); + dbf =NULL ; + } +if (dbf_depth <0 ) { +die ("packdb dbf_depth %d < 0" , dbf_depth); + } +return ; +} + +void packdb_close (void ) { +pthread_mutex_lock (&gdbm_mutex); +packdb_close_nolock (); +pthread_mutex_unlock (&gdbm_mutex); +} + +void packdb_finish (void ) { +pthread_mutex_lock (&gdbm_mutex); +if (!initialized) { +pthread_mutex_unlock (&gdbm_mutex); +return ; + } +if (dbf !=NULL )gdbm_close (dbf); + dbf =NULL ; + dbf_depth =0 ; + initialized =0 ; +pthread_mutex_unlock (&gdbm_mutex); +}
mdigest.c new file +221 -0
221 lines not on this page · raw message
mdigest.h new file +334 -0
334 lines not on this page · raw message
mdsdb.h new file +192 -0
192 lines not on this page · raw message
objd-mdsdb.c new file +340 -0
340 lines not on this page · raw message
packdb.h new file +93 -0
93 lines not on this page · raw message
Signature · 1 line
1.7.1