OpenState KLineFile::open(const char *file, time_t &lastModified) { struct _stati64 sbuf; if (lstat(file, &sbuf) != 0 || !S_ISREG(sbuf.st_mode)) { return OPEN_FAILED; } if (sbuf.st_mtime == lastModified) { return OPEN_NOT_MODIFIED; } int len = (int)MIN(sbuf.st_size,MAX_OPEN_FILE_SIZE); if (buf) { free(buf); } buf = (char *) malloc(len + 1); KFile fp; if (fp.open(file,fileRead)) { len = fp.read(buf,len); if (len >= 0) { buf[len] = '\0'; hot = buf; lastModified = sbuf.st_mtime; return OPEN_SUCCESS; } } return OPEN_FAILED; }
int create_file_index(const char *file,void *param) { KStringBuf s; cor_result result = cor_failed; KHttpObject *obj; s << (char *)param << PATH_SPLIT_CHAR << file; char *file_name = s.getString(); KFile fp; if (!fp.open(s.getString(),fileRead)) { fprintf(stderr,"cann't open file[%s]\n",s.getString()); return 0; } if (recreate_start_time>0) { INT64 t = fp.getCreateTime(); if (t>recreate_start_time) { klog(KLOG_DEBUG,"file [%s] is new file t=%d\n",file_name,(int)(t-recreate_start_time)); return 0; } } /////////[139] KHttpObjectFileHeader header; if(fp.read((char *)&header,sizeof(KHttpObjectFileHeader))!=sizeof(KHttpObjectFileHeader)){ fprintf(stderr,"cann't read head size [%s]\n",file_name); goto failed; } obj = new KHttpObject; memcpy(&obj->index,&header.index,sizeof(obj->index)); /////////[140] result = create_http_object(&fp,obj,file_name); if (result==cor_success) { #ifdef ENABLE_DB_DISK_INDEX if (dci) { dci->start(ci_add,obj); } #endif load_count++; #if 0 if (rebuild_cache_hash) { char *file_name2 = obj->getFileName(); if (file_name2) { if (strcmp(file_name,file_name2)!=0) { if (rename(file_name,file_name2)!=0) { rebuild_cache_files.insert(std::pair<std::string,std::string>(file_name,file_name2)); } /////////[141] } free(file_name2); } } #endif } obj->release(); failed: fp.close(); if (result==cor_failed) { klog(KLOG_NOTICE,"create http object failed,remove file[%s]\n",file_name); unlink(file_name); } return 0; }
char *KXml::getContent(const std::string &file) { KFile fp; if (!fp.open(file.c_str(),fileRead)) { return NULL; } INT64 fileSize = fp.getFileSize(); if (fileSize > 1048576) { return NULL; } char *buf = (char *)malloc((int)fileSize + 1); if (buf==NULL) { return NULL; } if (fp.read(buf,(int)fileSize) != (int)fileSize) { free(buf); return NULL; } buf[fileSize] = '\0'; return buf; /* KStringBuf s; char buf[512]; for (;;) { long readSize = fp.read(buf, sizeof(buf)); if (readSize <= 0) { break; } s.write_all(buf, readSize); if (s.getSize()>1048576) { break; } } return s.stealString(); */ }
bool load_index_scan_state() { KStringBuf s; get_index_scan_state_filename(s); KFile fp; if (!fp.open(s.getString(),fileRead)) { return false; } bool result = true; if (sizeof(index_scan_state_t) != fp.read((char *)&index_scan_state,sizeof(index_scan_state_t))) { result = false; } fp.close(); return result; }
bool loadCacheIndex() { load_count = 0; bool result = false; char *file_name = getCacheIndexFile(); #if 0 rebuild_cache_hash = true; #endif KFile fp; if (!fp.open(file_name,fileReadWrite)) { xfree(file_name); #ifdef ENABLE_DB_DISK_INDEX if (dci) { return true; } #endif recreate_index(time(NULL)); return false; } HttpObjectIndexHeader indexHeader; if(fp.read((char *)&indexHeader,sizeof(HttpObjectIndexHeader))!=sizeof(HttpObjectIndexHeader)){ klog(KLOG_ERR,"cann't read cache index header\n"); recreate_index(time(NULL)); goto done; } #if 0 if (indexHeader.cache_dir_mask1==CACHE_DIR_MASK1 && indexHeader.cache_dir_mask2==CACHE_DIR_MASK2) { rebuild_cache_hash = false; } #endif if(indexHeader.state != INDEX_STATE_CLEAN #if 0 || rebuild_cache_hash #endif ){ klog(KLOG_ERR,"cache index file not clean.\n"); recreate_index(time(NULL)); goto done; } for(;;){ KHttpObject *obj = new KHttpObject; if (fp.read((char *)&obj->index,sizeof(HttpObjectIndex))!=sizeof(HttpObjectIndex)) { obj->release(); break; } if (create_http_object(&fp,obj)==cor_success) { load_count++; #ifdef ENABLE_DB_DISK_INDEX if (dci) { dci->start(ci_add,obj); } #endif } else { SET(obj->index.flags,FLAG_DEAD|FLAG_IN_DISK); } obj->release(); } result = true; if(load_count!=indexHeader.object_count){ klog(KLOG_ERR,"Warning not all obj have loaded,total obj count=%d,loaded=%d\n",indexHeader.object_count,load_count); debug("total object count=%d\n",cache.getCount()); } //设置index为notclean //fseeko(fp,0,SEEK_SET); fp.seek(0,seekBegin); indexHeader.state = INDEX_STATE_UNCLEAN; fp.write((char *)&indexHeader,sizeof(HttpObjectIndexHeader)); done: if(file_name){ xfree(file_name); } klog(KLOG_ERR,"total load disk obj count=%d\n",load_count); return result; }