📄 dsmcc-cache.c
字号:
for(file = file_cache; file != NULL; file = file->next) { if((file->carousel_id == car_id) && (file->module_id == module_id) && CacheKeyCmp(file->key, key, file->key_len, key_len)) { return file; } } /* Scan through known files and return details if known else NULL */ file = CacheScanFile(gateway, car_id, module_id, key_len, key); return file;}voidCache::CacheDirInfo(unsigned short module_id, unsigned int objkey_len, char *objkey, struct biop_binding *bind) { struct cache_dir *dir, *last, *subdir; struct cache_file *file; dir = CacheDirFind(bind->ior.body.full.obj_loc.carousel_id, bind->ior.body.full.obj_loc.module_id, bind->ior.body.full.obj_loc.objkey_len, bind->ior.body.full.obj_loc.objkey); if(dir != NULL) { return; } /* Already got (check version TODO) */ dir = (struct cache_dir *)malloc(sizeof(struct cache_dir)); dir->name = (char *)malloc(bind->name.comps[0].id_len); memcpy(dir->name, bind->name.comps[0].id, bind->name.comps[0].id_len); dir->dirpath = NULL; dir->next = dir->prev = dir->sub = NULL; dir->files = NULL; dir->carousel_id = bind->ior.body.full.obj_loc.carousel_id; dir->module_id = bind->ior.body.full.obj_loc.module_id; dir->key_len = bind->ior.body.full.obj_loc.objkey_len; dir->key = (char *)malloc(dir->key_len); memcpy(dir->key, bind->ior.body.full.obj_loc.objkey, dir->key_len);// dir->p_carousel_id = carousel_id; Must be the same ? dir->p_module_id = module_id; dir->p_key_len = objkey_len; dir->p_key = (char *)malloc(dir->p_key_len); memcpy(dir->p_key, objkey, objkey_len); dir->parent = CacheDirFind(dir->carousel_id, module_id, objkey_len, objkey);// fprintf(cache_fd,"Caching dir %s\n", dir->name); if(dir->parent == NULL) { if(dir_cache == NULL) { dir_cache = dir; } else { /* Directory not yet known. Add this to dirs list */ for(last=dir_cache;last->next!=NULL;last=last->next){;}// fprintf(cache_fd,"Added to Unknown list not empty\n"); last->next = dir; dir->prev = last; } } else {// fprintf(cache_fd,"Caching dir under parent %s\n", dir->parent->name); /* Create under parent directory */ if(dir->parent->sub == NULL) {// fprintf(cache_fd,"Parent has no subdirs\n"); dir->parent->sub = dir; } else {// fprintf(cache_fd,"Parent has other subdirs\n"); for(last=dir->parent->sub;last->next!=NULL;last=last->next) { ; } last->next = dir; dir->prev = last;// fprintf(cache_fd,"Added to Parent has other subdirs\n"); } }// fprintf(cache_fd,"Dir Cached (%s)\n", dir->name ); /* Attach any files that arrived previously */ for(file = file_cache; file != NULL; file = file->next) { CacheAttachFile(dir, file); } /* Attach any subdirs that arrived beforehand */ for(subdir = dir_cache; subdir != NULL; subdir = subdir->next) { CacheAttachDir(dir, subdir); } if((dir->parent!=NULL)&&(dir->parent->dirpath!=NULL)) CacheWriteDir(dir); /* Write dir/files to filesystem */ num_dirs++; total_dirs++;}voidCache::CacheWriteDir(struct cache_dir *dir) { struct cache_dir *subdir; struct cache_file *file; char dirbuf[256]; if(dir->dirpath == NULL) { dir->dirpath= (char*) malloc(strlen(dir->parent->dirpath)+strlen(dir->name)+2); strcpy(dir->dirpath, dir->parent->dirpath); strcat(dir->dirpath, "/"); strcat(dir->dirpath, dir->name); } sprintf(dirbuf, "%s/%s/%s", "/tmp/cache/", name, dir->dirpath); mkdir(dirbuf, 0755); /* Write out files that had arrived before directory */ for(file=dir->files;file!=NULL;file=file->next) { if(file->data != NULL) CacheWriteFile(file); } /* Recurse thorugh child directories */ for(subdir=dir->sub;subdir!=NULL;subdir=subdir->next) { CacheWriteDir(subdir); }} voidCache::CacheFile(struct biop_message *bm, struct cache_module_data *cachep) { struct cache_file *file; /* search for file info */ file = CacheFileFind(cachep->carousel_id, cachep->module_id, bm->hdr.objkey_len, bm->hdr.objkey); if(file == NULL) { /* Not known yet. Save data */ file = (struct cache_file *)malloc(sizeof(struct cache_file)); file->data_len = bm->body.file.content_len; file->data = (char*)malloc(file->data_len); memcpy(file->data, cachep->data+cachep->curp, file->data_len); file->carousel_id= cachep->carousel_id; file->module_id= cachep->module_id; file->key_len= bm->hdr.objkey_len; file->key= (char*)malloc(file->key_len); memcpy(file->key, bm->hdr.objkey, file->key_len); file->next = file->prev = NULL; // Add to unknown data cache if(data_cache == NULL) { data_cache = file; } else { struct cache_file *last; for(last=data_cache;last->next!=NULL;last=last->next){;} last->next = file; file->prev = last; } } else { /* Save data. Save file if wanted (TODO check versions ) */// fprintf(cache_fd, "Found file %s\n", file->filename); if(file->data == NULL) { file->data_len = bm->body.file.content_len; file->data = (char *)malloc(file->data_len); memcpy(file->data,cachep->data+cachep->curp, file->data_len); /* TODO this should be a config option */ CacheWriteFile(file); } }}voidCache::CacheWriteFile(struct cache_file *file) { FILE *data_fd; char buf[128]; /* TODO create directory structure rather than one big mess! */// fprintf(cache_fd,"Writing file %s (%d bytes)\n", file->filename, file->data_len); if((file->parent!=NULL) && (file->parent->dirpath != NULL)) { sprintf(buf,"/tmp/cache/%s/%s/%s", name, file->parent->dirpath, file->filename); data_fd = fopen(buf, "w"); fwrite(file->data, 1, file->data_len, data_fd); fclose(data_fd); /* Free data as no longer needed */ free(file->data); file->data = NULL; file->data_len = 0; num_files--;// esyslog("%d of %d files now received", num_files, total_files); }}voidCache::CacheUnknownDirInfo(struct cache_dir *newdir) { struct cache_dir *last; if(dir_cache == NULL) { dir_cache = newdir; newdir->next = newdir->prev = NULL; } else { for(last=dir_cache;last->next!=NULL;last=last->next) { ; } last->next = newdir; newdir->prev = last; newdir->next = NULL; }}voidCache::CacheUnknownFileInfo(struct cache_file *newfile) { struct cache_file *last;/* TODO Check if already unknown file (i.e. data arrived twice before * dir/srg or missed dir/srg message, if so skip. */ if(file_cache == NULL) { file_cache = newfile; file_cache->next = file_cache->prev = NULL; } else { for(last=file_cache;last->next!=NULL; last = last->next) { ; } last->next = newfile; newfile->prev = last; newfile->next = NULL; }}struct cache_file *Cache::CacheFileFindData(unsigned long car_id, unsigned short mod_id, unsigned int key_len, char *key) { struct cache_file *last; for(last=data_cache; last!=NULL; last = last->next) { if((last->carousel_id==car_id) && (last->module_id==mod_id) && CacheKeyCmp(key, last->key, key_len, last->key_len)) { if(last->prev != NULL) { last->prev->next = last->next; } else { data_cache = last->next; } if(last->next != NULL) last->next->prev = last->prev; break; } } return last;}voidCache::CacheFileInfo(unsigned short mod_id, unsigned int key_len, char *key, struct biop_binding *bind) { struct cache_file *newfile, *last; struct cache_dir *dir; // Check we do not already have file (or file info) cached if(CacheFileFind(bind->ior.body.full.obj_loc.carousel_id, bind->ior.body.full.obj_loc.module_id, bind->ior.body.full.obj_loc.objkey_len, bind->ior.body.full.obj_loc.objkey) != NULL) { return; } // See if the data had already arrived for the file newfile = CacheFileFindData(bind->ior.body.full.obj_loc.carousel_id, bind->ior.body.full.obj_loc.module_id, bind->ior.body.full.obj_loc.objkey_len, bind->ior.body.full.obj_loc.objkey); if(newfile == NULL) { // Create the file from scratch newfile = (struct cache_file*)malloc(sizeof(struct cache_file)); newfile->carousel_id = bind->ior.body.full.obj_loc.carousel_id; newfile->module_id = bind->ior.body.full.obj_loc.module_id; newfile->key_len = bind->ior.body.full.obj_loc.objkey_len; newfile->key= (char *)malloc(newfile->key_len); memcpy(newfile->key, bind->ior.body.full.obj_loc.objkey, newfile->key_len); newfile->data = NULL; } newfile->filename = (char*)malloc(bind->name.comps[0].id_len); memcpy(newfile->filename, bind->name.comps[0].id, bind->name.comps[0].id_len); newfile->next = NULL; dir = CacheDirFind(newfile->carousel_id, mod_id, key_len, key); num_files++; total_files++;// esyslog("%d files now in total (added %s)", total_files, newfile->filename); if(dir == NULL) { /* Parent directory not yet known */ newfile->p_module_id = mod_id; newfile->p_key_len = key_len; newfile->p_key = (char *)malloc(newfile->p_key_len); memcpy(newfile->p_key, key, key_len); newfile->parent = NULL; CacheUnknownFileInfo(newfile); } else { /* TODO Check if already stored under directory (new version?) * Checking version info for a file is difficult, * the data should not be passed to us by dsmcc layer * unless the version has changed. Need to remove old * and store new. */ /* If not append to list */ newfile->p_key_len = 0; newfile->p_key = NULL; newfile->parent = dir; if(dir->files == NULL) { dir->files = newfile; newfile->prev = NULL; } else { for(last=dir->files;last->next!=NULL;last=last->next){;} last->next = newfile; newfile->prev = last; } if(newfile->data != NULL) CacheWriteFile(newfile); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -