void init_disk_cache(bool firstTime) { #ifdef ENABLE_SQLITE_DISK_INDEX if (dci==NULL) { memset(&index_scan_state,0,sizeof(index_scan_state)); load_index_scan_state(); dci = new KSqliteDiskCacheIndex; char *file_name = getCacheIndexFile(); KStringBuf sqliteIndex; sqliteIndex << file_name << ".sqt"; free(file_name); KFile fp; if (fp.open(sqliteIndex.getString(),fileRead)) { fp.close(); if (dci->open(sqliteIndex.getString())) { dci->start(ci_load,NULL); } else { klog(KLOG_ERR,"recreate the disk cache index database\n"); dci->close(); unlink(sqliteIndex.getString()); dci->create(sqliteIndex.getString()); rescan_disk_cache(); } } else { dci->create(sqliteIndex.getString()); m_thread.start(NULL,load_cache_index); } } #else memset(&index_scan_state,0,sizeof(index_scan_state)); load_index_scan_state(); m_thread.start(NULL,load_cache_index); #endif }
/* Copy data to dest file */ int KFile::copyFile(const char* destFile) { if (fileDescriptor == -1) { kprint(E, NOT_OPEN, "\n\tWhile reading %s", fileName); return -1; } KFile destF; if (strcmp(destFile, "") == 0) { destF.fileDescriptor = STDOUT_FILENO; } else { if (!destF.openFile(destFile, "c")) { return -1; } if(chmod(destFile, 775) == -1) { kprint(E, 0, "\n\tWhile changing permissions %s", destFile); return -1; } } ssize_t sent = 0; while ((sent = sendfile(destF.fileDescriptor, fileDescriptor, NULL, 64*1024*1024)) > 0); if (sent == -1) { kprint(E, 0, "\n\tWhile copying %s to %s", fileName, destFile); } 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(); */ }
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; }
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; }
KFile* KUiBase::OpenSaveSentenceFile(const char* szRoleName) { KFile* pFile = NULL; if (pFile) { pFile->Seek(0, FILE_END); } return pFile; }
void KFontProperties::enumerateDirectory(const KFile &sPath, int depth, int ptSize) { if(sPath.isDirectory()) { auto dContents = sPath.listFiles(); for(auto file : dContents) { if(file->isFile()) { KFontProperties::enumerateFont(file.get(), ptSize); } if(file->isDirectory() && depth > 0) { enumerateDirectory(file->getName(), depth - 1, ptSize); } } } }
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 KConfigBuilder::saveConfig() { KConfigBuilder builder; #ifdef KANGLE_ETC_DIR string configFile = KANGLE_ETC_DIR; #else string configFile = conf.path + "/etc"; #endif configFile += CONFIG_FILE; string tmpfile = configFile + ".tmp"; string lstfile = configFile + ".lst"; KFile fp; if(!fp.open(tmpfile.c_str(),fileWrite)){ fprintf(stderr, "cann't open configfile[%s] for write\n", tmpfile.c_str()); return false; } stringstream s; builder.build(s); s << "\r\n" << CONFIG_FILE_SIGN; bool result = false; if ((int)s.str().size() == fp.write(s.str().c_str(), s.str().size())) { result = true; } fp.close(); if (!result) { fprintf(stderr,"cann't write sign string\n"); return false; } unlink(lstfile.c_str()); rename(configFile.c_str(),lstfile.c_str()); rename(tmpfile.c_str(),configFile.c_str()); if (conf.mergeFiles.size()>0) { //remove the merge config files. std::list<std::string>::iterator it; for(it=conf.mergeFiles.begin();it!=conf.mergeFiles.end();it++){ unlink((*it).c_str()); } conf.mergeFiles.clear(); std::string errMsg; conf.gvm->saveConfig(errMsg); } //°Ñvh.xmlÖØÃüÃû string file = conf.path; file += VH_CONFIG_FILE; string oldfile = file + ".old"; rename(file.c_str(),oldfile.c_str()); return true; }
bool KProcess::saveFile(const char *dir,const char *unix_file) { std::stringstream s; s << dir << "kp_" << getpid() << "_" << getProcessId() << "_" << sig; kassert(file==NULL); file = strdup(s.str().c_str()); KFile fp; if (fp.open(file,fileWrite)) { if (unix_file) { fp.fprintf("%s",unix_file); } return true; } else { free(file); file = NULL; return false; } }
bool saveCacheIndex(bool fast) { //clean all DEAD obj cache.syncDisk(); #ifdef ENABLE_DB_DISK_INDEX if (dci) { dci->start(ci_close,NULL); while (!dci->allWorkedDone()) { my_msleep(100); } return false; } #endif HttpObjectIndexHeader indexHeader; memset(&indexHeader,0,sizeof(HttpObjectIndexHeader)); indexHeader.head_size = sizeof(HttpObjectIndexHeader); indexHeader.block_size = sizeof(HttpObjectIndex); indexHeader.cache_dir_mask1 = CACHE_DIR_MASK1; indexHeader.cache_dir_mask2 = CACHE_DIR_MASK2; char *file_name = getCacheIndexFile(); KFile fp; bool result = true; if(!fp.open(file_name,fileWrite)){ klog(KLOG_ERR,"cann't open cache index file for write [%s]\n",file_name); xfree(file_name); return false; } fp.write((char *)&indexHeader,sizeof(HttpObjectIndexHeader)); int save_count = cache.save_disk_index(&fp); if(result){ //set the index file state is clean indexHeader.state = INDEX_STATE_CLEAN; indexHeader.object_count = save_count; fp.seek(0,seekBegin); fp.write((char *)&indexHeader,sizeof(HttpObjectIndexHeader)); klog(KLOG_ERR,"total save %d object\n",save_count); } if(file_name){ xfree(file_name); } return result; }
bool KTaskDataFile::SaveData() { KFile File; char szFileName[128]; GetFileName(szFileName, sizeof(szFileName)); bool bOk = false; while (ms_pPersonalRecord || ms_nSystemRecordCount > 0) { if (!File.Create(szFileName)) break; //==写文件头== TASK_FILE_HEADER Header = { 0 }; *(int*)(&(Header.cFlag[0])) = TASK_FILE_FLAG; if (ms_pPersonalRecord && ms_pPersonalRecord->nLen > 0) { Header.nPersonalRecordBytes = sizeof(KPersonalRecord) + ms_pPersonalRecord->nLen - sizeof(ms_pPersonalRecord->cBuffer); } Header.nSystemRecordCount = ms_nSystemRecordCount; if (File.Write(&Header, sizeof(Header)) != sizeof(Header)) break; //==写个人纪录== if (ms_pPersonalRecord) { if (File.Write(ms_pPersonalRecord, Header.nPersonalRecordBytes) != Header.nPersonalRecordBytes) break; } //==写系统纪录== KTaskSystemRecordNode* pCurrent = ms_pSystemRecordList; int i; for (i = 0; i < ms_nSystemRecordCount; i++) { int nSize = sizeof(TASK_SYSTEM_RECORD) + pCurrent->Record.nContentLen - sizeof(pCurrent->Record.cBuffer); if (File.Write(&pCurrent->Record, nSize) != nSize) break; pCurrent = pCurrent->pNext; } if (i >= ms_nSystemRecordCount) { bOk = true; } break; }; File.Close(); if (bOk == false) { char szFullName[MAX_PATH]; g_GetFullPath(szFullName, szFileName); DeleteFile(szFullName); } return bOk; }
std::shared_ptr<KFontProperties> KFontProperties::create(const KFile &file, int ptSize) { std::string absFont = file.getAbsolutePath(); auto lock = getLock(); if(fontList.find(absFont) != fontList.end()) { if(fontList[absFont].find(ptSize) != fontList[absFont].end()) { return fontList[absFont][ptSize]; } } lock.unlock(); if(!KFontProperties::enumerateFont(&file, ptSize)) { return std::shared_ptr<KFontProperties>(); } lock.lock(); return fontList[absFont][ptSize]; }
int KHttp::getHttpFile(const KData& kServer, const KData& kHttpFile, const KData& kSaveFile, int nStartPos) { LOGD("Entry getHttpFile"); m_dtHttpServer = kServer; m_dtHttpFile = kHttpFile; m_bChunked = false; m_iWriteLen = nStartPos; m_iLength = 0; m_clientSock.close(); m_kCnnect.close(); _respHeadMap.clear(); m_iNotifyPos = 0; m_iNotifyGap = 0; m_iContentLength = 0; m_iStatusCode = 0; bool bGet = (_paramMap.size() == 0); KFile kFile; KData kHttpRequest; KData kDataPost; if (bGet) { kHttpRequest = "GET "; } else { kHttpRequest = "POST "; map<KData, KData>::iterator iter; for (iter = _paramMap.begin(); iter != _paramMap.end(); iter++) { if (iter != _paramMap.begin()) kDataPost += "&"; kDataPost += iter->first; kDataPost += "="; kDataPost += iter->second; } } kHttpRequest += kHttpFile; kHttpRequest += " HTTP/1.1"; kHttpRequest += CRLF; kHttpRequest += "Host: "; kHttpRequest += kServer; kHttpRequest += CRLF; kHttpRequest += "Accept: */*"; kHttpRequest += CRLF; if (!m_dtUserAgent.isEmpty()) { kHttpRequest += "User-Agent: "; kHttpRequest += m_dtUserAgent; kHttpRequest += CRLF; } if (nStartPos > 0) { kHttpRequest += "RANGE: bytes="; kHttpRequest += nStartPos; kHttpRequest += "-"; kHttpRequest += CRLF; } kHttpRequest += "Pragma: no-cache"; kHttpRequest += CRLF; kHttpRequest += "Cache-Control: no-cache"; kHttpRequest += CRLF; kHttpRequest += "Connection: close"; kHttpRequest += CRLF; if (!bGet) { kHttpRequest += "Content-Type: application/x-www-form-urlencoded"; kHttpRequest += CRLF; kHttpRequest += "Content-Length: "; kHttpRequest += KData((int) kDataPost.length()); kHttpRequest += CRLF; } kHttpRequest += CRLF; char szBuffer[MTU] = {0}; if (m_dtHttpProxy.isEmpty()) { LOGD("Connect to Server: %s", kServer.getData() ); m_clientSock.setServer(kServer, 80); } else { LOGD("Connect to Server: %s", m_dtHttpProxy.getData() ); m_clientSock.setServer(m_dtHttpProxy, 80); } m_clientSock.initSocket(); if (!m_clientSock.connect()) { LOGERROR("m_clientSock.connect() failed"); return -1; } m_kCnnect = m_clientSock.getConn(); m_kCnnect.setTimeout(m_timeout); if (m_kCnnect.writeData(kHttpRequest) != (int) kHttpRequest.length()) { LOGERROR(" m_conn.writeData(httpRequest) != (int)httpRequest.length() failed"); return -1; } m_iWritedBytes += kHttpRequest.length(); if (!bGet) { if (m_kCnnect.writeData(kDataPost) != (int) kDataPost.length()) { LOGERROR("m_conn.writeData(dtPost) != (int)dtPost.length() failed"); return -1; } m_iWritedBytes += kDataPost.length(); } int iRead = 0; m_iStatusCode = 0; bool bRun = true; memset(szBuffer, 0, MTU); if ((iRead = m_kCnnect.readLine(szBuffer, MTU)) <= 0) { LOGERROR( "Read command line err: %d", iRead ); m_iReadedBytes += iRead; return 0; } KData dtKData; KData dtLine(szBuffer, iRead); if (dtLine.match(SP, &dtKData, true) == NOT_FOUND) { LOGERROR("Read command line mactch space err" ); return 0; } if (dtKData != "HTTP/1.1" && dtKData != "HTTP/1.0") { LOGERROR( "GET HTTP HEAD ERR" ); return 0; } if (dtLine.match(SP, &dtKData, true) == NOT_FOUND) { LOGERROR("Read command line mactch space 2 err" ); return 0; } m_iStatusCode = (int)dtKData; LOGD("Ready to while ( (iRead = m_conn.readLine(buff,MTU)) > 0 )"); while ((iRead = m_kCnnect.readLine(szBuffer, MTU)) > 0) { m_iReadedBytes += iRead; KData dtLine(szBuffer, iRead); KData dtBefVal; if (FOUND == dtLine.match(":", &dtBefVal, true)) { dtBefVal.removeSpaces(); dtLine.removeSpaces(); if (isEqualNoCase(dtBefVal, "Content-Length")) { m_iLength = (int) dtLine + nStartPos; if (m_iNotifyPercent > 0 && m_iNotifyPercent <= 100) { m_iNotifyPos = m_iNotifyGap = m_iLength / (100 / m_iNotifyPercent); } } else if (isEqualNoCase(dtBefVal, "Transfer-Encoding")) { if (isEqualNoCase(dtLine, "chunked")) { m_bChunked = true; } } _respHeadMap[dtBefVal] = dtLine; } } if (iRead < 0) { LOGERROR("read err" ); return -1; } if (m_iStatusCode != 200 && m_iStatusCode != 206) { if (m_iStatusCode == 302) { KData dtRedirectUrl = getRespFieldValue("Location"); LOGD("Ready to return getHttpFile( dtRedirectUrl, savefile, startpos );"); return getHttpFile(dtRedirectUrl, kSaveFile, nStartPos); } LOGERROR(" m_iStatusCode!=200 && m_iStatusCode!=206"); return 0; } FILE* pkFile = 0; string strFile = kSaveFile.getData(); if (nStartPos <= 0) { pkFile = fopen(strFile.c_str(),"wb+"); } else { pkFile = fopen(strFile.c_str(), "wb+"); fclose(pkFile); pkFile = fopen(strFile.c_str(),"r+b"); if (0 == pkFile) { return -1; } fseek(pkFile, nStartPos, SEEK_SET); } if (m_bChunked) { LOGD("m_bChunked is true!"); unsigned char* pBuff = new unsigned char[MTU]; int iBuffLen = MTU; while ((iRead = m_kCnnect.readLine(szBuffer, MTU)) > 0) { m_iReadedBytes += iRead; if (iRead > 8) { return -1; } int nLength = KData(szBuffer, iRead).HexToInt(); if (nLength <= 0) { delete [] pBuff; return m_iWriteLen; } if (nLength > iBuffLen) { delete[] pBuff; iBuffLen = nLength; pBuff = new unsigned char[iBuffLen]; } int iReaded = 0; memset(pBuff, 0, nLength); m_kCnnect.readData(pBuff, nLength, iReaded); m_iReadedBytes += iReaded; kFile.write(pBuff, iReaded); m_iWriteLen += iReaded; if (m_iLength > 0) { if (m_iWriteLen >= m_iLength) { if (m_iNotifyGap > 0) { m_pNotifyCallback(m_pNotifyParam, 100, m_iWriteLen, m_iLength); } break; } if (m_iNotifyGap > 0 && m_iWriteLen > m_iNotifyPos) { int nPercent = int((m_iWriteLen / (float) m_iLength) * 100); m_iNotifyPos += m_iNotifyGap; m_pNotifyCallback(m_pNotifyParam, nPercent, m_iWriteLen, m_iLength); } } if (iReaded != nLength) { delete[] pBuff; return m_iWriteLen; } if (m_kCnnect.readLine(szBuffer, MTU) != 0) { return m_iWriteLen; } } delete[] pBuff; return m_iWriteLen; } else { string strFile = kSaveFile.getData(); while ((iRead = m_kCnnect.readn(szBuffer, MTU)) > 0 && bRun) { m_iReadedBytes += iRead; unsigned int uiWriteSize = fwrite((unsigned char*) szBuffer,1,iRead,pkFile); //kFile.write((unsigned char*) szBuffer, iRead); m_iWriteLen += iRead; if (m_iLength > 0) { if (m_iWriteLen >= m_iLength) { if (m_iNotifyGap > 0) { m_pNotifyCallback(m_pNotifyParam, 100, m_iWriteLen, m_iLength); } break; } if (m_iNotifyGap > 0 && m_iWriteLen > m_iNotifyPos) { int nPercent = int((m_iWriteLen / (float) m_iLength) * 100); m_iNotifyPos += m_iNotifyGap; m_pNotifyCallback(m_pNotifyParam, nPercent, m_iWriteLen, m_iLength); } } while (m_bPause) { vsleep(1000); } } //fclose(pkFile); } fclose(pkFile); m_kCnnect.close(); kFile.closeFile(); LOGD("return m_iWriteLen;"); return m_iWriteLen; }
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; }
bool KFile::operator==(const KFile& other) { if (getFullName() == other.getFullName()) return true; return false; }
void KTaskDataFile::LoadData() { ClearAll(); KFile File; char szFileName[128]; GetFileName(szFileName, sizeof(szFileName)); bool bOk = false; while(File.Open(szFileName)) { //==读文件头== TASK_FILE_HEADER Header; if (File.Read(&Header, sizeof(Header)) != sizeof(Header)) break; if (*(int*)(&(Header.cFlag[0])) != TASK_FILE_FLAG) break; //==读个人纪录== if (Header.nPersonalRecordBytes) { ms_pPersonalRecord = (KPersonalRecord*)malloc(Header.nPersonalRecordBytes); if (ms_pPersonalRecord) { if (!File.Read(ms_pPersonalRecord, Header.nPersonalRecordBytes)) break; } else { break; } } //==读系统纪录== TASK_SYSTEM_RECORD record; int nPreSize = sizeof(TASK_SYSTEM_RECORD) - sizeof(record.cBuffer); for (int i = 0; i < Header.nSystemRecordCount; i++) { if (File.Read(&record, nPreSize) != nPreSize) break; KTaskSystemRecordNode* pNode = (KTaskSystemRecordNode*)malloc( sizeof(KTaskSystemRecordNode) + record.nContentLen); if (pNode) { if (File.Read(pNode->Record.cBuffer, record.nContentLen) != record.nContentLen) { free (pNode); break; } pNode->pNext = NULL; pNode->Record.nContentLen = record.nContentLen; pNode->Record.tTime = record.tTime; pNode->Record.uReserved = record.uReserved; AppendSystemRecord(pNode); } } bOk = true; break; }; File.Close(); if (bOk == false) ClearAll(); }