static int directory_init(struct directory *d, const char *pathname) { struct stat statbuf; if (stat(pathname, &statbuf) < 0) { sf_log(SF_LOG_ERR, "failed to stat file %s", pathname); return SF_ERR; } strncpy(d->name, pathname, NAME_MAX); if (S_ISDIR(statbuf.st_mode)) { d->isopened = 0; d->iszip = 0; } else { int err; if ((d->zip = zip_open(pathname, 0, &err)) == NULL) { char buf[1024]; zip_error_to_str(buf, 1024, err, errno); sf_log(SF_LOG_ERR, "failed to open %s: %s\n", pathname, buf); return SF_ERR; } d->iszip = 1; d->isopened = 1; } d->nopens = 0; return SF_OK; }
void sf_save_chkpoint(char *conf_fn, int line) { FILE *fp; fp = fopen(SF_CHKPOINT_FN, "a+"); if (fp == NULL) { sf_log(LOGMOD_CHK, LOG_ERR, "Cannot open checkpoint file for saving\n"); return; } fprintf(fp, SF_CHKPOINT_PATTERN, conf_fn, line); fclose(fp); }
int fs_init(int argc, char **argv) { char buf[PATH_MAX]; int len; char *ptr; sf_list_def_t def; if (fs.isinited) { sf_log(SF_LOG_WARN, "fs_init: filesystem already initialized."); return SF_OK; } sf_memzero(&def, sizeof(def)); def.size = sizeof(struct directory); def.free = directory_close; sf_list_init(&fs.directories, &def); #ifdef __WIN32__ if (argv[0][1] == ':') { #else if (argv[0][0] == '/') { #endif strncpy(buf, argv[0], PATH_MAX); } else { len = PATH_MAX; getcwd(buf, len); len = strlen(buf); buf[len++] = seperator; buf[len] = '\0'; strncat(buf, argv[0], PATH_MAX - len); } ptr = strrchr(buf, seperator); assert(ptr != NULL); *ptr = '\0'; if (fs_cd(buf) != SF_OK) { sf_list_destroy(&fs.directories); return SF_ERR; } fs.isinited = 1; return SF_OK; } void fs_term(void) { sf_list_destroy(&fs.directories); }
static int fs_file_open_in_zip(struct fs_file *f, const char *filename) { struct directory *d; char buf[NAME_MAX]; d = sf_list_tail(&fs.directories); get_zip_cwd(buf, NAME_MAX); strcat(buf, filename); f->isinzip = 1; if ((f->zip_file = zip_fopen(d->zip, buf, 0)) == NULL) { sf_log(SF_LOG_ERR, "failed to open %s", filename); return SF_ERR; } return SF_OK; }
static int directory_open(struct directory *d) { if (d->isopened) { return SF_OK; } if (d->iszip) { assert(0); } else { char buf[PATH_MAX]; get_directory_pathname(sf_list_head(&fs.directories), d, buf, PATH_MAX); if ((d->dir = opendir(buf)) == NULL) { sf_log(SF_LOG_ERR, "failed to open directory %s", d->name); return SF_ERR; } } d->isopened = 1; return SF_OK; }
static int fs_cd_file_zip(struct directory *parent, const char *filename) { int isdirexist = 0; char buf[NAME_MAX]; int len; zip_int64_t nentries, i; struct directory dir; if (strcmp(filename, "..") == 0) { sf_list_pop(&fs.directories); return SF_OK; } get_zip_cwd(buf, NAME_MAX); strcat(buf, filename); len = strlen(buf); buf[len++] = '/'; buf[len] = '\0'; nentries = zip_get_num_entries(parent->zip, 0); for (i = 0; i < nentries; ++i) { if (strncmp(zip_get_name(parent->zip, i, 0), buf, len) == 0) { isdirexist = 1; break; } } if (isdirexist) { strncpy(dir.name, filename, NAME_MAX); dir.iszip = 1; dir.isopened = 1; dir.zip = parent->zip; dir.nopens = parent->nopens + 1; sf_list_push(&fs.directories, &dir); return SF_OK; } else { sf_log(SF_LOG_ERR, "not a directory: %s", filename); return SF_ERR; } }
/* 对于检查点需要做的事情,就是对于每个输入的URL文件记录下已经处理的行数 */ void sf_load_chkpoint() { FILE *fp; char conf_fn[255]; int line; sf_hash_conf_t *h; sf_hash_conf_t *h_new; fp = fopen(SF_CHKPOINT_FN, "r"); if (NULL == fp) { /* 如果没有文件或是无法读取,则结束 */ sf_log(LOGMOD_CHK, LOG_ERR, "Cannot open checkpoint file for loading\n"); return; } while (EOF != fscanf(fp, SF_CHKPOINT_FN, conf_fn, &line)) { h = (sf_hash_conf_t*)sf_confset_search(conf_fn); if (h) /* 如果找到了节点,则修改里面的值 ,没有则创建*/ { h->line = line; } else { pthread_mutex_lock(&global_pool_mutex); h_new = (sf_hash_conf_t*)AllocHashData(global_pool, sizeof(sf_hash_conf_t)); pthread_mutex_unlock(&global_pool_mutex); h_new->s = NULL; h_new->conf_fn = sf_strdup(conf_fn); h_new->line = line; sf_confset_insert(h_new); } } fclose(fp); }