searchengine/oss/cl/clucene/src/clucene/store/fsdirectory.cpp
changeset 2 6c1a2771f4b7
parent 0 671dee74050a
equal deleted inserted replaced
1:6f2c1c46032b 2:6c1a2771f4b7
    85         else if ( err == EMFILE )
    85         else if ( err == EMFILE )
    86             _CLTHROWA(CL_ERR_IO, "Too many open files");
    86             _CLTHROWA(CL_ERR_IO, "Too many open files");
    87 	  }
    87 	  }
    88 
    88 
    89 	  //Store the file length
    89 	  //Store the file length
    90 	  handle->_length = fileSize(handle->fhandle);
    90 	  if ( handle->fhandle > 0 )
       
    91 	      handle->_length = fileSize(handle->fhandle);
    91 	  handle->_fpos = 0;
    92 	  handle->_fpos = 0;
    92 	  this->_pos = 0;
    93 	  this->_pos = 0;
    93   }
    94   }
    94 
    95 
    95   FSDirectory::FSIndexInput::FSIndexInput(const FSIndexInput& other): BufferedIndexInput(other){
    96   FSDirectory::FSIndexInput::FSIndexInput(const FSIndexInput& other): BufferedIndexInput(other){
   273    Directory(),
   274    Directory(),
   274    refCount(0),
   275    refCount(0),
   275    useMMap(false)
   276    useMMap(false)
   276   {
   277   {
   277   	_realpath(path,directory);//set a realpath so that if we change directory, we can still function
   278   	_realpath(path,directory);//set a realpath so that if we change directory, we can still function
   278   	if ( !directory || !*directory ){
   279   	//if ( !directory || !*directory ){
       
   280   	if ( !*directory ){
   279   		strcpy(directory,path);	
   281   		strcpy(directory,path);	
   280   	}
   282   	}
   281     
   283     
   282 		const char* tmplockdir = getLockDir();
   284 		const char* tmplockdir = getLockDir();
   283     if ( tmplockdir == NULL ) {
   285     if ( tmplockdir == NULL ) {
   285     } else {
   287     } else {
   286       strcpy(lockDir,tmplockdir);
   288       strcpy(lockDir,tmplockdir);
   287     }
   289     }
   288     
   290     
   289     // Ensure that lockDir exists and is a directory.
   291     // Ensure that lockDir exists and is a directory.
   290 		struct fileStat fstat;
   292 		struct fileStat fstat = { 0 };
   291 	  if ( fileStat(tmplockdir,&fstat) != 0 ) {
   293 	  if ( (tmplockdir) && (fileStat(tmplockdir,&fstat) != 0)  ) {
   292 			//todo: should construct directory using _mkdirs... have to write replacement
   294 			//todo: should construct directory using _mkdirs... have to write replacement
   293 			if ( _mkdir(lockDir) == -1 ){
   295 			if ( _mkdir(lockDir) == -1 ){
   294 				_CLTHROWA(CL_ERR_IO,"Cannot create temp directory"); //todo: make richer error
   296 				_CLTHROWA(CL_ERR_IO,"Cannot create temp directory"); //todo: make richer error
   295 			}
   297 			}
   296 		}
   298 		}
   399     strcat(path,PATH_DELIMITERA);
   401     strcat(path,PATH_DELIMITERA);
   400     char* pathP = path + strlen(path);
   402     char* pathP = path + strlen(path);
   401 
   403 
   402     while ( fl != NULL ){
   404     while ( fl != NULL ){
   403       strcpy(pathP,fl->d_name);
   405       strcpy(pathP,fl->d_name);
   404       fileStat(path,&buf);
   406       //fileStat(path,&buf);
   405       if ( !(buf.st_mode & S_IFDIR) ) {
   407       if ( (fileStat(path,&buf) == 0) && (!(buf.st_mode & S_IFDIR)) ) {
   406         names->push_back( fl->d_name );
   408         names->push_back( fl->d_name );
   407       }
   409       }
   408       fl = readdir(dir);
   410       fl = readdir(dir);
   409     }
   411     }
   410     closedir(dir);
   412     closedir(dir);
   462   //static
   464   //static
   463   int64_t FSDirectory::fileModified(const char* dir, const char* name){
   465   int64_t FSDirectory::fileModified(const char* dir, const char* name){
   464     struct fileStat buf;
   466     struct fileStat buf;
   465     char buffer[CL_MAX_DIR];
   467     char buffer[CL_MAX_DIR];
   466 	_snprintf(buffer,CL_MAX_DIR,"%s%s%s",dir,PATH_DELIMITERA,name);
   468 	_snprintf(buffer,CL_MAX_DIR,"%s%s%s",dir,PATH_DELIMITERA,name);
   467     fileStat( buffer, &buf );
   469     (void)fileStat( buffer, &buf );
   468     return buf.st_mtime;
   470     return buf.st_mtime;
   469   }
   471   }
   470 
   472 
   471   void FSDirectory::touchFile(const char* name){
   473   void FSDirectory::touchFile(const char* name){
   472 	  CND_PRECONDITION(directory[0]!=0,"directory is not open");
   474 	  CND_PRECONDITION(directory[0]!=0,"directory is not open");
   474     _snprintf(buffer,CL_MAX_DIR,"%s%s%s",directory,PATH_DELIMITERA,name);
   476     _snprintf(buffer,CL_MAX_DIR,"%s%s%s",directory,PATH_DELIMITERA,name);
   475 	
   477 	
   476     int32_t r = _open(buffer, O_RDWR, _S_IWRITE);
   478     int32_t r = _open(buffer, O_RDWR, _S_IWRITE);
   477 	if ( r < 0 )
   479 	if ( r < 0 )
   478 		_CLTHROWA(CL_ERR_IO,"IO Error while touching file");
   480 		_CLTHROWA(CL_ERR_IO,"IO Error while touching file");
   479 	_close(r);
   481 	(void)_close(r);
   480   }
   482   }
   481 
   483 
   482   int64_t FSDirectory::fileLength(const char* name) const {
   484   int64_t FSDirectory::fileLength(const char* name) const {
   483 	  CND_PRECONDITION(directory[0]!=0,"directory is not open");
   485 	  CND_PRECONDITION(directory[0]!=0,"directory is not open");
   484     struct fileStat buf;
   486     struct fileStat buf;
   687     int32_t r = _open(lockFile,  O_RDWR | O_CREAT | O_RANDOM | O_EXCL, 
   689     int32_t r = _open(lockFile,  O_RDWR | O_CREAT | O_RANDOM | O_EXCL, 
   688     	_S_IREAD | _S_IWRITE); //must do this or file will be created Read only
   690     	_S_IREAD | _S_IWRITE); //must do this or file will be created Read only
   689 	if ( r < 0 )
   691 	if ( r < 0 )
   690 	  return false;
   692 	  return false;
   691 	else{
   693 	else{
   692 	  _close(r);
   694 	  (void)_close(r);
   693 	  return true;
   695 	  return true;
   694 	}
   696 	}
   695 
   697 
   696   }
   698   }
   697   bool FSDirectory::FSLock::isLocked(){
   699   bool FSDirectory::FSLock::isLocked(){
   701      return Misc::dir_Exists(lockFile);
   703      return Misc::dir_Exists(lockFile);
   702   }
   704   }
   703   void FSDirectory::FSLock::release() {
   705   void FSDirectory::FSLock::release() {
   704     if (disableLocks)
   706     if (disableLocks)
   705           return;
   707           return;
   706     _unlink( lockFile );
   708     (void)_unlink( lockFile );
   707   }
   709   }
   708 
   710 
   709   TCHAR* FSDirectory::toString() const{
   711   TCHAR* FSDirectory::toString() const{
   710 	  TCHAR* ret = _CL_NEWARRAY(TCHAR, strlen(this->directory) + 13); //strlen("FSDirectory@")
   712 	  TCHAR* ret = _CL_NEWARRAY(TCHAR, strlen(this->directory) + 13); //strlen("FSDirectory@")
   711 	  _tcscpy(ret,_T("FSDirectory@"));
   713 	  _tcscpy(ret,_T("FSDirectory@"));