⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dsmcc-cache.c

📁 Parses UK Profile 1.05/1.06 Object Carousel and saves files to disk (all stored under /tmp/cache at
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <stdlib.h>#include <stdio.h>#include "dsmcc-cache.h"#include "dsmcc-biop.h"#include "dsmcc-receiver.h"#include "dsmcc-descriptor.h"// #include <mpatrol.h>FILE *cache_fd = NULL;/* TODO This should be stored in obj_carousel structure  */Cache::Cache(const char *channel_name) {	/* TODO - load cache from disk into obj_carousel */	gateway = dir_cache = NULL;	file_cache = NULL;	data_cache = NULL;	/* Write contents under channel name */	if(channel_name) {		name = (char*)malloc(strlen(channel_name)+1);		strcpy(name, channel_name);	} else {		name = (char*)malloc(1);		name = '\0';	}	mkdir("/tmp/cache", 0755);	/* By popular demand... */	num_files = num_dirs = total_files = total_dirs = 0;}Cache::~Cache() {	struct cache_file *f, *fn;	struct cache_dir *d, *dn;	/* Free unconnected files */	f = file_cache;	while(f!=NULL) {	  fn = f->next;	  if(f->key_len>0) { free(f->key); }	  if(f->filename!=NULL) { free(f->filename); }	  if(f->data!=NULL) { free(f->data); }	  if(f->p_key_len>0) { free(f->p_key); }	  free(f);	  f = fn;	}	/* Free cached data */	f = data_cache;	while(f!=NULL) {	  fn = f->next;	  if(f->key_len>0) { free(f->key); }	  if(f->data!=NULL) { free(f->data); }	  free(f);	  f = fn;	}	/* Free unconnected dirs */	d = dir_cache;	while(d!=NULL) {	  dn = d->next;	  if(d->name!=NULL) { free(d->name); }	  if(d->dirpath!=NULL) { free(d->dirpath); }	  if(d->key_len>0) { free(d->key); }	  if(d->p_key_len>0) { free(d->p_key); }	  f = d->files;	  while(f!=NULL) {	    fn = f->next;	    if(f->key_len>0) { free(f->key); }	    if(f->filename!=NULL) { free(f->filename); }	    if(f->data!=NULL) { free(f->data); }	    if(f->p_key_len>0) { free(f->p_key); }	    free(f);	    f = fn;	  }	  free(d);	  d = dn;	}	/* Free cache - TODO improve this */	if(gateway != NULL)		CacheFreeDir(gateway);	file_cache = data_cache = NULL;	gateway = dir_cache = NULL;	if(name)		free(name);}voidCache::CacheFreeDir(struct cache_dir *d) {	struct cache_dir *dn, *dnn;	struct cache_file *f, *fn;	if(d->sub!=NULL) {		dn = d->sub;		while(dn!=NULL) {		  dnn = dn->next;		  CacheFreeDir(dn);		  dn = dnn;		}	}	if(d->name!=NULL) { free(d->name); }	if(d->dirpath!=NULL) { free(d->dirpath); }	if(d->key_len>0) { free(d->key); }	if(d->p_key_len>0) { free(d->p_key); }	f = d->files;	while(f!=NULL) {	  fn = f->next;	  if(f->key_len>0) { free(f->key); }	  if(f->filename!=NULL) { free(f->filename); }	  if(f->data!=NULL) { free(f->data); }	  if(f->p_key_len>0) { free(f->p_key); }	  free(f);	  f = fn;	}	free(d);}unsigned intCache::CacheKeyCmp(char *str1, char *str2, unsigned int len1, unsigned int len2) {        unsigned int i;        /* Key Len must be equal */        if(len1 != len2) { return 0; }        for(i = 0; i < len1; i++) {                if(str1[i] != str2[i]) {                        return 0;                }        }        return 1;}struct cache_dir *Cache::CacheScanDir(struct cache_dir *dir, unsigned long car_id, unsigned short module_id, unsigned int key_len, char *key) {        struct cache_dir *founddir, *subdir;        if(dir == NULL) { return NULL; }        if((dir->carousel_id == car_id) && (dir->module_id == module_id) &&                        CacheKeyCmp(dir->key, key, dir->key_len, key_len)) {                        return dir;        }        /* Search sub dirs */        for(subdir = dir->sub; subdir != NULL; subdir=subdir->next) {                founddir = CacheScanDir(subdir, car_id, module_id, key_len,key);                if(founddir != NULL)                         return founddir;        }        return NULL;}struct cache_dir *Cache::CacheDirFind(unsigned long car_id, unsigned short module_id, unsigned int key_len, char *key) {        struct cache_dir *dir, *fdir;	struct cache_file *file;	//      fprintf(cache_fd,"Searching for dir %d/%d/(key)\n", module_id, key_len);        /* Scan through known dirs and return details if known else NULL */        if(module_id == 0 && key_len == 0) {                /* Return gateway object. Create if not already */                if(gateway == NULL) {                        gateway = (struct cache_dir *)malloc(sizeof(struct cache_dir));                        gateway->name = (char *)malloc(2);			gateway->carousel_id = car_id;			gateway->module_id = gateway->key_len = 					gateway->p_key_len = 0;	/*TODO argg! a hack to fix a bug caused by a hack.Need better linking */                        strcpy(gateway->name, "/");			gateway->dirpath = (char *)malloc(2);                        strcpy(gateway->dirpath, "/");                        gateway->sub = gateway->parent = 				gateway->prev = gateway->next = NULL;                        gateway->files = NULL;                        /* Attach any subdirs or files that arrived prev. */			for(file=file_cache;file!=NULL;file=file->next)				CacheAttachFile(gateway, file);			for(fdir=dir_cache;fdir!=NULL;fdir=fdir->next) 				CacheAttachDir(gateway, fdir);					CacheWriteDir(gateway);	/* Write files to filesystem */                        return gateway;                } else {                        return gateway;                }        }        /* Find dir magic */        dir = CacheScanDir(gateway, car_id, module_id, key_len, key);	if(dir == NULL) {	/* Try looking in unlinked dirs list */          for(fdir=dir_cache;(dir==NULL)&&(fdir!=NULL); fdir = fdir->next) {		dir = CacheScanDir(fdir, car_id, module_id, key_len, key);	  }        }        /* TODO - Directory not known yet, cache it ? */        return dir;}voidCache::CacheAttachFile(struct cache_dir *root, struct cache_file *file) {	struct cache_file *cfile;	/* Search for any files that arrived previously in unknown files list*/	if((file->carousel_id == root->carousel_id) &&	   (file->p_module_id == root->module_id) &&	   CacheKeyCmp(file->p_key, root->key,file->p_key_len,root->key_len)) {	      if(root->files == NULL) {		if(file->prev!=NULL) {			file->prev->next = file->next;		} else {			file_cache=file->next;		}		if(file->next!=NULL)			file->next->prev = file->prev; 			        root->files = file;		root->files->next = root->files->prev = NULL;		file->parent = root;	      } else {	  	if(file->prev!=NULL) {			file->prev->next = file->next;		} else {			file_cache=file->next;		}		if(file->next!=NULL)			file->next->prev = file->prev;		for(cfile=root->files;cfile->next!=NULL;cfile=cfile->next){;}		cfile->next = file;		file->prev = cfile;		file->next = NULL;	/* TODO uurrgh */		file->parent = root;	      }	}		    }voidCache::CacheAttachDir(struct cache_dir *root, struct cache_dir *dir) {	struct cache_dir *last;	   if((dir->carousel_id == root->carousel_id) &&      (dir->p_module_id == root->module_id) &&      CacheKeyCmp(dir->p_key,root->key,dir->p_key_len,root->key_len)) {	if(root->sub == NULL) {	  if(dir->prev != NULL) { 		dir->prev->next = dir->next; 	  } else {		dir_cache = dir->next;	  }	  if(dir->next!=NULL) 		dir->next->prev = dir->prev;	  root->sub = dir;	  root->sub->next = root->sub->prev = NULL;	  dir->parent = root;	} else {	  if(dir->prev!=NULL) {		dir->prev->next = dir->next;	  } else {		dir_cache = dir->next;	  }	  if(dir->next!=NULL)		dir->next->prev = dir->prev;	  for(last=root->sub;last->next!=NULL;last=last->next) { ; }	  last->next = dir;	  dir->prev = last;	  dir->next = NULL;	  dir->parent = root;	}   }}struct cache_file *Cache::CacheScanFile(struct cache_dir *dir, unsigned long car_id, unsigned int mod_id, unsigned int key_len, char *key) {        struct cache_file *file;        struct cache_dir *subdir;        if(dir == NULL) { return NULL; }/*      fprintf(cache_fd,"Searching for file %d - \n", mod_id);        for(i = 0; i < key_len; i++) {                fprintf(cache_fd, "%c", key[i]);        }        fprintf(cache_fd, "\n");*/        /* Search files in this dir */        for(file = dir->files; file != NULL; file = file->next) {                if((file->carousel_id == car_id) &&		   (file->module_id == mod_id) &&                   CacheKeyCmp(file->key, key, file->key_len, key_len)) {                        return file;                }        }        /* Search sub dirs */        for(subdir = dir->sub; subdir != NULL; subdir=subdir->next) {                file = CacheScanFile(subdir, car_id, mod_id, key_len, key);                if(file != NULL) {                        return file;                }        }        return NULL;}struct cache_file *Cache::CacheFileFind(unsigned long car_id, unsigned short module_id, 		     unsigned int key_len, char *key){        struct cache_file *file;        /* Try looking in parent-less list */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -