//--------------------------------------------------------------------- //--------------------------------------------------------------------- static void writeMD5(LPCTSTR fname, LPCTSTR md5) { kjm::_tstring md5fname = kjm::util::renameExtension(fname, _T(".md5")); kjm::cfile md5file(md5fname, _T("w")); if (md5file.is_open()) { _ftprintf(md5file.get_handle(), _T("%s *%s\n"), md5, kjm::util::findFileName(fname).c_str()); } }
//--------------------------------------------------------------------- //--------------------------------------------------------------------- static kjm::_tstring readMD5(LPCTSTR fname) { kjm::_tstring result; kjm::cfile md5file(fname, _T("r")); if (md5file.is_open()) { kjm::_tstring line; if (md5file.getline(line) >= 0) { kjm::_tstrings cols = kjm::util::split(line, _T(' ')); result = cols.at(0); } } return result; }
void CKKFile::GetMD5() { MD5_CTX fileMD5; char tmpbuf[5]; int i; md5file(fileName,0,&fileMD5); strcpy(MD5hexview,""); for (i=0;i<16;i++) { sprintf_s(tmpbuf,"%.2X",fileMD5.digest[i]); strcat(MD5hexview,tmpbuf); } };
void * md5thread(void * arguments){ mdparam * ptr = (mdparam *) arguments; md5file(ptr->filename, ptr->result); pthread_mutex_lock(&ptr->link->mylock); ptr->link->t_finished++; pthread_mutex_unlock(&ptr->link->mylock); free(ptr->filename); free(ptr); if (!pthread_detach(pthread_self())){ return; } }
/* * Get cached md5sum of a file. If none found, or not up to date, if the file is source * try to recalculate the sum. If not, then return empty sum (all zeroes). */ int getcachedmd5sum( TARGET *t, int source ) { HCACHEDATA cachedata, *c = &cachedata; int use_cache = 1; HCACHEFILE *file; const char *target = t->boundname; # ifdef DOWNSHIFT_PATHS char path[ MAXJPATH ]; char *p; #endif if ( t->contentmd5sum_calculated ) return t->contentmd5sum_changed; if (!source) { memset(&t->buildmd5sum, 0, sizeof(t->buildmd5sum)); memset(&t->contentmd5sum, 0, sizeof(t->contentmd5sum)); t->contentmd5sum_calculated = 1; t->contentmd5sum_changed = 0; return t->contentmd5sum_changed; } file = hcachefile_get( t ); ++queries; # ifdef DOWNSHIFT_PATHS p = path; do *p++ = (char)tolower( *target ); while( *target++ ); target = path; # endif c->boundname = target; if( hashcheck( file->hcachehash, (HASHDATA **) &c ) ) { if ( t->time == 0 ) { /* This file was generated. Grab its timestamp. */ file_time( c->boundname, &c->mtime ); } else if( c->mtime != t->time ) use_cache = 0; if ( use_cache ) { use_cache = memcmp(md5sumempty, &c->contentmd5sum, sizeof(c->contentmd5sum)) != 0; } if( use_cache ) { if( DEBUG_MD5HASH ) printf( "- content md5: %s (%s)\n", t->boundname, md5tostring(c->contentmd5sum)); c->age = 0; /* The entry has been used, its young again */ ++hits; t->contentmd5sum_changed = 0; memcpy(&t->contentmd5sum, &c->contentmd5sum, sizeof(t->contentmd5sum)); t->contentmd5sum_calculated = 1; return t->contentmd5sum_changed; } else { if( DEBUG_MD5HASH ) printf( "md5 cache out of date for %s (time %d, md5time %d)\n", t->boundname , (int)t->time, (int)c->mtime ); } } else { if( hashenter( file->hcachehash, (HASHDATA **)&c ) ) { c->boundname = newstr( c->boundname ); c->next = file->hcachelist; file->hcachelist = c; c->time = 0; c->includes = NULL; c->hdrscan = NULL; } } file->dirty = 1; /* 'c' points at the cache entry. Its out of date. */ { MD5SUM origmd5sum; #ifdef OPT_BUILTIN_LUA_SUPPORT_EXT LIST *md5callback; #endif memcpy( &origmd5sum, &c->contentmd5sum, sizeof( MD5SUM ) ); #ifdef OPT_BUILTIN_LUA_SUPPORT_EXT pushsettings( t->settings ); md5callback = var_get( "MD5CALLBACK" ); popsettings( t->settings ); if ( md5callback ) { luahelper_md5callback(t->boundname, c->contentmd5sum, md5callback->string); } else { #endif md5file( t->boundname, c->contentmd5sum ); #ifdef OPT_BUILTIN_LUA_SUPPORT_EXT } #endif t->contentmd5sum_changed = memcmp( &origmd5sum, &c->contentmd5sum, sizeof( MD5SUM ) ) != 0; } if( DEBUG_MD5HASH ) printf( "- content md5: %s (%s)\n", t->boundname, md5tostring(c->contentmd5sum)); c->mtime = t->time; if ( c->mtime == 0 ) { /* This file was generated. Grab its timestamp. */ file_time( c->boundname, &c->mtime ); } c->age = 0; memcpy(&t->contentmd5sum, &c->contentmd5sum, sizeof(t->contentmd5sum)); t->contentmd5sum_calculated = (char)(memcmp(md5sumempty, &t->contentmd5sum, sizeof(t->contentmd5sum)) != 0); memset(&t->buildmd5sum, 0, sizeof(t->buildmd5sum)); return t->contentmd5sum_changed; }
void filecache_update(TARGET *t) { MD5SUM blobmd5sum; int haveblobmd5sum = 0; const char *cachedname; const char *blobname; int cacheerror; if (!t->filecache_generate) return; /* If the buildmd5sum is empty, then the file doesn't exist. */ cacheerror = ismd5empty(t->buildmd5sum); if (cacheerror) return; haveblobmd5sum = 0; cachedname = filecache_getfilename(t, t->buildmd5sum, NULL); if (!cachedname) return; /* Search for the appropriate .link file that matches the target. */ haveblobmd5sum = filecache_findlink(cachedname, blobmd5sum); /* If we weren't able to determine the target md5sum, do it now. */ if (!haveblobmd5sum) { #ifdef OPT_BUILTIN_LUA_SUPPORT_EXT LIST *md5callback; pushsettings( t->settings ); md5callback = var_get( "MD5CALLBACK" ); popsettings( t->settings ); if ( md5callback ) { luahelper_md5callback(t->boundname, blobmd5sum, md5callback->string); } else { #endif md5file(t->boundname, blobmd5sum); #ifdef OPT_BUILTIN_LUA_SUPPORT_EXT } #endif memcpy(t->contentmd5sum, blobmd5sum, sizeof(MD5SUM)); if (ismd5empty(t->contentmd5sum)) return; } { /* Is the blob already there? */ time_t blobtime; blobname = filecache_getfilename(t, blobmd5sum, ".blob"); if (file_time(blobname, &blobtime) == -1) { time_t blobpartialtime; const char *blobpartialname; if(DEBUG_MD5HASH) printf("Caching %s as %s\n", t->name, cachedname); else printf("Caching %s\n", t->name); /* Write the new .blob to the cache. */ blobpartialname = filecache_getfilename(t, blobmd5sum, ".blob.partial"); if (file_time(blobpartialname, &blobpartialtime) == -1) { if (copyfile(blobpartialname, t->boundname, &blobmd5sum) == 0 || rename(blobpartialname, blobname) != 0) { printf("** Unable to write %s to cache.\n", t->name, cachedname); filecache_disable(t); return; } } } } /* Write the new .link file to the cache. */ { FILE *file; BUFFER linknamebuff; buffer_init(&linknamebuff); buffer_addstring(&linknamebuff, cachedname, strlen(cachedname)); buffer_addchar(&linknamebuff, '-'); buffer_addstring(&linknamebuff, md5tostring(blobmd5sum), 32); buffer_addstring(&linknamebuff, ".link", 5); buffer_addchar(&linknamebuff, 0); file_mkdir(buffer_ptr(&linknamebuff)); file = fopen(buffer_ptr(&linknamebuff), "wb"); if (file) { write_md5sum(file, blobmd5sum); write_string(file, t->name); fclose(file); } buffer_free(&linknamebuff); } }
void ProcessChanges(wchar_t* dir){ HANDLE hFind = INVALID_HANDLE_VALUE; WIN32_FIND_DATA ffd; TCHAR szDir[MAX_PATH]; StringCchCopy(szDir, MAX_PATH, dir); StringCchCat(szDir, MAX_PATH, TEXT("\\*")); stringstream idfile; idfile << ws2s(dir) << "\\.id"; stringstream revfile; revfile << ws2s(dir) << "\\.rev"; string id = readSingleLine(idfile.str().c_str()); string rev = readSingleLine(revfile.str().c_str()); if ( id.length() == 0 ){ return; } hFind = FindFirstFile(szDir, &ffd); bool hasUploadedAttachments = false; do { if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { } else { if ( wcscmp(L".id", ffd.cFileName) != 0 && wcscmp(L".rev", ffd.cFileName) != 0 ){ wchar_t filename[MAX_PATH]; StringCchCopy(filename, MAX_PATH, dir); StringCchCat(filename, MAX_PATH, L"\\"); StringCchCat(filename, MAX_PATH, ffd.cFileName); string c = md5file(filename); /* Compare to content md5 of attachment in the db */ Document doc = db.getDocument(id, rev); try { Attachment a = doc.getAttachment(ws2s(ffd.cFileName)); string serverMD5 = a.getContentMD5(); const char* j = serverMD5.c_str(); int i = strcmp(c.c_str(), j); if ( i != 0 ) { doc.updateAttachmentFromFile(a.getID(), ws2s(filename)); hasUploadedAttachments = true; } }catch(AttachmentNotFoundException e){ doc.addAttachmentFromFile(ws2s(ffd.cFileName), "", ws2s(filename)); hasUploadedAttachments = true; } } } } while (FindNextFile(hFind, &ffd) != 0); FindClose(hFind); if ( hasUploadedAttachments ){ PostMessage(GetParent(attachmentsHwnd), WM_ATTACHMENTS_UPLOADED, 0, 0); } }
int serialize(char *filepath, void *dynamic){ FILE * none = NULL; none = fopen("/dev/null", "w"); char * temp = NULL; //Let the code know what datatype this is. dynamic_t * ptr = (dynamic_t *) dynamic; mdparam * threadVar = malloc(sizeof(mdparam)); pthread_t tid = 0; int length = 0; char thiscwd [PATH_MAX]; char * md5 = NULL; char * string = NULL; char * filename = NULL; struct stat info; int status = 0; int boolean = 0; mode_t mask = 0; mask = S_IROTH | S_IWOTH ; if(getcwd(thiscwd, PATH_MAX) == ""){ fprintf(stderr, "Couldn't get current working directory!\n"); exit(1); } //Append current working directory with the file's name! temp = thiscwd; if(temp[strlen(temp) - 1] != '/'){ strcat(temp, "/"); } strcat(temp, filepath); if(temp[strlen(temp) - 1] == '/'){ temp[strlen(temp) - 1] == '\0'; } if(lstat(filepath, &info) < 0){ free(threadVar); status = 0; } else if( S_ISDIR(info.st_mode)){ free(threadVar); status = 1; } else if( S_ISREG(info.st_mode)){ status = 0; //If the file's size is small enough, or if the number of //running threads exceeds 50... pthread_mutex_lock(&ptr->mylock); if(info.st_size < 100000 || (ptr->t_created - ptr->t_finished) >= 50){ boolean = 1; //mark it so that it won't be made into a thread. free(threadVar); //Won't need a static struct //if the thread isn't being made. } else{//set up the path name to be passed into the thread. filename = malloc(strlen(temp) + 1); strcpy(filename, temp); } pthread_mutex_unlock(&ptr->mylock); } else if( S_ISLNK(info.st_mode)){ free(threadVar); status = 0; } else{ free(threadVar); status = 0; } //If the permissions are set to owner-only if((info.st_mode & mask) == 0){ status = 0; } length = fprintf(none,"%s/// %lld %lld %lld %lld %lld %lld %lld ", temp, (long long) info.st_dev, (long long) info.st_mode, (long long) info.st_uid, (long long) info.st_gid, (long long) info.st_size, (long long) info.st_mtime, (long long) info.st_ctime); //Including md5sum! string = malloc(length + 33); sprintf(string, "%s/// %lld %lld %lld %lld %lld %lld %lld ", temp, (long long) info.st_dev,(long long) info.st_mode, (long long) info.st_uid, (long long) info.st_gid, (long long) info.st_size, (long long) info.st_mtime, (long long) info.st_ctime); md5 = string + length; //Set up all the md5 digits in serialize to 32 1's as default memset(md5, '1', 32); string[length + 32] = '\0'; adddynamic(ptr, string); //Increment count of thread's made. if(S_ISREG(info.st_mode)){ if(boolean == 0){ threadVar->filename = filename; threadVar->result = md5; threadVar->link = ptr; ptr->t_created++; if( pthread_create( &tid, NULL, md5thread, threadVar)){ //finished thread increments from error... fprintf(stderr, "pthread failure"); free(filename); free(threadVar); pthread_mutex_lock(&ptr->mylock); ptr->t_finished++; pthread_mutex_unlock(&ptr->mylock); } } else{ md5file(temp, md5); } } fclose(none); return status; }
/* * Get cached md5sum of a file. If none found, or not up to date, if the file is source * try to recalculate the sum. If not, then return empty sum (all zeroes). */ int getcachedmd5sum( TARGET *t, int forcetimecheck ) { CHECKSUMDATA checksumdata, *c = &checksumdata; checksums_readfile(); if ( t->contentchecksum == NULL ) { const char *target = t->boundname; # ifdef DOWNSHIFT_PATHS char path[ MAXJPATH ]; char *p = path; do *p++ = (char)tolower( *target ); while( *target++ ); target = path; # endif c->boundname = target; if ( !hashcheck( checksumhash, (HASHDATA **) &c ) ) { if ( hashenter( checksumhash, (HASHDATA **)&c ) ) { c->boundname = newstr( c->boundname ); c->next = checksumdatalist; checksumdatalist = c; c->mtime = 0; memset( &c->contentmd5sum, 0, MD5_SUMSIZE ); c->contentmd5sum_changed = 1; c->contentmd5sum_calculated = 0; c->age = 0; } } t->contentchecksum = c; } else { c = t->contentchecksum; } if ( !c->contentmd5sum_calculated || forcetimecheck || t->time != c->mtime || c->mtime == 0 ) { /* This file may have generated or updated in another fashion. Grab its timestamp. */ time_t ftime; c->contentmd5sum_calculated = 1; // if ( file_time( c->boundname, &ftime ) == -1 ) { timestamp( c->boundname, &ftime, 0 ); if ( ftime == 0 ) { /* This file is not present anymore. */ c->age = 0; /* The entry has been used, its young again */ c->mtime = 0; c->contentmd5sum_changed = 1; memset( &c->contentmd5sum, 0, MD5_SUMSIZE ); checksumsdirty = 1; return 1; } /* 'c' points at the cache entry. Its out of date. */ if ( ftime != c->mtime ) { MD5SUM origmd5sum; #ifdef OPT_BUILTIN_LUA_SUPPORT_EXT SETTINGS *md5callback; #endif memcpy( &origmd5sum, &c->contentmd5sum, sizeof( MD5SUM ) ); #ifdef OPT_BUILTIN_LUA_SUPPORT_EXT md5callback = quicksettingslookup( t, "MD5CALLBACK" ); if ( md5callback && md5callback->value ) { luahelper_md5callback(t->boundname, c->contentmd5sum, list_value(list_first(md5callback->value))); } else { #endif md5file( t->boundname, c->contentmd5sum ); #ifdef OPT_BUILTIN_LUA_SUPPORT_EXT } #endif c->mtime = ftime; c->contentmd5sum_changed = memcmp( &origmd5sum, &c->contentmd5sum, sizeof( MD5SUM ) ) != 0; } //if( DEBUG_MD5HASH ) //printf( "- content md5: %s (%s)\n", t->boundname, md5tostring(c->contentmd5sum)); c->age = 0; checksumsdirty = 1; } return c->contentmd5sum_changed; #if 0 if( DEBUG_MD5HASH ) printf( "md5 cache out of date for %s (time %d, md5time %d)\n", t->boundname , (int)t->time, (int)c->mtime ); #endif }