void cache_delfiles() { // deleting db files DIR *dirp; struct dirent *dp; char path[PATH_MAX]; bmkdir_p(bfile(_temp_path), 0755); if ((dirp = opendir(bfile(_temp_path))) == NULL) { printf("Cant open: %s\n", bfile(_temp_path) ); exit(1); } while ((dp = readdir(dirp)) != NULL) { if (dp->d_name[0] == '.') continue; snprintf(path,sizeof(path),"%s/%s",bfile(_temp_path),dp->d_name); printf("Unlinking %s\n",path); unlink(path); } closedir(dirp); }
static void * cache_spelling_keepalive_thread(spelling_t **spelling) { for (;;) { // Hush little baby pthread_mutex_lock(&spelling_cache_lock); pthread_cond_wait(&spelling_cache_cv, &spelling_cache_lock); // Refresh cache bblog(INFO, "Refreshing spelling cache..."); // do it if (*spelling != NULL) { untrain(*spelling); } if ((*spelling = train(bfile("var/dictionarywords"))) == NULL) { bblog(WARN, "Can't init spelling."); } pthread_mutex_unlock(&spelling_cache_lock); bblog(INFO, "~Refreshing spelling cache"); } }
/// Tries to read program binaries from file cache. inline boost::optional<cl::Program> load_program_binaries( const std::string &hash, const cl::Context &context, const std::vector<cl::Device> &device, const std::string &options = "" ) { std::ifstream bfile(program_binaries_path(hash) + "kernel", std::ios::binary); if (!bfile) return boost::optional<cl::Program>(); size_t n; std::vector<char> buf; bfile.read((char*)&n, sizeof(size_t)); buf.resize(n); bfile.read(buf.data(), n); cl::Program program(context, device, cl::Program::Binaries( 1, std::make_pair(static_cast<const void*>(buf.data()), n))); try { program.build(device, options.c_str()); } catch(const cl::Error&) { std::cerr << "Loading binaries failed:" << std::endl << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device[0]) << std::endl; return boost::optional<cl::Program>(); } return boost::optional<cl::Program>(program); }
int userToSubname_open(struct userToSubnameDbFormat * userToSubnameDb, char mode) { unsigned int flags; int ret; int filemode; DB * db; debug("Using DB version %s\n", DB_VERSION_STRING); switch (mode) { case 'w': flags = DB_CREATE; /* If the database does not exist, * create it.*/ filemode = 0664; break; case 'r': flags = DB_RDONLY; filemode = 0; break; default: errx(1, "%s %d Unknown open mode '%d'", __FILE__, __LINE__, mode); } if ((ret = db_create(&db, NULL, 0)) != 0) errx(1, "%s db_create: %s\n", __FILE__, db_strerror(ret)); if ((ret = db->set_flags(db, DB_DUPSORT)) != 0) errx(1, "set dupsort flags, %s\n", db_strerror(ret)); ret = db->open(db, NULL, bfile(userToSubnameDbFile), NULL, DB_BTREE, flags, filemode); if (ret != 0) errx(1, "db open error %s\n", db_strerror(ret)); (*userToSubnameDb).dbp = db; return 1; }
FILE *lockcoll(char subname[]) { char lockfile[512]; FILE *LOCK; //oppretter var mappen hvis den ikke finnes. Dette slik at vi slipper og gjøre dette under instalsjonen bmkdir_p(bfile("var/"),0755); sprintf(lockfile,"var/boitho-collections-%s.lock",subname); printf("locking lock \"%s\"\n",lockfile); if ((LOCK = bfopen(lockfile,"w+")) == NULL) { perror(lockfile); return NULL; } //geting the lock. if (flock(fileno(LOCK),LOCK_EX) != 0) { fclose(LOCK); return NULL; } return LOCK; }
bool SolidLeaf::Save(bool) { if (IsDirty()) { File jf; if (!jf.OpenWrite(GetName())) { return false; } if (m_pLeafData) { bool ret = m_pLeafData->Save(&jf); } // Binary file std::string bfilename = GetName(); bfilename += "b"; File bfile(true); bfile.OpenWrite(bfilename, File::CURRENT_VERSION, true /*binary*/); if (m_pLeafData) { bool ret = m_pLeafData->Save(&bfile); } } return true; }
struct spelling * spelling_init(char *lang) { AspellConfig *config; AspellCanHaveError *ret; struct spelling *spelling = malloc(sizeof(struct spelling)); if (spelling == NULL) { warn("malloc(spelling)"); return NULL; } config = new_aspell_config(); aspell_config_replace(config, "lang", spelling_lookup_lang(lang)); aspell_config_replace(config, "dict-dir", bfile("data/dict/")); aspell_config_replace(config, "encoding", "iso-8859-1"); ret = new_aspell_speller(config); delete_aspell_config(config); spelling->speller = to_aspell_speller(ret); if (aspell_error(ret) != 0) { printf("Error: %s\n", aspell_speller_error_message(spelling->speller)); delete_aspell_can_have_error(ret); return NULL; } spelling->conv = iconv_open("iso-8859-1", "utf-8"); spelling->conv_out = iconv_open("utf-8", "iso-8859-1"); return spelling; }
static bool LoadBuildPic(const string& filename, CBitmap& bitmap) { CFileHandler bfile(filename); if (bfile.FileExists()) { bitmap.Load(filename); return true; } return false; }
void XApp::initPakFiles() { wstring binFile = xapp().findFile(L"texture01.pak", XApp::TEXTUREPAK, false); if (binFile.size() == 0) { Log("pak file texture01.pak not found!" << endl); return; } ifstream bfile(binFile, ios::in | ios::binary); #if defined(_DEBUG) Log("pak file opened: " << binFile << "\n"); #endif // basic assumptions about data types: assert(sizeof(long long) == 8); assert(sizeof(int) == 4); long long magic; bfile.read((char*)&magic, 8); magic = _byteswap_uint64(magic); if (magic != 0x5350313250414B30L) { // magic "SP12PAK0" not found Log("pak file invalid: " << binFile << endl); return; } long long numEntries; bfile.read((char*)&numEntries, 8); if (numEntries > 30000) { Log("pak file invalid: contained number of textures: " << numEntries << endl); return; } int num = (int)numEntries; for (int i = 0; i < num; i++) { PakEntry pe; long long ll; bfile.read((char*)&ll, 8); pe.offset = (long)ll; bfile.read((char*)&ll, 8); pe.len = (long)ll; int name_len; bfile.read((char*)&name_len, 4); char *tex_name = new char[108 + 1]; bfile.read((char*)tex_name, 108); tex_name[name_len] = '\0'; //Log("pak entry name: " << tex_name << "\n"); pe.name = std::string(tex_name); pe.pakname = binFile; pak_content[pe.name] = pe; delete[] tex_name; } // check: for (auto p : pak_content) { Log(" pak file entry: " << p.second.name.c_str() << endl); } }
void perl_embed_init(char **incl_path, int cache_perl_files) { static int is_initialized = 0; if (is_initialized) { fprintf(stderr, "perl_embed_init has already been initialized, ignoring call.\n"); return; } is_initialized = 1; int incl_n = _chararray_size(incl_path); perl_opt_cache = cache_perl_files; char sdlib_arg[512]; snprintf(sdlib_arg, sizeof sdlib_arg, "-Mblib=%s", bfile(SD_CRAWL_LIB_PATH)); //char *perl_argv[] = { "", blib, "-I", bfile("crawlers/Modules/"), bfile2("perl/persistent.pl"), NULL }; char *perl_argv[incl_n ? incl_n + 4 : 3]; int perl_argc = 0; perl_argv[perl_argc++] = ""; if (incl_n) { perl_argv[perl_argc++] = "-I"; int i; for (i = 0; i < incl_n; i++) perl_argv[perl_argc++] = incl_path[i]; } perl_argv[perl_argc++] = sdlib_arg; perl_argv[perl_argc++] = bfile(PERSISTENT_PATH); perl_argv[perl_argc] = NULL; extern char **environ; PERL_SYS_INIT3(&argc, &argv, &environ); my_perl = perl_alloc(); perl_construct(my_perl); //int j = 0; //while (perl_argv[j++] != NULL) //printf("perl argument %s\n", perl_argv[j]); perl_parse(my_perl, xs_init, perl_argc, perl_argv, NULL); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; }
void WebSession::updateSessions(){ QDir dir(SESSION_PATH); QList<QFileInfo> flist = dir.entryInfoList(); for(int i = 0; i < flist.size(); ++i){ if(flist[i].isFile()){ qint64 ftime = flist[i].lastModified().currentMSecsSinceEpoch(); qint64 ctime = QDateTime::currentDateTime().currentMSecsSinceEpoch(); if(ctime - LIFE_MSECS > ftime){ QFile bfile(flist[i].absoluteFilePath()); bfile.remove(); } } } }
void XApp::readFile(PakEntry * pakEntry, vector<byte>& buffer, FileCategory cat) { Log("read file from pak: " << pakEntry->name.c_str() << endl); ifstream bfile(pakEntry->pakname.c_str(), ios::in | ios::binary); if (!bfile) { Error(L"failed re-opening pak file: " + pakEntry->pakname); } else { // position to start of file in pak: bfile.seekg(pakEntry->offset); buffer.resize(pakEntry->len); bfile.read((char*)&(buffer[0]), pakEntry->len); bfile.close(); } }
int dp_priority_locl_start() { //open the lock file //if ((DP_LOCK =open(DP_LOCK_FILE,O_CREAT|O_RDWR,S_IRWXU)) == -1) { if ((DP_LOCK =open(bfile(DP_LOCK_FILE),O_CREAT|O_RDWR,S_IRWXU)) == -1) { perror(DP_LOCK_FILE); return 0; } flock(DP_LOCK,LOCK_SH); return 1; }
/// Saves program binaries for future reuse. inline void save_program_binaries( const std::string &hash, const cl::Program &program, const std::string &source ) { std::ofstream bfile(program_binaries_path(hash, true) + "kernel", std::ios::binary); if (!bfile) return; std::vector<size_t> sizes = program.getInfo<CL_PROGRAM_BINARY_SIZES>(); std::vector<char*> binaries = program.getInfo<CL_PROGRAM_BINARIES>(); assert(sizes.size() == 1); bfile.write((char*)&sizes[0], sizeof(size_t)); bfile.write(binaries[0], sizes[0]); delete[] binaries[0]; bfile << "\n" << source << "\n"; }
bool PComplex<C,BT>::makeOutputFiles(const string& filename) { std::ostringstream dimst; // string representation for dimension // first we make interval files for each dimension typename INT_STR::const_iterator it; INTVEC* curints; typename INTVEC::const_iterator iit; string fname; // file name for writing for(it = ints.begin(); it != ints.end(); ++it) { // create file name by appending dimension dimst.str(""); dimst<<it->first; fname = filename + "_" + dimst.str() + ".txt"; ofstream outf(fname.c_str()); if (!outf.good()) { cout<<"\nUnable to create output file "<<fname; return false; } curints = it->second; for (iit = curints->begin(); iit!=curints->end(); ++iit) { //outf<<"["<<iit->first<<","<<iit->second<<"]\n"; outf<<iit->first<<" "<<iit->second<<"\n"; } outf.close(); } // now make betti number file! fname = filename + "_betti.txt"; ofstream bfile(fname.c_str()); // if the file doesn't get made, exit gracefully... if (!bfile.good()) { cout<<"\nUnable to create output file "<<fname; return false; } // otherwise, write betti numbers to file showBetti(bfile); bfile.close(); return true; }
char * cache_path(char *path, size_t len, enum cache_type type, char *query, int start, char *country, int anonymous, char search_user[], struct subnamesFormat *collections, int num_colls) { char tmppath[PATH_MAX]; char modquery[PATH_MAX]; unsigned int hash; char *p; char *cache; switch (type) { case CACHE_PREQUERY: cache = "var/cache/prequery_v_" CACHE_STRUCT_VERSION; break; case CACHE_SEARCH: cache = "var/cache/search_v_" CACHE_STRUCT_VERSION; break; } /* XXX: Base 64 encode the query */ strcpy(modquery, query); for (p = modquery; *p; p++) { if (*p == '/') *p = '-'; else if (*p == ' ') *p = '#'; } hash = cache_hash(modquery, start, country); p = (char *)&hash; strncpy(path, bfile(cache), len); mkdir(path, 0755); snprintf(tmppath, sizeof(tmppath), "/%x%x", *p & 0xF, (*p >> 4) & 0xF); strncat(path, tmppath, len); mkdir(path, 0755); p++; snprintf(tmppath, sizeof(tmppath), "/%x%x", *p & 0xF, (*p >> 4) & 0xF); strncat(path, tmppath, len); mkdir(path, 0755); snprintf(tmppath, sizeof(tmppath), "/%s.%d.%s.%s.%i", modquery, start, country, anonymous ? "anonymous":search_user, cache_collhash(collections, num_colls)); strncat(path, tmppath, len); return path; }
/// Saves program binaries for future reuse. inline void save_program_binaries( const std::string &hash, const cl::Program &program ) { // Prevent writing to the same file by several threads at the same time. static boost::mutex mx; boost::lock_guard<boost::mutex> lock(mx); std::ofstream bfile(program_binaries_path(hash, true) + "kernel", std::ios::binary); if (!bfile) return; std::vector<size_t> sizes = program.getInfo<CL_PROGRAM_BINARY_SIZES>(); std::vector<char*> binaries = program.getInfo<CL_PROGRAM_BINARIES>(); assert(sizes.size() == 1); bfile.write((char*)&sizes[0], sizeof(size_t)); bfile.write(binaries[0], sizes[0]); delete[] binaries[0]; }
void XApp::readFile(wstring filename, vector<byte> &buffer, FileCategory cat) { //ofstream f("fx\\HERE"); //f.put('c'); //f.flush(); //f.close(); //if (filename.) filename = findFile(filename, cat); ifstream bfile(filename.c_str(), ios::in | ios::binary); if (!bfile) { Error(L"failed reading file: " + filename); } else { streampos start = bfile.tellg(); bfile.seekg(0, std::ios::end); streampos len = bfile.tellg() - start; bfile.seekg(start); buffer.resize((SIZE_T)len); bfile.read((char*)&(buffer[0]), len); bfile.close(); } }
FILE *openMaplist() { FILE *MAPLIST; char *cptr; //sjekker først om vi har en env variabel kalt "BOITHOMAPLIST". Hvis vi har det så bruker vi den filen //gjør det slik slik at vi kan ha lokal maplist, på hver bbs, man fortsat ha resten likt på alle, og på read onlu nfs. if ((cptr = getenv("BOITHOMAPLIST")) != NULL) { if ( (MAPLIST = fopen(cptr,"r")) == NULL) { perror(cptr); exit(1); } } //leser liten over mapper vi kan bruke. else if ( (MAPLIST = bfopen("config/maplist.conf","r")) == NULL) { perror(bfile("config/maplist.conf")); exit(1); } return MAPLIST; }
wstring XApp::findFile(wstring filename, FileCategory cat, bool errorIfNotFound) { // try without path: ifstream bfile(filename.c_str(), ios::in | ios::binary); if (!bfile) { // try with Debug or release path: switch (cat) { case FX: filename = FX_PATH + filename; break; case TEXTURE: case TEXTUREPAK: filename = TEXTURE_PATH + filename; break; case MESH: filename = MESH_PATH + filename; break; case SOUND: filename = SOUND_PATH + filename; break; } bfile.open(filename.c_str(), ios::in | ios::binary); if (!bfile && cat == TEXTURE) { wstring oldname = filename; // try loading default texture filename = TEXTURE_PATH + wstring(L"default.dds"); bfile.open(filename.c_str(), ios::in | ios::binary); if (bfile) Log("WARNING: texture " << oldname << " not found, replaced by default.dds texture" << endl); } if (!bfile && errorIfNotFound) { Error(L"failed reading file: " + filename); } } if (bfile) { bfile.close(); return filename; } return wstring(); }
/*** * @brief converts files with hex strings to binary * * This method converts the given file, which contains * command strings in hexadecimal and saves them in * binary form in a binary file. * */ void asc2bin(std::string const &filein,std::string const &fileout){ #ifdef TRACE T(std::cout, "Enters BinFile::asc2bin"); #endif // coverts configuration files to binary files std::ifstream mfile(filein.c_str()); std::ofstream bfile(fileout.c_str(), std::ios::out | std::ios::binary); assert(mfile.is_open()); assert(bfile.is_open()); std::string line; while(mfile.good()){ // remove trash in line std::getline(mfile,line); line.erase(0, line.find_first_not_of(" \t\r\n\v\f")); size_t index = line.find_first_of("#"); if (index != std::string::npos) line.erase(index, std::string::npos); line.erase(remove_if(line.begin(), line.end(), ::isspace), line.end()); line.append("\n"); // write binary file unsigned short int value = 0; char hex[3]; hex[2] = '\0'; for(unsigned int i=0;i<line.size();i+=2){ hex[0] = line[i]; hex[1] = line[i+1]; sscanf(hex,"%hx\n", &value); bfile.write((char*)&value,1); } } mfile.close(); bfile.close(); }
int cache_init(cache_t *c, void (*freevalue)(void *value)) { memset(c, '\0', sizeof(*c)); int ret; DB_ENV *dbenv; ret = db_env_create(&dbenv, 0); dbenv->err(dbenv,ret,"err db_env_create "); dbenv->set_errfile(dbenv, stderr); dbenv->err(dbenv,ret,"err set_errfile "); if ((ret = dbenv->set_shm_key(dbenv, 664)) != 0) { dbenv->err(dbenv,ret,"err set_shm_key "); return 0; } ret = dbenv->open(dbenv,bfile(_temp_path),DB_CREATE | DB_INIT_MPOOL | DB_INIT_CDB ,0); dbenv->err(dbenv,ret,"err db_env_open "); /* Create and initialize database object */ if ((ret = db_create(&c->c_data, dbenv, 0)) != 0) { fprintf(stderr, "%s: db_create: %s\n", "bbdocument", db_strerror(ret)); return 0; } #ifdef DEBUG dbenv->stat_print(dbenv, DB_STAT_ALL); #endif /* open the database. */ if ((ret = c->c_data->open(c->c_data, NULL, "libcachedb", NULL, DB_BTREE, DB_CREATE, 0)) != 0) { c->c_data->err(c->c_data, ret, "db open"); //goto err1; //dette skjer nor collection mappen ikke er opprettet enda, typisk forde vi ikke har lagret et dokument der enda #ifdef DEBUG printf("can't dbp->open(), but db_create() was sucessful!\n"); #endif return 0; } #ifdef DEBUG c->c_data->stat_print(c->c_data, DB_STAT_ALL); #endif pthread_mutex_init(&c->c_lock, NULL); c->c_freevalue = freevalue; return 1; }
int main(int argc, char *argv[]) { bblog_init("searchd"); bblog(CLEAN, "Initializing..."); int sockfd; int runCount; //int newsockfd; socklen_t clilen; struct sockaddr_in cli_addr, serv_addr; FILE *LOGFILE; //FILE *LOCK; struct searchd_configFORMAT searchd_config; struct config_t maincfg; searchd_config.searchport = 0; searchd_config.optLog = 0; searchd_config.optMax = 0; searchd_config.optSingle = 0; searchd_config.optrankfile = NULL; searchd_config.optPreOpen = 0; searchd_config.optFastStartup = 0; searchd_config.optCacheIndexes = 1; searchd_config.optAlarm = 60; // Needed for the speller to properly convert utf8 to wchar_t setlocale(LC_ALL, "en_US.UTF-8"); /* Ignore collection updates for now */ signal(SIGUSR2, SIG_IGN); /* And ignore spelling updates */ signal(SIGUSR1, SIG_IGN); char c; while ((c=getopt(argc,argv,"clp:m:b:vsofA:L:S:a:"))!=-1) { switch (c) { case 'p': searchd_config.searchport = atoi(optarg); bblog(CLEAN, "searchd: Option -p: Using port %i.",searchd_config.searchport); break; case 'l': searchd_config.optLog = 1; break; case 'o': searchd_config.optPreOpen = 1; break; case 'm': searchd_config.optMax = atoi(optarg); break; case 'b': searchd_config.optrankfile = optarg; break; case 'v': /* XXX: Remove now that we have severity in the logger? */ bblog(CLEAN, "searchd: Option -v: Verbose output."); globalOptVerbose = 1; break; case 's': bblog(INFO, "Option -s: Won't fork for new connections"); searchd_config.optSingle = 1; break; case 'f': searchd_config.optFastStartup = 1; break; case 'c': searchd_config.optCacheIndexes = 0; break; case 'A': bblog_set_appenders(atoi(optarg)); break; case 'L': bblog_set_severity(atoi(optarg)); break; case 'S': spelling_min_freq = strtol(optarg, NULL, 10); break; case 'a': searchd_config.optAlarm = atoi(optarg); break; default: bblog(ERROR, "Unknown argument: %c", c); errx(1, "Unknown argument: %c", c); } } #ifdef BLACK_BOKS bblog(CLEAN, "Blackbox mode (searchdbb)"); time_t starttime; time(&starttime); if (searchd_config.optLog) { /* Only add file logging if syslog is disabled */ if ((bblog_get_appenders() & LOGGER_APPENDER_SYSLOG) == 0) bblog_set_appenders(LOGGER_APPENDER_FILE|bblog_get_appenders()); } /* Write pidfile */ FILE *pidfile = fopen(bfile("var/searchd.pid"), "w"); if (pidfile != NULL) { fprintf(pidfile, "%d", getpid()); fclose(pidfile); } else { bblog(WARN, "Unable to write to pidfile"); } bblog(CLEAN, "searchd: Starting. Time is %s",ctime(&starttime)); #endif #ifdef DEBUG bblog(DEBUGINFO, "searchd: Debug: argc %i, optind %i",argc,optind); #endif if (searchd_config.optrankfile == NULL) { searchd_config.optrankfile = "Brank"; } #ifdef WITH_SPELLING if (searchd_config.optFastStartup != 1) { if ((spelling = train(bfile("var/dictionarywords"))) == NULL) { bblog(ERROR, "Can't init spelling."); } cache_spelling_keepalive(&spelling); signal(SIGUSR1, cache_spelling_hup); } #endif if (argc > optind) { strncpy(servername,argv[optind], sizeof(servername) -1); } else { bblog(ERROR, "No hostname supplied"); errx(1, "You have to supply a hostname"); } lotPreOpenStartl(&searchd_config.lotPreOpen.DocumentIndex,"DocumentIndex","www",searchd_config.optPreOpen); lotPreOpenStartl(&searchd_config.lotPreOpen.Summary,"summary","www",searchd_config.optPreOpen); #ifdef BLACK_BOKS if (searchd_config.optCacheIndexes == 1) { if (searchd_config.optFastStartup != 1) { bblog(INFO, "Reading indexes"); cache_indexes(0); bblog(INFO, "Cached indexes: %dMB, cached indexes: %d", indexcachescached[0]/(1024*1024), indexcachescached[1]); preopen(); cache_fresh_lot_collection(); cache_indexes_keepalive(); signal(SIGUSR2, cache_indexes_hup); } else { signal(SIGUSR2, SIG_IGN); } } else { signal(SIGUSR2, SIG_IGN); } #endif maincfg = maincfgopen(); if (searchd_config.searchport == 0) { searchd_config.searchport = maincfg_get_int(&maincfg,"BSDPORT"); } searchd_config.cmc_port = maincfg_get_int(&maincfg,"CMDPORT"); maincfgclose(&maincfg); /***********************************************************************************/ //prøver å få fil lock. Bare en deamon kan kjøre avgangen /* #ifndef ALLOW_MULTIPLE_SEARCHD if ((LOCK = fopen("/tmp/searchd.loc","w")) == NULL) { perror("lock file"); exit(1); } if (flock(fileno(LOCK),LOCK_EX | LOCK_NB) != 0) { if (errno == EWOULDBLOCK) { printf("En annen prosses kjører allerede. Steng denne først.\n"); } else { perror("cant get lock file"); } exit(1); } #endif */ /***********************************************************************************/ //#ifndef BLACK_BOKS /* Initialize the configuration */ config_init(&cfg); /* Load the file */ #ifdef DEBUG bblog(DEBUGINFO, "searchd: Debug: Loading [%s] ...",bfile(cfg_searchd)); #endif if (!config_read_file(&cfg, bfile(cfg_searchd))) { bblog(ERROR, "config read failed: [%s]: %s at line %i",bfile(cfg_searchd),config_error_text(&cfg),config_error_line(&cfg)); exit(1); } //#endif html_parser_init(); /* #ifdef WITH_THREAD pthread_t chld_thr; printf("starting whth thread\n"); #else printf("starting single thread version\n"); #endif */ bblog(CLEAN, "Servername: %s", servername); //ToDo: må ha låsing her if ((LOGFILE = bfopen("config/query.log","a")) == NULL) { bblog_errno(ERROR, "%s", bfile("config/query.log")); } else { fprintf(LOGFILE,"starting server %s\n",servername); fclose(LOGFILE); } #ifdef BLACK_BOKS // Initialiser thesaurus med ouput-filene fra 'build_thesaurus_*': searchd_config.thesaurus_all = NULL; #ifndef WITHOUT_THESAURUS bblog(INFO, "init thesaurus"); searchd_config.thesaurus_all = NULL; if (searchd_config.optFastStartup != 1) { searchd_config.thesaurus_all = load_all_thesauruses(bfile("data/thesaurus/")); if (searchd_config.thesaurus_all == NULL) { bblog(ERROR, "Unable to open thesaurus. Disabling stemming"); } else { bblog(INFO, "init thesaurus done"); } } #endif bblog(INFO, "init file-extensions"); searchd_config.getfiletypep = fte_init(bfile("config/file_extensions.conf")); if (searchd_config.getfiletypep == NULL) { bblog(ERROR, "Unable to open file-extensions configuration file. Disabling file-extensions."); } bblog(INFO, "init attribute descriptions"); searchd_config.attrdescrp = adf_init(bfile("config/attribute_descriptions.conf")); if (searchd_config.attrdescrp == NULL) { bblog(ERROR, "Unable to open attribute descriptions configuration file. Disabling attribute descriptions."); } bblog(INFO, "init show-attributes"); char *warnings; /*searchd_config.showattrp = show_attributes_init(bfile("config/show_attributes.conf"), &warnings); if (searchd_config.showattrp == NULL) { fprintf(stderr, "searchd: ERROR!! Unable to open show-attributes configuration file. Disabling attributes.\n"); } else if (warnings[0]!='\0') { fprintf(stderr, "searchd: ******************* Warnings reading show-attributes config: ********************\n"); fprintf(stderr, "%s", warnings); fprintf(stderr, "searchd: *********************************************************************************\n"); }*/ #else //starter opp bblog(INFO, "Loading domain-ids..."); iintegerLoadMemArray2(&global_DomainIDs,"domainid",sizeof(unsigned short), "www"); //laster inn alle poprankene bblog(INFO, "Loading pop MemArray..."); popopenMemArray2("www",searchd_config.optrankfile); // ToDo: hardkoder subname her, da vi ikke vet siden vi ikke her får et inn enda bblog(INFO, "Loading adultWeight MemArray..."); adultWeightopenMemArray2("www"); // ToDo: hardkoder subname her, da vi ikke vet siden vi ikke her får et inn enda #endif IIndexInaliser(); #ifdef WITH_MEMINDEX IIndexLoad(); #endif if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { bblog(ERROR, "Server error! Can't open stream socket."); exit(1); } memset((char *) &serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(searchd_config.searchport); bblog(INFO, "Will bind to port %i",searchd_config.searchport); //seter at sokket kan rebrukes int yes=1; if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) { bblog_errno(ERROR, "setsockopt()"); exit(1); } if(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { bblog_errno(ERROR, "Can't bind local address. Port %i", searchd_config.searchport); exit(1); } /* set the level of thread concurrency we desire */ //thr_setconcurrency(5); listen(sockfd, 5); runCount = 0; #ifndef NEW_CHILD_CATCHER signal(SIGCLD, SIG_IGN); /* now I don't have to wait() for forked children! */ #else { // runarb 28 juni 2009: // Hvis vi har verbose output så skal vi wait()e for våre barn, og vise prity print når de dør. // desverre har det vært mye kød, der hovedprosessen blir hengene i sigchild_handler() og ikke kan // fork'e flere barn. For å ungå dtte venter vi ikke på våre barn til vanlig. if (globalOptVerbose) { struct sigaction sa; int ret; sa.sa_sigaction = sigchild_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_SIGINFO; ret = sigaction(SIGCHLD, &sa, 0); if (ret) { bblog_errno(ERROR, "sigaction()"); exit(1); } } else { signal(SIGCLD, SIG_IGN); /* now I don't have to wait() for forked children! */ } } #endif bblog(CLEAN, "|------------------------------------------------------------------------------------------------|"); bblog(CLEAN, "|%-40s | %-11s | %-11s | %-11s | %-11s|","query", "TotaltTreff", "showabal", "filtered", "total_usecs"); bblog(CLEAN, "|------------------------------------------------------------------------------------------------|"); for((clilen = sizeof(cli_addr));;) { searchd_config.newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if(searchd_config.newsockfd < 0) { /* Just restart */ if (errno == EINTR) continue; bblog(WARN, "searchd: Server warning! Accept error"); } else { if (searchd_config.optSingle) { do_chld((void *) &searchd_config); } else { #ifdef DEBUG bblog(DEBUGINFO, "Debug mode; will not fork to new process."); do_chld((void *) &searchd_config); #else /* #ifdef WITH_THREAD //create a new thread to process the incomming request //thr_create(NULL, 0, do_chld, (void *) searchd_config, THR_DETACHED, &chld_thr); pthread_create(&chld_thr, NULL, do_chld, (void *) &searchd_config); //the server is now free to accept another socket request #else do_chld((void *) &searchd_config); #endif */ bblog(DEBUGINFO, "Forking new prosess."); if (fork() == 0) { // this is the child process close(sockfd); // child doesn't need the listener do_chld((void *) &searchd_config); close(searchd_config.newsockfd); bblog(DEBUGINFO, "Terminating child."); exit(0); } else { close(searchd_config.newsockfd); // perent doesn't need the new socket } #endif } } ++runCount; if ((searchd_config.optMax != 0) && (runCount >= searchd_config.optMax)) { //venter på siste trå. Ikke helt optimalt dette, da vi kan ha flere tråer som kjører i paralell /* #ifdef WITH_THREAD pthread_join(chld_thr, NULL); #endif */ bblog(WARN, "have reached Max runs. Exiting..."); break; } } html_parser_exit(); free(searchd_config.lotPreOpen.Summary); free(searchd_config.lotPreOpen.DocumentIndex); adf_destroy(searchd_config.attrdescrp); fte_destroy(searchd_config.getfiletypep); if (searchd_config.optFastStartup != 1) { thesaurus_destroy(searchd_config.thesaurusp); } //freegjør spelling. Trekt, men kjekt av valgring kan finne ut om noe ikke her blirr frigjort. if (searchd_config.optFastStartup != 1) { untrain(&spelling); } return(0); }
ILboolean iLoadSunInternal(void) { SUNHEAD Header; BITFILE *File; ILuint i, j, Padding, Offset, BytesRead; ILubyte PaddingData[16]; if (iCurImage == NULL) { ilSetError(IL_ILLEGAL_OPERATION); return IL_FALSE; } //@TODO: Right now, iGetSunHead cannot fail. if (!iGetSunHead(&Header) || !iCheckSun(&Header)) { ilSetError(IL_INVALID_FILE_HEADER); return IL_FALSE; } switch (Header.Depth) { case 1: //@TODO: Find a file to test this on. File = bfile(iGetFile()); if (File == NULL) return IL_FALSE; if (!ilTexImage(Header.Width, Header.Height, 1, 1, IL_COLOUR_INDEX, IL_UNSIGNED_BYTE, NULL)) return IL_FALSE; if (Header.ColorMapLength != 0) { // Data should be an index into the color map, but the color map should only be RGB (6 bytes, 2 entries). if (Header.ColorMapLength != 6) { ilSetError(IL_INVALID_FILE_HEADER); return IL_FALSE; } } iCurImage->Pal.Palette = (ILubyte*)ialloc(6); // Just need 2 entries in the color map. if (Header.ColorMapLength == 0) { // Create the color map iCurImage->Pal.Palette[0] = 0x00; // Entry for black iCurImage->Pal.Palette[1] = 0x00; iCurImage->Pal.Palette[2] = 0x00; iCurImage->Pal.Palette[3] = 0xFF; // Entry for white iCurImage->Pal.Palette[4] = 0xFF; iCurImage->Pal.Palette[5] = 0xFF; } else { iread(iCurImage->Pal.Palette, 1, 6); // Read in the color map. } iCurImage->Pal.PalSize = 6; iCurImage->Pal.PalType = IL_PAL_RGB24; Padding = (16 - (iCurImage->Width % 16)) % 16; // Has to be aligned on a 16-bit boundary. The rest is padding. // Reads the bits for (i = 0; i < iCurImage->Height; i++) { bread(&iCurImage->Data[iCurImage->Width * i], 1, iCurImage->Width, File); //bseek(File, BitPadding, IL_SEEK_CUR); //@TODO: This function does not work correctly. bread(PaddingData, 1, Padding, File); // Skip padding bits. } break; case 8: if (Header.ColorMapType == IL_SUN_NO_MAP) { // Greyscale image if (!ilTexImage(Header.Width, Header.Height, 1, 1, IL_LUMINANCE, IL_UNSIGNED_BYTE, NULL)) return IL_FALSE; } else { // Colour-mapped image if (!ilTexImage(Header.Width, Header.Height, 1, 1, IL_COLOUR_INDEX, IL_UNSIGNED_BYTE, NULL)) return IL_FALSE; iCurImage->Pal.Palette = (ILubyte*)ialloc(Header.ColorMapLength); // Allocate color map. if (iCurImage->Pal.Palette == NULL) return IL_FALSE; if (iread(iCurImage->Pal.Palette, 1, Header.ColorMapLength) != Header.ColorMapLength) { // Read color map. ilSetError(IL_FILE_READ_ERROR); return IL_FALSE; } iCurImage->Pal.PalSize = Header.ColorMapLength; iCurImage->Pal.PalType = IL_PAL_RGB24; } if (Header.Type != IL_SUN_BYTE_ENC) { // Regular uncompressed image data Padding = (2 - (iCurImage->Bps % 2)) % 2; // Must be padded on a 16-bit boundary (2 bytes) for (i = 0; i < Header.Height; i++) { iread(iCurImage->Data + i * Header.Width, 1, iCurImage->Bps); if (Padding) // Only possible for padding to be 0 or 1. igetc(); } } else { // RLE image data for (i = 0; i < iCurImage->Height; i++) { BytesRead = iSunGetRle(iCurImage->Data + iCurImage->Bps * i, iCurImage->Bps); if (BytesRead % 2) // Each scanline must be aligned on a 2-byte boundary. igetc(); // Skip padding } } break; case 24: if (Header.ColorMapLength > 0) // Ignore any possible colormaps. iseek(Header.ColorMapLength, IL_SEEK_CUR); if (Header.Type == IL_SUN_RGB) { if (!ilTexImage(Header.Width, Header.Height, 1, 3, IL_RGB, IL_UNSIGNED_BYTE, NULL)) return IL_FALSE; } else { if (!ilTexImage(Header.Width, Header.Height, 1, 3, IL_BGR, IL_UNSIGNED_BYTE, NULL)) return IL_FALSE; } if (Header.Type != IL_SUN_BYTE_ENC) { // Regular uncompressed image data Padding = (2 - (iCurImage->Bps % 2)) % 2; // Must be padded on a 16-bit boundary (2 bytes) for (i = 0; i < Header.Height; i++) { iread(iCurImage->Data + i * Header.Width * 3, 1, iCurImage->Bps); if (Padding) // Only possible for padding to be 0 or 1. igetc(); } } else { // RLE image data for (i = 0; i < iCurImage->Height; i++) { BytesRead = iSunGetRle(iCurImage->Data + iCurImage->Bps * i, iCurImage->Bps); if (BytesRead % 2) // Each scanline must be aligned on a 2-byte boundary. igetc(); // Skip padding } } break; case 32: if (Header.ColorMapLength > 0) // Ignore any possible colormaps. iseek(Header.ColorMapLength, IL_SEEK_CUR); if (Header.Type == IL_SUN_RGB) { if (!ilTexImage(Header.Width, Header.Height, 1, 3, IL_RGB, IL_UNSIGNED_BYTE, NULL)) return IL_FALSE; } else { if (!ilTexImage(Header.Width, Header.Height, 1, 3, IL_BGR, IL_UNSIGNED_BYTE, NULL)) return IL_FALSE; } // There is no padding at the end of each scanline. Offset = 0; for (i = 0; i < Header.Height; i++) { for (j = 0; j < Header.Width; j++) { igetc(); // There is a pad byte before each pixel. iCurImage->Data[Offset] = igetc(); iCurImage->Data[Offset+1] = igetc(); iCurImage->Data[Offset+2] = igetc(); } } break; default: // Should have already been checked with iGetSunHead. return IL_FALSE; } iCurImage->Origin = IL_ORIGIN_UPPER_LEFT; return ilFixImage(); }
void die_va(int errorcode,char query[], const char *fmt, va_list ap) { char queryesc[MaxQueryLen*2+1]; escapeHTML(queryesc, sizeof queryesc, query); printf("<search>\n"); printf("<error>\n"); printf(" <errorcode>%i</errorcode>\n",errorcode); printf(" <errormessage>"); vprintf(fmt,ap); printf("</errormessage>\n"); printf("</error>\n"); printf("<RESULT_INFO TOTAL=\"0\" QUERY=\"%s\" HILITE=\"\" TIME=\"0\" FILTERED=\"0\" SHOWABAL=\"0\" BOITHOHOME=\"%s\"/>\n",queryesc,bfile("")); printf("</search>\n"); vfprintf(stderr,fmt,ap); fprintf(stderr,"\n"); va_end(ap); exit(1); }
void connectHandler(int socket) { struct packedHedderFormat packedHedder; int isAuthenticated = 0; char tkeyForTest[32]; int i,n; int intrespons; int count = 0; container *attrkeys = NULL; #ifdef DEBUG_TIME struct timeval start_time, end_time; struct timeval tot_start_time, tot_end_time; gettimeofday(&tot_start_time, NULL); #endif ionice_benice(); while ((i=recv(socket, &packedHedder, sizeof(struct packedHedderFormat),MSG_WAITALL)) > 0) { #ifdef DEBUG printf("size is: %i\nversion: %i\ncommand: %i\n",packedHedder.size,packedHedder.version,packedHedder.command); #endif packedHedder.size = packedHedder.size - sizeof(packedHedder); if (attrkeys == NULL) { attrkeys = ropen(); } if (packedHedder.command == bbc_askToAuthenticate) { if ((i=recv(socket, tkeyForTest, sizeof(tkeyForTest),MSG_WAITALL)) == -1) { perror("Cant read tkeyForTest"); exit(1); } if (1) { printf("authenticated\n"); intrespons = bbc_authenticate_ok; bbdocument_init(NULL); isAuthenticated = 1; } else { printf("authenticate faild\n"); intrespons = bbc_authenticate_feiled; } if ((n=sendall(socket, &intrespons, sizeof(intrespons))) == -1) { perror("Cant recv filerest"); exit(1); } } else { if (!isAuthenticated) { printf("user not autentikated\n"); exit(1); } if (packedHedder.command == bbc_docadd) { #ifdef DEBUG printf("bbc_docadd\n"); #endif char *subname,*documenturi,*documenttype,*document,*acl_allow,*acl_denied,*title,*doctype; char *attributes; int dokument_size; unsigned int lastmodified; #ifdef DEBUG_TIME gettimeofday(&start_time, NULL); #endif //subname if ((i=recvall(socket, &intrespons, sizeof(intrespons))) == 0) { perror("Cant read intrespons"); exit(1); } subname = malloc(intrespons +1); if ((i=recvall(socket, subname, intrespons)) == 0) { perror("Cant read subname"); exit(1); } //documenturi if ((i=recvall(socket, &intrespons, sizeof(intrespons))) == 0) { perror("Cant read intrespons"); exit(1); } documenturi = malloc(intrespons +1); if ((i=recvall(socket, documenturi, intrespons)) == 0) { perror("Cant read documenturi"); exit(1); } //documenttype if ((i=recvall(socket, &intrespons, sizeof(intrespons))) == 0) { perror("Cant read intrespons"); exit(1); } documenttype = malloc(intrespons +1); if ((i=recvall(socket, documenttype, intrespons)) == 0) { perror("Cant read documenttype"); exit(1); } //document //dokument_size if ((i=recvall(socket, &dokument_size, sizeof(dokument_size))) == 0) { perror("Cant read dokument_size"); exit(1); } document = malloc(dokument_size +1); if (dokument_size == 0) { document[0] = '\0'; } else { if ((i=recvall(socket, document, dokument_size)) == 0) { fprintf(stderr,"Can't read document of size %i\n",dokument_size); perror("recvall"); exit(1); } } //lastmodified if ((i=recvall(socket, &lastmodified, sizeof(lastmodified))) == 0) { perror("Cant read lastmodified"); exit(1); } //acl_allow if ((i=recvall(socket, &intrespons, sizeof(intrespons))) == 0) { perror("Cant read intrespons"); exit(1); } acl_allow = malloc(intrespons +1); if ((i=recvall(socket, acl_allow, intrespons)) == 0) { perror("Cant read acl_allow"); exit(1); } //acl_denied if ((i=recvall(socket, &intrespons, sizeof(intrespons))) == 0) { perror("Cant read intrespons"); exit(1); } acl_denied = malloc(intrespons +1); if ((i=recvall(socket, acl_denied, intrespons)) == 0) { perror("Cant read acl_denied"); exit(1); } //title if ((i=recvall(socket, &intrespons, sizeof(intrespons))) == 0) { perror("Cant read intrespons"); exit(1); } title = malloc(intrespons +1); if ((i=recvall(socket, title, intrespons)) == 0) { perror("Cant read title"); exit(1); } //doctype if ((i=recvall(socket, &intrespons, sizeof(intrespons))) == 0) { perror("Cant read intrespons"); exit(1); } doctype = malloc(intrespons +1); if ((i=recvall(socket, doctype, intrespons)) == 0) { perror("Cant read doctype"); exit(1); } // Attribute list if ((i = recvall(socket, &intrespons, sizeof(intrespons))) == 0) err(1, "Can't receive attribute list len"); attributes = malloc(intrespons +1); if ((i=recvall(socket, attributes, intrespons)) == 0) err(1, "Can't receive attribute list"); #ifdef DEBUG_TIME gettimeofday(&end_time, NULL); printf("Time debug: bbdn_docadd recv data time: %f\n",getTimeDifference(&start_time, &end_time)); #endif printf("\n"); printf("########################################################\n"); printf("Url: %s\n",documenturi); printf("got subname \"%s\": title \"%s\". Nr %i, dokument_size %i attrib: %s\n",subname,title,count,dokument_size, attributes); printf("########################################################\n"); printf("calling bbdocument_add():\n"); #ifdef DEBUG_TIME gettimeofday(&start_time, NULL); #endif intrespons = bbdocument_add(subname,documenturi,documenttype,document,dokument_size,lastmodified,acl_allow,acl_denied,title,doctype, attributes, attrkeys); printf(":bbdocument_add end\n"); printf("########################################################\n"); #ifdef DEBUG_TIME gettimeofday(&end_time, NULL); printf("Time debug: bbdn_docadd runing bbdocument_add() time: %f\n",getTimeDifference(&start_time, &end_time)); #endif free(subname); free(documenturi); free(documenttype); free(document); free(acl_allow); free(acl_denied); free(title); free(doctype); free(attributes); // send status if ((n=sendall(socket, &intrespons, sizeof(intrespons))) == -1) { perror("Cant recv filerest"); exit(1); } } else if (packedHedder.command == bbc_opencollection) { char *subname; char path[PATH_MAX]; printf("open collection\n"); if ((i=recv(socket, &intrespons, sizeof(intrespons),MSG_WAITALL)) == -1) err(1, "Cant read intrespons"); subname = malloc(intrespons +1); if ((i=recv(socket, subname, intrespons,MSG_WAITALL)) == -1) err(1, "Cant read subname"); GetFilPathForLot(path, 1, subname); strcat(path, "fullyCrawled"); unlink(path); free(subname); } else if (packedHedder.command == bbc_closecollection) { printf("closecollection\n"); char *subname; //subname if ((i=recv(socket, &intrespons, sizeof(intrespons),MSG_WAITALL)) == -1) { perror("Cant read intrespons"); exit(1); } subname = malloc(intrespons +1); if ((i=recv(socket, subname, intrespons,MSG_WAITALL)) == -1) { perror("Cant read subname"); exit(1); } bbdocument_close(attrkeys); attrkeys = NULL; //toDo må bruke subname, og C ikke perl her printf("cleanin lots start\n"); char command[PATH_MAX]; snprintf(command,sizeof(command),"perl %s -l -s \"%s\"",bfile("perl/cleanLots.pl"),subname); printf("running \"%s\"\n",command); intrespons = system(command); printf("cleanin lots end\n"); // legger subnamet til listen over ventene subnavn, og huper searchd. lot_recache_collection(subname); /* We are done crawling */ { int fd = lotOpenFileNoCasheByLotNrl(1, "fullyCrawled", ">>", '\0', subname); if (fd == -1) { warn("Unable to write fullyCrawled file"); } else { close(fd); } } free(subname); if ((n=sendall(socket, &intrespons, sizeof(intrespons))) == -1) { perror("Cant recv filerest"); exit(1); } } else if (packedHedder.command == bbc_deleteuri) { printf("deleteuri\n"); char *subname, *uri; //subname if ((i=recv(socket, &intrespons, sizeof(intrespons),MSG_WAITALL)) == -1) { perror("Cant read intrespons"); exit(1); } subname = malloc(intrespons +1); if ((i=recv(socket, subname, intrespons,MSG_WAITALL)) == -1) { perror("Cant read subname"); exit(1); } subname[intrespons] = '\0'; if ((i=recv(socket, &intrespons, sizeof(intrespons),MSG_WAITALL)) == -1) { perror("Cant read intrespons"); exit(1); } uri = malloc(intrespons +1); if ((i=recv(socket, uri, intrespons,MSG_WAITALL)) == -1) { perror("Cant read uri"); exit(1); } uri[intrespons] = '\0'; printf("going to delete: %s from %s\n", uri, subname); /* Add docid to the gced file */ { FILE *fh; unsigned int DocID, lastmodified; unsigned int lotNr; int err = 0; if (uriindex_get(uri, &DocID, &lastmodified, subname) == 0) { fprintf(stderr,"Unable to get uri info. uri=\"%s\",subname=\"%s\".",uri,subname); perror("Unable to get uri info"); err++; } if (!err) { lotNr = rLotForDOCid(DocID); if ((fh = lotOpenFileNoCasheByLotNr(lotNr,"gced","a", 'e',subname)) == NULL) { perror("can't open gced file"); err++; } else { fwrite(&DocID, sizeof(DocID), 1, fh); fclose(fh); } } if (!err) { struct reformat *re; if((re = reopen(rLotForDOCid(DocID), sizeof(struct DocumentIndexFormat), "DocumentIndex", subname, RE_HAVE_4_BYTES_VERSION_PREFIX)) == NULL) { perror("can't reopen()"); err++; } else { DIS_delete(RE_DocumentIndex(re, DocID)); reclose(re); } } //markerer at den er skitten if (!err) { FILE *dirtfh; dirtfh = lotOpenFileNoCashe(DocID,"dirty","ab",'e',subname); fwrite("1",1,1,dirtfh); fclose(dirtfh); } if (err == 0) bbdocument_delete(uri, subname); } free(subname); intrespons = 1; // Always return ok for now if ((n=sendall(socket, &intrespons, sizeof(intrespons))) == -1) { perror("Cant recv filerest"); exit(1); } } else if (packedHedder.command == bbc_deletecollection) { printf("deletecollection\n"); char *subname, *uri; //subname if ((i=recv(socket, &intrespons, sizeof(intrespons),MSG_WAITALL)) == -1) { perror("Cant read intrespons"); exit(1); } subname = malloc(intrespons +1); if ((i=recv(socket, subname, intrespons,MSG_WAITALL)) == -1) { perror("Cant read subname"); exit(1); } subname[intrespons] = '\0'; printf("going to delete collection: %s\n", subname); intrespons = bbdocument_deletecoll(subname); if ((n=sendall(socket, &intrespons, sizeof(intrespons))) == -1) { perror("Cant recv filerest"); exit(1); } free(subname); } else if (packedHedder.command == bbc_addwhisper) { whisper_t add; char *subname; if ((i=recv(socket, &intrespons, sizeof(intrespons),MSG_WAITALL)) == -1) err(1, "Cant read intrespons"); subname = malloc(intrespons+1); if ((i=recv(socket, subname, intrespons,MSG_WAITALL)) == -1) { perror("Cant read subname"); exit(1); } subname[intrespons] = '\0'; if ((i=recv(socket, &add, sizeof(add),MSG_WAITALL)) == -1) err(1, "Cant read add whisper"); gcwhisper_write(subname, add); free(subname); } else if (packedHedder.command == bbc_HasSufficientSpace) { char *subname; //subname if ((i=recvall(socket, &intrespons, sizeof(intrespons))) == 0) { perror("Cant read intrespons"); exit(1); } subname = malloc(intrespons +1); if ((i=recvall(socket, subname, intrespons)) == 0) { perror("Cant read subname"); exit(1); } // tester bare i lot 1 her. Må også sjekke andre loter når vi begynner å støtte frlere disker på ES. intrespons = lotHasSufficientSpace(1, 4096, subname); if ((n=sendall(socket, &intrespons, sizeof(intrespons))) == -1) { perror("Cant recv filerest"); exit(1); } printf("~Asked for HasSufficientSpace for subname \"%s\". Returnerer %d\n",subname, intrespons); free(subname); } else { printf("unnown comand. %i\n", packedHedder.command); } } ++count; // #ifdef DEBUG_BREAK_AFTER // if (count >= DEBUG_BREAK_AFTER) { // printf("exeting after %i docoments\n",count); // exit(1); // } // #endif } #ifdef DEBUG_TIME gettimeofday(&tot_end_time, NULL); printf("Time debug: bbdn total time time: %f\n",getTimeDifference(&tot_start_time, &tot_end_time)); #endif }
int main(int argc, char **argv) { int i; char *subname; struct gcaoptFormat gcaopt; gcaopt.MaxAgeDiflastSeen = (86400 * 5); //86400=1 dag gcaopt.log = NULL; gcaopt.logSummary = NULL; gcaopt.lastSeenHack = 0; gcaopt.dontcheckok = 0; extern char *optarg; extern int optind, opterr, optopt; char c; while ((c=getopt(argc,argv,"t:dlsho"))!=-1) { switch (c) { case 'h': print_usage(); break; case 't': gcaopt.MaxAgeDiflastSeen = atou(optarg); break; case 'l': if ((gcaopt.log = fopen(bfile("logs/gc"),"ab")) == NULL) { perror("logs/gc"); exit(-1); } if ((gcaopt.logSummary = fopen(bfile("logs/gcSummary"),"ab")) == NULL) { perror("logs/gcSummary"); exit(-1); } break; case 's': gcaopt.lastSeenHack = 1; break; case 'o': gcaopt.dontcheckok = 1; break; default: exit(1); } } --optind; DIR *ll; #ifndef BLACK_BOX fprintf("dette fungerer bare med black boks for nå\n"); exit(1); #endif if ((argc -optind) == 2) { subname = argv[1 +optind]; gc_coll(subname, &gcaopt); } else if ((argc -optind) == 1) { ll = listAllColl_start(); while((subname = listAllColl_next(ll)) != NULL) { printf("indexing collection \"%s\"\n",subname); gc_coll(subname, &gcaopt); } listAllColl_close(ll); } else { print_usage(); } return 0; }
void CUnitDefHandler::ParseTAUnit(std::string file, int id) { TdfParser tdfparser(file); UnitDef& ud=unitDefs[id]; ud.name = tdfparser.SGetValueMSG("UNITINFO\\UnitName"); ud.humanName = tdfparser.SGetValueMSG("UNITINFO\\name"); tdfparser.GetDef(ud.extractsMetal, "0", "UNITINFO\\ExtractsMetal"); tdfparser.GetDef(ud.windGenerator, "0", "UNITINFO\\WindGenerator"); tdfparser.GetDef(ud.tidalGenerator, "0", "UNITINFO\\TidalGenerator"); ud.health=atof(tdfparser.SGetValueDef("0", "UNITINFO\\MaxDamage").c_str()); ud.metalUpkeep=atof(tdfparser.SGetValueDef("0", "UNITINFO\\MetalUse").c_str()); ud.energyUpkeep=atof(tdfparser.SGetValueDef("0", "UNITINFO\\EnergyUse").c_str()); ud.metalMake=atof(tdfparser.SGetValueDef("0", "UNITINFO\\MetalMake").c_str()); ud.makesMetal=atof(tdfparser.SGetValueDef("0", "UNITINFO\\MakesMetal").c_str()); ud.energyMake=atof(tdfparser.SGetValueDef("0", "UNITINFO\\EnergyMake").c_str()); ud.metalStorage=atof(tdfparser.SGetValueDef("0", "UNITINFO\\MetalStorage").c_str()); ud.energyStorage=atof(tdfparser.SGetValueDef("0", "UNITINFO\\EnergyStorage").c_str()); ud.autoHeal=atof(tdfparser.SGetValueDef("0", "UNITINFO\\AutoHeal").c_str())/(16.0/30.0); ud.idleAutoHeal=atof(tdfparser.SGetValueDef("10", "UNITINFO\\IdleAutoHeal").c_str())/(16.0/30.0); ud.idleTime=atoi(tdfparser.SGetValueDef("600", "UNITINFO\\IdleTime").c_str()); ud.isMetalMaker=(ud.makesMetal>=1 && ud.energyUpkeep>ud.makesMetal*40); ud.controlRadius=32; ud.losHeight=20; ud.metalCost=atof(tdfparser.SGetValueDef("0", "UNITINFO\\BuildCostMetal").c_str()); if(ud.metalCost<1) //avoid some nasty divide by 0 etc ud.metalCost=1; ud.mass=atof(tdfparser.SGetValueDef("0", "UNITINFO\\Mass").c_str()); if(ud.mass<=0) ud.mass=ud.metalCost; ud.energyCost=atof(tdfparser.SGetValueDef("0", "UNITINFO\\BuildCostEnergy").c_str()); ud.buildTime=atof(tdfparser.SGetValueDef("0", "UNITINFO\\BuildTime").c_str()); if(ud.buildTime<1) //avoid some nasty divide by 0 etc ud.buildTime=1; ud.aihint=id; //fix ud.losRadius=atof(tdfparser.SGetValueDef("0", "UNITINFO\\SightDistance").c_str()); ud.airLosRadius=atof(tdfparser.SGetValueDef("0", "UNITINFO\\SightDistance").c_str())*1.5; ud.tooltip=tdfparser.SGetValueDef(ud.name,"UNITINFO\\Description"); ud.moveType=0; tdfparser.GetDef(ud.canfly, "0", "UNITINFO\\canfly"); tdfparser.GetDef(ud.canmove, "0", "UNITINFO\\canmove"); tdfparser.GetDef(ud.builder, "0", "UNITINFO\\Builder"); tdfparser.GetDef(ud.upright, "0", "UNITINFO\\Upright"); tdfparser.GetDef(ud.onoffable, "0", "UNITINFO\\onoffable"); tdfparser.GetDef(ud.maxSlope, "0", "UNITINFO\\MaxSlope"); ud.maxHeightDif=40*tan(ud.maxSlope*(PI/180)); ud.maxSlope = cos(ud.maxSlope*(PI/180)); tdfparser.GetDef(ud.minWaterDepth, "-10e6", "UNITINFO\\MinWaterDepth"); tdfparser.GetDef(ud.maxWaterDepth, "10e6", "UNITINFO\\MaxWaterDepth"); std::string value; ud.floater = tdfparser.SGetValue(value, "UNITINFO\\Waterline"); tdfparser.GetDef(ud.waterline, "0", "UNITINFO\\Waterline"); if(ud.waterline>8 && ud.canmove) ud.waterline+=5; //make subs travel at somewhat larger depths to reduce vulnerability to surface weapons tdfparser.GetDef(ud.selfDCountdown, "5", "UNITINFO\\selfdestructcountdown"); ud.speed=atof(tdfparser.SGetValueDef("0", "UNITINFO\\MaxVelocity").c_str())*30; ud.maxAcc=atof(tdfparser.SGetValueDef("0.5", "UNITINFO\\acceleration").c_str()); ud.maxDec=atof(tdfparser.SGetValueDef("0.5", "UNITINFO\\BrakeRate").c_str())*0.1; ud.turnRate=atof(tdfparser.SGetValueDef("0", "UNITINFO\\TurnRate").c_str()); ud.buildSpeed=atof(tdfparser.SGetValueDef("0", "UNITINFO\\WorkerTime").c_str()); ud.buildDistance=atof(tdfparser.SGetValueDef("64", "UNITINFO\\Builddistance").c_str()); ud.buildDistance=max(128.f,ud.buildDistance); ud.armoredMultiple=atof(tdfparser.SGetValueDef("1", "UNITINFO\\DamageModifier").c_str()); ud.armorType=damageArrayHandler->GetTypeFromName(ud.name); // info->AddLine("unit %s has armor %i",ud.name.c_str(),ud.armorType); ud.radarRadius=atoi(tdfparser.SGetValueDef("0", "UNITINFO\\RadarDistance").c_str()); ud.sonarRadius=atoi(tdfparser.SGetValueDef("0", "UNITINFO\\SonarDistance").c_str()); ud.jammerRadius=atoi(tdfparser.SGetValueDef("0", "UNITINFO\\RadarDistanceJam").c_str()); ud.sonarJamRadius=atoi(tdfparser.SGetValueDef("0", "UNITINFO\\SonarDistanceJam").c_str()); ud.stealth=!!atoi(tdfparser.SGetValueDef("0", "UNITINFO\\Stealth").c_str()); ud.targfac=!!atoi(tdfparser.SGetValueDef("0", "UNITINFO\\istargetingupgrade").c_str()); ud.isFeature=!!atoi(tdfparser.SGetValueDef("0", "UNITINFO\\IsFeature").c_str()); ud.canResurrect=!!atoi(tdfparser.SGetValueDef("0", "UNITINFO\\canResurrect").c_str()); ud.canCapture=!!atoi(tdfparser.SGetValueDef("0", "UNITINFO\\canCapture").c_str()); ud.hideDamage=!!atoi(tdfparser.SGetValueDef("0", "UNITINFO\\HideDamage").c_str()); ud.isCommander=!!atoi(tdfparser.SGetValueDef("0", "UNITINFO\\commander").c_str()); ud.cloakCost=atof(tdfparser.SGetValueDef("-1", "UNITINFO\\CloakCost").c_str()); ud.cloakCostMoving=atof(tdfparser.SGetValueDef("-1", "UNITINFO\\CloakCostMoving").c_str()); if(ud.cloakCostMoving<0) ud.cloakCostMoving=ud.cloakCost; if(ud.cloakCost>=0) ud.canCloak=true; else ud.canCloak=false; ud.startCloaked=!!atoi(tdfparser.SGetValueDef("0", "UNITINFO\\init_cloaked").c_str()); ud.decloakDistance=atof(tdfparser.SGetValueDef("-1", "UNITINFO\\mincloakdistance").c_str()); ud.highTrajectoryType=atoi(tdfparser.SGetValueDef("0", "UNITINFO\\HighTrajectory").c_str()); ud.canKamikaze=!!atoi(tdfparser.SGetValueDef("0", "UNITINFO\\kamikaze").c_str()); ud.kamikazeDist=atof(tdfparser.SGetValueDef("-25", "UNITINFO\\kamikazedistance").c_str())+25; //we count 3d distance while ta count 2d distance so increase slightly tdfparser.GetDef(ud.canfly, "0", "UNITINFO\\canfly"); tdfparser.GetDef(ud.canmove, "0", "UNITINFO\\canmove"); tdfparser.GetDef(ud.canhover, "0", "UNITINFO\\canhover"); if(tdfparser.SGetValue(value, "UNITINFO\\floater")) tdfparser.GetDef(ud.floater, "0", "UNITINFO\\floater"); tdfparser.GetDef(ud.builder, "0", "UNITINFO\\Builder"); if(ud.builder && !ud.buildSpeed) //core anti is flagged as builder for some reason ud.builder=false; ud.wantedHeight=atof(tdfparser.SGetValueDef("0", "UNITINFO\\cruisealt").c_str());; ud.hoverAttack = !!atoi(tdfparser.SGetValueDef("0", "UNITINFO\\hoverattack").c_str()); ud.dontLand = !!atoi(tdfparser.SGetValueDef("0", "UNITINFO\\dontland").c_str()); tdfparser.GetDef(ud.transportSize, "0", "UNITINFO\\transportsize"); tdfparser.GetDef(ud.transportCapacity, "0", "UNITINFO\\transportcapacity"); ud.isAirBase=!!atoi(tdfparser.SGetValueDef("0", "UNITINFO\\isAirBase").c_str()); ud.stunnedCargo=!ud.isAirBase; ud.loadingRadius=220; tdfparser.GetDef(ud.transportMass, "100000", "UNITINFO\\TransportMass"); tdfparser.GetDef(ud.wingDrag, "0.07", "UNITINFO\\WingDrag"); //drag caused by wings tdfparser.GetDef(ud.wingAngle, "0.08", "UNITINFO\\WingAngle"); //angle between front and the wing plane tdfparser.GetDef(ud.drag, "0.005", "UNITINFO\\Drag"); //how fast the aircraft loses speed (see also below) tdfparser.GetDef(ud.frontToSpeed, "0.1", "UNITINFO\\frontToSpeed"); //fudge factor for lining up speed and front of plane tdfparser.GetDef(ud.speedToFront, "0.07", "UNITINFO\\speedToFront");//fudge factor for lining up speed and front of plane tdfparser.GetDef(ud.myGravity, "0.4", "UNITINFO\\myGravity"); //planes are slower than real airplanes so lower gravity to compensate tdfparser.GetDef(ud.maxBank, "0.8", "UNITINFO\\maxBank"); //max roll tdfparser.GetDef(ud.maxPitch, "0.45", "UNITINFO\\maxPitch"); //max pitch this plane tries to keep tdfparser.GetDef(ud.turnRadius, "500", "UNITINFO\\turnRadius"); //hint to the ai about how large turn radius this plane needs tdfparser.GetDef(ud.maxAileron, "0.015", "UNITINFO\\maxAileron"); //turn speed around roll axis tdfparser.GetDef(ud.maxElevator, "0.01", "UNITINFO\\maxElevator"); //turn speed around pitch axis tdfparser.GetDef(ud.maxRudder, "0.004", "UNITINFO\\maxRudder"); //turn speed around yaw axis ud.categoryString=tdfparser.SGetValueDef("", "UNITINFO\\Category"); ud.category=CCategoryHandler::Instance()->GetCategories(tdfparser.SGetValueDef("", "UNITINFO\\Category")); ud.noChaseCategory=CCategoryHandler::Instance()->GetCategories(tdfparser.SGetValueDef("", "UNITINFO\\NoChaseCategory")); // info->AddLine("Unit %s has cat %i",ud.humanName.c_str(),ud.category); for(int a=0;a<16;++a){ char c[50]; sprintf(c,"%i",a+1); string name; tdfparser.GetDef(name, "", std::string("UNITINFO\\")+"weapon"+c); WeaponDef *wd = weaponDefHandler->GetWeapon(name); if(!wd){ if(a>2) //allow empty weapons among the first 3 break; else continue; } while(ud.weapons.size()<a){ if(!weaponDefHandler->GetWeapon("NOWEAPON")) info->AddLine("Error: Spring requires a NOWEAPON weapon type to be present as a placeholder for missing weapons"); else ud.weapons.push_back(UnitDef::UnitDefWeapon("NOWEAPON",weaponDefHandler->GetWeapon("NOWEAPON"),0,float3(0,0,1),-1,0,0)); } string badTarget; tdfparser.GetDef(badTarget, "", std::string("UNITINFO\\") + "badTargetCategory"+c); unsigned int btc=CCategoryHandler::Instance()->GetCategories(badTarget); if(a<3){ switch(a){ case 0: tdfparser.GetDef(badTarget, "", std::string("UNITINFO\\") + "wpri_badTargetCategory"); break; case 1: tdfparser.GetDef(badTarget, "", std::string("UNITINFO\\") + "wsec_badTargetCategory"); break; case 2: tdfparser.GetDef(badTarget, "", std::string("UNITINFO\\") + "wspe_badTargetCategory"); break; } btc|=CCategoryHandler::Instance()->GetCategories(badTarget); } string onlyTarget; tdfparser.GetDef(onlyTarget, "", std::string("UNITINFO\\") + "onlyTargetCategory"+c); unsigned int otc; if(!onlyTarget.empty()) otc=CCategoryHandler::Instance()->GetCategories(onlyTarget); else otc=0xffffffff; unsigned int slaveTo=atoi(tdfparser.SGetValueDef("0", string("UNITINFO\\WeaponSlaveTo")+c).c_str()); float3 mainDir=tdfparser.GetFloat3(float3(1,0,0),string("UNITINFO\\WeaponMainDir")+c); mainDir.Normalize(); float angleDif=cos(atof(tdfparser.SGetValueDef("360", string("UNITINFO\\MaxAngleDif")+c).c_str())*PI/360); ud.weapons.push_back(UnitDef::UnitDefWeapon(name,wd,slaveTo,mainDir,angleDif,btc,otc)); } tdfparser.GetDef(ud.canDGun, "0", "UNITINFO\\candgun"); string TEDClass=tdfparser.SGetValueDef("0", "UNITINFO\\TEDClass").c_str(); ud.TEDClassString=TEDClass; if(ud.extractsMetal) { ud.extractRange = readmap->extractorRadius; ud.type = "MetalExtractor"; } else if(ud.transportCapacity) { ud.type = "Transport"; } else if(ud.builder) { if(TEDClass!="PLANT") ud.type = "Builder"; else ud.type = "Factory"; } else if(ud.canfly && !ud.hoverAttack) { if(!ud.weapons.empty() && ud.weapons[0].def!=0 && (ud.weapons[0].def->type=="AircraftBomb" || ud.weapons[0].def->type=="TorpedoLauncher")){ ud.type = "Bomber"; if(ud.turnRadius==500) //only reset it if user hasnt set it explicitly ud.turnRadius=800; //hint to the ai about how large turn radius this plane needs } else { ud.type = "Fighter"; } tdfparser.GetDef(ud.maxAcc, "0.065", "UNITINFO\\maxAcc"); //engine power } else if(ud.canmove) { ud.type = "GroundUnit"; } else { ud.type = "Building"; } ud.movedata=0; if(ud.canmove && !ud.canfly && ud.type!="Factory"){ string moveclass=tdfparser.SGetValueDef("", "UNITINFO\\MovementClass"); ud.movedata=moveinfo->GetMoveDataFromName(moveclass); if(ud.movedata->moveType==MoveData::Hover_Move || ud.movedata->moveType==MoveData::Ship_Move){ ud.upright=true; } if(ud.canhover){ if(ud.movedata->moveType!=MoveData::Hover_Move){ info->AddLine("Inconsistant move data hover %i %s %s",ud.movedata->pathType,ud.humanName.c_str(),moveclass.c_str()); } } else if(ud.floater){ if(ud.movedata->moveType!=MoveData::Ship_Move){ info->AddLine("Inconsistant move data ship %i %s %s",ud.movedata->pathType,ud.humanName.c_str(),moveclass.c_str()); } } else { if(ud.movedata->moveType!=MoveData::Ground_Move){ info->AddLine("Inconsistant move data ground %i %s %s",ud.movedata->pathType,ud.humanName.c_str(),moveclass.c_str()); } } // info->AddLine("%s uses movetype %i",ud.humanName.c_str(),ud.movedata->pathType); } if(ud.maxAcc!=0 && ud.speed!=0) ud.drag=1.0/(ud.speed/GAME_SPEED*1.1/ud.maxAcc)-ud.wingAngle*ud.wingAngle*ud.wingDrag; //meant to set the drag such that the maxspeed becomes what it should be std::string objectname; tdfparser.GetDef(objectname, "", "UNITINFO\\Objectname"); ud.model.modelpath = "objects3d/" + objectname; ud.model.modelname = objectname; tdfparser.GetDef(ud.wreckName, "", "UNITINFO\\Corpse"); tdfparser.GetDef(ud.deathExplosion, "", "UNITINFO\\ExplodeAs"); tdfparser.GetDef(ud.selfDExplosion, "", "UNITINFO\\SelfDestructAs"); string buildpic; tdfparser.GetDef(buildpic, "", "UNITINFO\\BuildPic"); if(buildpic.empty()) { //try pcx first and then bmp if no pcx exist CFileHandler bfile("unitpics/" + ud.name + ".pcx"); if(bfile.FileExists()) { CBitmap bitmap("unitpics/" + ud.name + ".pcx"); ud.unitimage = bitmap.CreateTexture(false); } else { CFileHandler bfile("unitpics/" + ud.name + ".bmp"); if(bfile.FileExists()){ CBitmap bitmap("unitpics/" + ud.name + ".bmp"); ud.unitimage = bitmap.CreateTexture(false); } else { CBitmap bitmap; ud.unitimage = bitmap.CreateTexture(false); } } } else { CBitmap bitmap("unitpics/" + buildpic); ud.unitimage = bitmap.CreateTexture(false); } ud.power = (ud.metalCost + ud.energyCost/60.0f); tdfparser.GetDef(ud.activateWhenBuilt, "0", "UNITINFO\\ActivateWhenBuilt"); ud.xsize=atoi(tdfparser.SGetValueDef("1", "UNITINFO\\FootprintX").c_str())*2;//ta has only half our res so multiply size with 2 ud.ysize=atoi(tdfparser.SGetValueDef("1", "UNITINFO\\FootprintZ").c_str())*2; ud.needGeo=false; if(ud.type=="Building" || ud.type=="Factory"){ CreateYardMap(&ud, tdfparser.SGetValueDef("c", "UNITINFO\\YardMap")); } else { ud.yardmap = 0; } ud.leaveTracks=!!atoi(tdfparser.SGetValueDef("0", "UNITINFO\\LeaveTracks").c_str()); ud.trackWidth=atof(tdfparser.SGetValueDef("32", "UNITINFO\\TrackWidth").c_str()); ud.trackOffset=atof(tdfparser.SGetValueDef("0", "UNITINFO\\TrackOffset").c_str()); ud.trackStrength=atof(tdfparser.SGetValueDef("0", "UNITINFO\\TrackStrength").c_str()); ud.trackStretch=atof(tdfparser.SGetValueDef("1", "UNITINFO\\TrackStretch").c_str()); if(ud.leaveTracks && groundDecals) ud.trackType=groundDecals->GetTrackType(tdfparser.SGetValueDef("StdTank", "UNITINFO\\TrackType")); ud.canDropFlare=!!atoi(tdfparser.SGetValueDef("0", "UNITINFO\\CanDropFlare").c_str()); ud.flareReloadTime=atof(tdfparser.SGetValueDef("5", "UNITINFO\\FlareReload").c_str()); ud.flareEfficieny=atof(tdfparser.SGetValueDef("0.5", "UNITINFO\\FlareEfficiency").c_str()); ud.flareDelay=atof(tdfparser.SGetValueDef("0.3", "UNITINFO\\FlareDelay").c_str()); ud.flareDropVector=tdfparser.GetFloat3(ZeroVector,"UNITINFO\\FlareDropVector"); ud.flareTime=atoi(tdfparser.SGetValueDef("3", "UNITINFO\\FlareTime").c_str())*30; ud.flareSalvoSize=atoi(tdfparser.SGetValueDef("4", "UNITINFO\\FlareSalvoSize").c_str()); ud.flareSalvoDelay=atoi(tdfparser.SGetValueDef("0.1", "UNITINFO\\FlareSalvoDelay").c_str())*30; ud.smoothAnim = !!atoi(tdfparser.SGetValueDef("0", "UNITINFO\\SmoothAnim").c_str()); LoadSound(tdfparser, ud.sounds.ok, "ok1"); LoadSound(tdfparser, ud.sounds.select, "select1"); LoadSound(tdfparser, ud.sounds.arrived, "arrived1"); LoadSound(tdfparser, ud.sounds.build, "build"); LoadSound(tdfparser, ud.sounds.activate, "activate"); LoadSound(tdfparser, ud.sounds.deactivate, "deactivate"); LoadSound(tdfparser, ud.sounds.cant, "cant"); LoadSound(tdfparser, ud.sounds.underattack, "underattack"); }
int bbdocument_convert(char filetype[],char document[],const int dokument_size, buffer *outbuffer, const char titlefromadd[], char *subname, char *documenturi, unsigned int lastmodified, char *acl_allow, char *acl_denied, struct hashtable **metahash) { FILE *filconfp=NULL; char filconvertetfile_real[216] = ""; char filconvertetfile_out_txt[216] = ""; char filconvertetfile_out_html[216] = ""; int exeocbuflen; int i; char *documentfinishedbuftmp; char fileconverttemplate[1024]; struct fileFilterFormat *fileFilter = NULL; #ifdef DEBUG_TIME struct timeval start_time, end_time; #endif printf("bbdocument_convert: dokument_size %i, title \"%s\",filetype \"%s\"\n",dokument_size,titlefromadd,filetype); //konverterer filnavn til liten case for (i=0;i < strlen(filetype);i++) { //printf("%c\n",filetype[i]); filetype[i] = btolower(filetype[i]); } //hvis vi har et html dokument kan vi bruke dette direkte //er dog noe uefektist her, ved at vi gjør minnekopiering if ((strcmp(filetype,"htm") == 0) || (strcmp(filetype,"html") == 0 )) { if (titlefromadd[0]=='\0') { bmemcpy(outbuffer, document, dokument_size); } else { // Noen dokumenter kan ha lagt ved tittel ved add uten å ha tittel i html-en (f.eks epost). // Legg til korrekt tittel i dokumentet. // Html-parseren tar kun hensyn til den første tittelen, så det skal holde å legge den til // øverst i dokumentet. bprintf(outbuffer, "<title>%s</title>\n", titlefromadd); bmemcpy(outbuffer, document, dokument_size); } return 1; } else if (strcmp(filetype,"hnxt") == 0) { ntobr(document, dokument_size); bprintf(outbuffer, html_tempelate, titlefromadd, document); return 1; } #ifdef DEBUG printf("strcmp done\n"); #endif struct fileFilterFormat *fileFilterOrginal; if (NULL == (fileFilterOrginal = hashtable_search(h_fileFilter,filetype) )) { printf("don't have converter for \"%s\"\n",filetype); #ifdef DEBUG printf("writing to unknownfiltype.log\n"); FILE *fp; if ((fp = fopen(bfile("logs/unknownfiltype.log"),"ab")) == NULL) { perror(bfile("logs/unknownfiltype.log")); } else { printf("title %s\n",titlefromadd); printf("filetype %s\n",filetype); fprintf(fp,"%s: %s\n",titlefromadd,filetype); fclose(fp); } printf("writing to unknownfiltype.log. done\n"); #endif return 0; } //hvis dette er en fil av type text trenger vi ikke og konvertere den. if (strcmp((*fileFilterOrginal).format,"text") == 0) { #ifdef DEBUG printf("fileFilter ses it is a file of format text. Can use it direktly\n"); #endif char *cpbuf; int cpbufsize; //konvertere alle \n til <br> cpbufsize = (dokument_size + 512 +1); cpbuf = malloc(cpbufsize); memcpy(cpbuf,document,dokument_size); cpbuf[dokument_size] = '\0'; //stripper < og > tegn, da html parseren vil tro det er html tagger. //de er jo som kjent på formater < og > stripTags(cpbuf,dokument_size); #ifdef DEBUG printf("document %i\n",strlen(document)); #endif bprintf(outbuffer, html_text_tempelate, titlefromadd, cpbuf); //printf("documentfinishedbuf %i\n", buffer_length(outbuffer)); free(cpbuf); return 1; } //vi må lage en kopi av filfilter infoen, da vi skal endre den. fileFilter = malloc(sizeof(struct fileFilterFormat)); memcpy(fileFilter, fileFilterOrginal, sizeof(struct fileFilterFormat)); #ifdef DEBUG printf("have converter for file type\n"); #endif /***************************************************************************** Vi har konverter. Må skrive til fil får å kunne sende den med *****************************************************************************/ pid_t pid = getpid(); sprintf(fileconverttemplate, "%s-%d", filconvertetfile, rand()); sprintf(filconvertetfile_real,"%s-%u.%s",fileconverttemplate, (unsigned int)pid,filetype); sprintf(filconvertetfile_out_txt,"%s-%u.txt",fileconverttemplate, (unsigned int)pid); sprintf(filconvertetfile_out_html,"%s-%u.html",fileconverttemplate, (unsigned int)pid); #ifdef DEBUG printf("bbdocument_convert: filconvertetfile_real \"%s\"\n",filconvertetfile_real); #endif if ((filconfp = fopen(filconvertetfile_real,"wb")) == NULL) { perror(filconvertetfile_real); exit(1); } flock(fileno(filconfp),LOCK_EX); fwrite(document,1,dokument_size,filconfp); fclose(filconfp); //reåpner den read only, ogi lager en delt lås på filen, slik at vi ungår at perl /tmp watch sletter den. if ((filconfp = fopen(filconvertetfile_real,"rb")) == NULL) { perror(filconvertetfile_real); exit(1); } flock(fileno(filconfp),LOCK_SH); //convert to text. /*****************************************************************************/ strsandr((*fileFilter).command,"#file",filconvertetfile_real); strsandr((*fileFilter).command,"#outtxtfile",filconvertetfile_out_txt); strsandr((*fileFilter).command,"#outhtmlfile",filconvertetfile_out_html); exeocbuflen = (dokument_size * 2) + 513; // XXX: Find a better way //(*documentfinishedbufsize); if ((documentfinishedbuftmp = malloc(exeocbuflen)) == NULL) { perror("Can't malloc documentfinishedbuftmp"); return 0; } switch (fileFilter->filtertype) { case FILTER_EXEOC: run_filter_exeoc( documentfinishedbuftmp, exeocbuflen, fileFilter, metahash ); break; case FILTER_PERL_PLUGIN: run_filter_perlplugin( documentfinishedbuftmp, exeocbuflen , fileFilter, metahash ); break; default: errx(1, "Unknown filtertype '%d'", fileFilter->filtertype); } #ifdef USE_LIBEXTRACTOR if (fileFilter->attrwhitelist != NULL) add_libextractor_attr(metahash, filconvertetfile_real, fileFilter->attrwhitelist); #endif //<<<<<<< bbdocument.c //======= //her parser vi argumenter selv, og hver space blir en ny argyment, selv om vi //bruker "a b", som ikke riktig blir to argumenter her, a og b //splitter på space får å lage en argc /*TokCount = split((*fileFilter).command, " ", &splitdata); //#ifdef DEBUG printf("splitet comand in %i, program is \"%s\"\n",TokCount,splitdata[0]); //#endif printf("running: %s\n",(*fileFilter).command); //sender med størelsen på buferen nå. Vil få størelsen på hva vi leste tilbake char *execobuf = malloc(exeocbuflen); //>>>>>>> 1.64 //<<<<<<< bbdocument.c //======= char *envpairpath = strdup(/tmp/converter-metadata-XXXXXX); char envpair[PATH_MAX]; mktemp(envpairpath); sprintf(envpair, "SDMETAFILE=%s", envpairpath); free(envpairpath); envpairpath = envpair + strlen("SDMETAFILE="); char *shargs[] = { "/usr/bin/env", NULL, "/bin/sh", "-c", NULL, NULL, }; shargs[1] = envpair; shargs[4] = fileFilter->command; #ifdef DEBUG_TIME gettimeofday(&start_time, NULL); #endif if (!exeoc_timeout(shargs, execobuf, &exeocbuflen, &ret, 120)) { printf("dident get any data from exeoc. But can be a filter that creates files, sow we wil continue\n"); execobuf[0] = '\0'; exeocbuflen = 0; } #ifdef DEBUG_TIME gettimeofday(&end_time, NULL); printf("Time debug: exeoc_timeout() time: %f\n",getTimeDifference(&start_time, &end_time)); #endif */ /* if (metahash) { FILE *metafp; *metahash = create_hashtable(3, ht_stringhash, ht_stringcmp); if ((metafp = fopen(envpairpath, "r")) != NULL) { char *key, *value, line[2048]; while (fgets(line, sizeof(line), metafp)) { char *p, *p2; // Comment if (line[0] == '#') continue; key = line; p = strchr(key, '='); if (p == NULL) { fprintf(stderr, "Invalid format on meta spec file: %s\n", line); continue; } p2 = p; while (isspace(*(p2-1))) p2--; *p2 = '\0'; p++; // Skip past = while (isspace(*p)) p++; value = p; while (isspace(*key)) key++; if (value[strlen(value)-1] == '\n') value[strlen(value)-1] = '\0'; printf("Got pair: %s = %s\n", key, value); hashtable_insert(*metahash, strdup(key), strdup(value)); } fclose(metafp); unlink(envpairpath); } else { printf("Couldn't open %s\n", envpairpath); } } */ //>>>>>>> 1.64 #ifdef DEBUG //printf("did convert to %i bytes (strlen %i)\n",exeocbuflen,strlen(documentfinishedbuftmp)); #endif if (strcmp((*fileFilter).outputformat,"text") == 0) { //stripper < og > tegn, da html parseren vil tro det er html tagger. //de er jo som kjent på formater < og > stripTags(documentfinishedbuftmp,strlen(documentfinishedbuftmp)); bprintf(outbuffer, html_text_tempelate,titlefromadd,documentfinishedbuftmp); } else if (strcmp((*fileFilter).outputformat,"html") == 0) { //html trenger ikke å konvertere //dette er altså outputformat html. Ikke filtype outputformat. Filtupe hondteres lengere oppe //ToDo: må vel kopiere inn noe data her??? bprintf(outbuffer, "%s", documentfinishedbuftmp); // Ved filkonvertering vil tittelen som sendes med (from add) være filnavnet. // Den vil vi kun bruke dersom dokumentet i seg selv ikke har en tittel. // Derfor legges den tittelen til nederst i dokumentet: bprintf(outbuffer, "<title>%s</title>\n", titlefromadd); } else if (strcmp((*fileFilter).outputformat,"textfile") == 0) { FILE *fh; struct stat inode; char *cpbuf; printf("filconvertetfile_out_txt: \"%s\"\n",filconvertetfile_out_txt); if ((fh = fopen(filconvertetfile_out_txt,"rb")) == NULL) { printf("can't open out file \"%s\"\n",filconvertetfile_out_txt); perror(filconvertetfile_out_txt); goto bbdocument_convert_error; } fstat(fileno(fh),&inode); if ((cpbuf = malloc(inode.st_size +1)) == NULL) { perror("malloc"); goto bbdocument_convert_error; } fread(cpbuf,1,inode.st_size,fh); cpbuf[inode.st_size] = '\0'; printf("did read back %i bytes from file \"%s\"\n",(int)inode.st_size,filconvertetfile_out_txt); printf("strlen cpbuf: %i\n",strlen(cpbuf)); //stripper < og > tegn, da html parseren vil tro det er html tagger. //de er jo som kjent på formater < og > stripTags(cpbuf,inode.st_size); fclose(fh); //printf("have size %i\n",(*documentfinishedbufsize)); bprintf(outbuffer, html_text_tempelate, titlefromadd, cpbuf); free(cpbuf); //seltter filen vi lagde unlink(filconvertetfile_out_txt); } else if (strcmp((*fileFilter).outputformat,"htmlfile") == 0) { FILE *fh; struct stat inode; size_t n; char buf[4096]; if ((fh = fopen(filconvertetfile_out_html,"rb")) == NULL) { printf("can't open out file \"%s\"\n",filconvertetfile_out_html); perror(filconvertetfile_out_html); goto bbdocument_convert_error; } fstat(fileno(fh),&inode); #if 0 if ((*documentfinishedbufsize) > inode.st_size) { (*documentfinishedbufsize) = inode.st_size; } #endif while ((n = fread(buf, 1, sizeof(buf)-1, fh)) > 0) { bmemcpy(outbuffer, buf, n); } fclose(fh); unlink(filconvertetfile_out_html); bprintf(outbuffer, "<title>%s</title>\n", titlefromadd); } else if (strcmp(fileFilter->outputformat, "dir") == 0 || strcmp(fileFilter->outputformat, "diradd") == 0) { char *p, *pstart; /* Does len do anything any more? */ int len, failed = 0; int type; /* 1 for dir, 2 for diradd */ type = (strcmp(fileFilter->outputformat, "dir") == 0) ? 1 : 2; len = exeocbuflen; p = strdup(documentfinishedbuftmp); pstart = p; if (p == NULL) { goto bbdocument_convert_error; } bprintf(outbuffer, html_text_tempelate, titlefromadd, ""); while (*p != '\0') { char *ft, *path; char *part = NULL; ft = p; for (; *p != ' '; p++) len--; *p = '\0'; if (type == 2) { part = ++p; for (; *p != ' '; p++) len--; *p = '\0'; } path = ++p; /* XXX: strchr() */ for (; *p != '\n'; p++) len--; if (*p == '\n') *p++ = '\0'; /* We have a new file, let's get to work on it */ //printf("########Got: %s: %s\n", ft, path); { char *docbuf; int docbufsize; struct stat st; FILE *fp; if (stat(path, &st) == -1) { /* Unable to access file, move on to the next */ fprintf(stderr, "File: %s\n", path); perror("stat"); failed++; continue; } docbuf = malloc(st.st_size + 1); /* Make room for our lovely '\0' */ if (docbuf == NULL) { perror("malloc"); failed++; free(docbuf); continue; } docbufsize = st.st_size; if ((fp = fopen(path, "r")) == NULL) { perror("fopen"); failed++; free(docbuf); continue; } fread(docbuf, 1, docbufsize, fp); fclose(fp); unlink(path); docbuf[docbufsize] = '\0'; //runarb: 18 jan 2008: har var titel "", ikke titlefromadd, som gjorde at 24so crawling mistet titler. if (bbdocument_convert(ft, docbuf, docbufsize, outbuffer, titlefromadd, subname, documenturi, lastmodified, acl_allow, acl_denied, NULL) == 0) { fprintf(stderr, "Failed on bbdocument_convert.\n"); failed++; free(docbuf); continue; } free(docbuf); } } if (type == 2) { assert(0); #if 0 *documentfinishedbufsize = 1; *documentfinishedbuf = strdup("."); #endif } //printf("Got this: %d %d<<\n%s\n", strlen(*documentfinishedbuf), *documentfinishedbufsize, *documentfinishedbuf); free(pstart); } else { printf("unknown dokument outputformat \"%s\"\n",fileFilter->outputformat); free(documentfinishedbuftmp); goto bbdocument_convert_error; } free(documentfinishedbuftmp); unlink(filconvertetfile_real); unlink(filconvertetfile_out_txt); unlink(filconvertetfile_out_html); fclose(filconfp); #ifndef DEBUG //runarb: 13okr2007: hvorfor ver denne komentert ut? Det hoper seg opp med filer //unlink(filconvertetfile_real); #endif //printf("documentfinishedbuf is: \n...\n%s\n...\n", *documentfinishedbuf); free(fileFilter); return 1; bbdocument_convert_error: if (filconvertetfile_real[0] != '\0') { unlink(filconvertetfile_real); } if (filconvertetfile_out_txt[0] != '\0') { unlink(filconvertetfile_out_txt); } if (filconvertetfile_out_html[0] != '\0') { unlink(filconvertetfile_out_html); } if (filconfp != NULL) { fclose(filconfp); } if (fileFilter != fileFilter) { free(fileFilter); } if (fileFilter != NULL) { free(fileFilter); } return 0; }
int bbdocument_init(container **attrkeys) { DIR *dirp; FILE *filep; char buf[512]; char path[512]; struct dirent *dp; char lines[512]; char **splitdata; int TokCount; struct fileFilterFormat *fileFilter = NULL; char fileFilterName[] = "fileFilter"; perl_embed_init(NULL, 1); //chtbl_init(&htbl, PRIME_TBLSIZ, bbdocument_h, bbdocument_hmatch, free); h_fileFilter = create_hashtable(PRIME_TBLSIZ, bbdocument_h, bbdocument_hmatch); printf("opening %s\n",bfile(fileFilterName)); if ((dirp = opendir(bfile(fileFilterName))) == NULL) { fprintf(stderr,"warn: cant open fileFilter \"%s\". Cant use fileFilters\n",bfile(fileFilterName)); return 1; } while ((dp = readdir(dirp)) != NULL) { if (dp->d_name[0] == '.') { continue; } sprintf(path,"%s/%s/",bfile(fileFilterName),dp->d_name); sprintf(buf,"%sruninfo",path); printf("%s\n",buf); if ((filep = fopen(buf,"r")) == NULL) { printf("no runinfo file for \"%s\"\n",dp->d_name); continue; } printf("loading \"%s\"\n",dp->d_name); while ((!feof(filep)) && (fgets(lines,sizeof(lines) -1,filep) != NULL)) { //blanke linjer og komentarer som starter på # if ((lines[0] == '\n') || (lines[0] == '#')) { continue; } //void chomp(char string[]) chomp(lines); //printf("line %s\n",lines); TokCount = split(lines, ": ", &splitdata); //printf("\tfound %d token(s):\n", TokCount); /* if (TokCount != 2) { printf("bad config line \"%s\". Splitet in %i elements\n",lines,TokCount); continue; } */ if (strcmp(splitdata[0],"documentstype") == 0) { //legger til det gamle filteret if (fileFilter != NULL) { if (NULL != hashtable_search(h_fileFilter,fileFilter->documentstype )) { printf("####################### BUG ################################\n"); printf("allredy have a filter for \"%s\"!\n",fileFilter->documentstype); printf("#######################/BUG ################################\n"); } //add to hash printf("inserting %s\n",(*fileFilter).documentstype); //chtbl_insert(&htbl,(void *)fileFilter); if (!hashtable_insert(h_fileFilter,fileFilter->documentstype,fileFilter) ) { printf("cant insert\n"); exit(-1); } printf("end inserting\n"); } //begynner på et nytt filter fileFilter = malloc(sizeof(struct fileFilterFormat)); fileFilter->attrwhitelist = NULL; //ikke alle filfiltere har sat alle opsjoner, så vi nulstiller alt, slik at det er lett og strcmp()'e //etter en verdi, uten at vi må tenke på at den kansje ikke er satt. memset(fileFilter,'\0',sizeof(struct fileFilterFormat)); // default til FILTER_EXEOC fileFilter->filtertype = FILTER_EXEOC; strcpy((*fileFilter).documentstype,splitdata[1]); strlcpy(fileFilter->path, path, sizeof fileFilter->path); } else if (strcmp(splitdata[0],"command") == 0) { //vi kan ha : i komandoen. Kopierer derfor først inn hele, så fjerner vi command: //strcpy((*fileFilter).command,splitdata[1]); strscpy((*fileFilter).command,lines,sizeof((*fileFilter).command)); strcasesandr((*fileFilter).command,sizeof((*fileFilter).command),"command: ",""); //leger til path der vi har sakt vi skal ha lokal path ( ./ ) strcasesandr((*fileFilter).command,sizeof((*fileFilter).command),"./",path); printf(".command %s\n",(*fileFilter).command); } else if (strcmp(splitdata[0],"comment") == 0) { strscpy((*fileFilter).comment,splitdata[1],sizeof((*fileFilter).comment)); } else if (strcmp(splitdata[0],"format") == 0) { strscpy((*fileFilter).format,splitdata[1],sizeof((*fileFilter).format)); } else if (strcmp(splitdata[0],"outputtype") == 0) { //stdio, file, osv,, strcpy((*fileFilter).outputtype,splitdata[1]); } else if (strcmp(splitdata[0],"outputformat") == 0) { //text, html strcpy((*fileFilter).outputformat,splitdata[1]); } else if (strcmp(splitdata[0], "filtertype") == 0) { if (strcmp(splitdata[1], FILTER_EXEOC_STR) == 0) fileFilter->filtertype = FILTER_EXEOC; else if (strcmp(splitdata[1], FILTER_PERL_PLUGIN_STR) == 0) fileFilter->filtertype = FILTER_PERL_PLUGIN; else errx(1, "Unknown filtertype %s\n", splitdata[1]); } else if (strcmp(splitdata[0], "attrwhitelist") == 0) { // TODO: Free fileFilter->attrwhitelist if (!split(splitdata[1], ",", &fileFilter->attrwhitelist)) warnx("attrwhitelist was empty."); } else { printf("unknown command \"%s\"\n",lines); } //clean FreeSplitList(splitdata); } if (fileFilter != NULL) { //add to hash printf("inserting %s\n",(*fileFilter).documentstype); //chtbl_insert(&htbl,(void *)fileFilter); if (!hashtable_insert(h_fileFilter,fileFilter->documentstype,fileFilter) ) { printf("cant insert\n"); exit(-1); } printf("end inserting\n"); } //markerer at vi har lagt det til fileFilter = NULL; fclose(filep); } closedir(dirp); if (attrkeys != NULL) { *attrkeys = ropen(); } return 1; }