searchengine/util/cpixtools/src/cpixfstools.cpp
changeset 8 6547bf8ca13a
parent 2 6c1a2771f4b7
child 23 d4d56f5e7c55
equal deleted inserted replaced
7:a5fbfefd615f 8:6547bf8ca13a
    50 	}
    50 	}
    51 
    51 
    52 	size_t
    52 	size_t
    53             i = len-1;
    53             i = len-1;
    54 
    54 
    55 	while (1)/*(i >= 0)*/ {
    55 	while (i > 0) {
    56             char c = child[i];
    56             char c = child[i];
    57             if (c == '\\' ||
    57             if (c == '\\' ||
    58                 c == '/') {
    58                 c == '/') {
    59                 // found parent path 
    59                 // found parent path 
    60                 break;
    60                 break;
    61             }
    61             }
    62             i--;
    62             i--;
    63 	}
    63 	}
    64         
    64         
    65 	if (i+1 >= FILENAME_MAX 
    65 	if (i+1 >= FILENAME_MAX 
    66             || i+1 >= bufSize)
    66             || i+1 >= bufSize
    67             //|| i < 0) 
    67             || i == 0) {
    68 	{
       
    69             return -1;
    68             return -1;
    70 	}
    69 	}
    71 	
    70 	
    72         memcpy(parent, child, i);
    71         memcpy(parent, child, i);
    73         parent[i] = 0;
    72         parent[i] = 0;
    85             * d = NULL;
    84             * d = NULL;
    86 
    85 
    87         Cpt_EINTR_RETRY_PTR(d, opendir(path));
    86         Cpt_EINTR_RETRY_PTR(d, opendir(path));
    88 
    87 
    89 	if (d) {
    88 	if (d) {
    90             Cpt_EINTR_RETRY_SP( closedir(d) );
    89             int
       
    90                 result;
       
    91             Cpt_EINTR_RETRY(result, closedir(d));
       
    92             
    91             rv = true;
    93             rv = true;
    92 	} 
    94 	} 
    93     
    95     
    94         return rv;
    96         return rv;
    95     }
    97     }
   203         char 
   205         char 
   204             parent[FILENAME_MAX];
   206             parent[FILENAME_MAX];
   205 
   207 
   206         if (getparent(parent, sizeof(parent), path) >= 0) {
   208         if (getparent(parent, sizeof(parent), path) >= 0) {
   207             // make the parent
   209             // make the parent
   208             (void)mkdirs(parent, mod); 
   210             mkdirs(parent, mod); 
   209         } 
   211         } 
   210         
   212         
   211         return mkdir(path, mod);
   213         return mkdir(path, mod);
   212     }
   214     }
   213 
   215 
   223                         open(path,
   225                         open(path,
   224                              O_WRONLY | O_CREAT | O_TRUNC,
   226                              O_WRONLY | O_CREAT | O_TRUNC,
   225                              mod));
   227                              mod));
   226         if (fd != -1)
   228         if (fd != -1)
   227             {
   229             {
   228 
   230                 int
   229                 Cpt_EINTR_RETRY_SP( close(fd) );
   231                     result;
       
   232                 Cpt_EINTR_RETRY(result,
       
   233                                 close(fd));
   230             }
   234             }
   231 
   235 
   232         return fd == -1 ? -1 : 0;
   236         return fd == -1 ? -1 : 0;
   233     }
   237     }
   234 
   238 
   244         bool
   248         bool
   245             rv = (fd != -1);
   249             rv = (fd != -1);
   246         
   250         
   247         if (rv)
   251         if (rv)
   248             {
   252             {
   249             Cpt_EINTR_RETRY_SP( close(fd) );
   253                 int
       
   254                     result;
       
   255                 Cpt_EINTR_RETRY(result,
       
   256                                 close(fd));
   250             }
   257             }
   251 
   258 
   252         return rv;
   259         return rv;
   253     }
   260     }
   254 
   261 
   316                 rv = buf.st_size;
   323                 rv = buf.st_size;
   317             }
   324             }
   318         
   325         
   319         return rv;
   326         return rv;
   320     }
   327     }
   321 
   328     
   322 
   329     namespace 
       
   330 	{
       
   331 		class DirectorySizeCalculator : public IFileVisitor 
       
   332 		{
       
   333 		public: 
       
   334 		
       
   335 			DirectorySizeCalculator()
       
   336 			:	totalSize_(0)
       
   337 			{}
       
   338 		
       
   339 			virtual bool visitFile(const char * path) 
       
   340 			{
       
   341 				totalSize_ += filesize(path); 
       
   342 				return true; 
       
   343 			}
       
   344 	
       
   345 			virtual DirVisitResult visitDirPre(const char * path)
       
   346 			{
       
   347 			    //To avoid compiler warning. 
       
   348 			    std::string ret = path;
       
   349 			    
       
   350 			    return IFV_CONTINUE;
       
   351 			}
       
   352 			
       
   353 	        virtual bool visitDirPost(const char * path) 
       
   354 	        {
       
   355                 std::string ret = path; 
       
   356                 ret.empty();    
       
   357 	        	return true; 
       
   358 	        }
       
   359 			
       
   360 			long totalSize() 
       
   361 			{
       
   362 				return totalSize_; 
       
   363 			}
       
   364 			
       
   365 		private: 
       
   366 			
       
   367 			long totalSize_; 
       
   368 	
       
   369 		};
       
   370 	}
       
   371 
       
   372     off_t dirsize(const char* path) 
       
   373     {
       
   374     	DirectorySizeCalculator sizeCalculator; 
       
   375     	traverse(path, &sizeCalculator);
       
   376     	return sizeCalculator.totalSize(); 
       
   377     }
   323 
   378 
   324     time_t filemodified(const char * path)
   379     time_t filemodified(const char * path)
   325     {
   380     {
   326         time_t
   381         time_t
   327             rv = 0;
   382             rv = 0;
   337             {
   392             {
   338                 rv = buf.st_mtime;
   393                 rv = buf.st_mtime;
   339             }
   394             }
   340         
   395         
   341         return rv;
   396         return rv;
       
   397     }
       
   398 
       
   399     std::string appendpath(const char* path, const char* item)
       
   400     {
       
   401     	std::string ret; 
       
   402     	ret += path; 
       
   403     	if (ret[ret.length()-1] != '\\' && ret[ret.length()-1] != '/') {
       
   404 			ret += DIR_SEPARATOR;
       
   405     	}
       
   406     	ret += item;
       
   407     	return ret;
   342     }
   408     }
   343 
   409 
   344     bool fgetline(FILE* file, std::string& line)
   410     bool fgetline(FILE* file, std::string& line)
   345    	{
   411    	{
   346 		std::ostringstream buf; 
   412 		std::ostringstream buf; 
   653     }
   719     }
   654 
   720 
   655 
   721 
   656     DIRSentry::~DIRSentry()
   722     DIRSentry::~DIRSentry()
   657     {
   723     {
   658         Cpt_EINTR_RETRY_SP( closedir(d_) );
   724         int
       
   725             result;
       
   726 
       
   727         Cpt_EINTR_RETRY(result, closedir(d_));
   659     }
   728     }
   660 
   729 
   661 
   730 
   662 
   731 
   663     bool traverse_(const char   * path,
   732     bool traverse_(const char   * path,
   767     FileDescSentry::~FileDescSentry()
   836     FileDescSentry::~FileDescSentry()
   768     {
   837     {
   769         if (fileDesc_ != NULL
   838         if (fileDesc_ != NULL
   770             && *fileDesc_ != -1)
   839             && *fileDesc_ != -1)
   771             {
   840             {
   772                 Cpt_EINTR_RETRY_SP( close(*fileDesc_) );
   841                 int
       
   842                     result;
       
   843 
       
   844                 Cpt_EINTR_RETRY(result,
       
   845                                 close(*fileDesc_));
   773             }
   846             }
   774     }
   847     }
   775     
   848     
   776     
   849     
   777     FileSentry::FileSentry(FILE * file)
   850     FileSentry::FileSentry(FILE * file)
   788 
   861 
   789 	FileSentry::~FileSentry()
   862 	FileSentry::~FileSentry()
   790 	{
   863 	{
   791 		if ( file_ != NULL )
   864 		if ( file_ != NULL )
   792 		{
   865 		{
   793 			Cpt_EINTR_RETRY_SP( fclose(file_) ); 
   866 			int result;
       
   867 			
       
   868 			Cpt_EINTR_RETRY(result, 
       
   869 							fclose(file_)); 
   794 		}
   870 		}
   795 	}
   871 	}
   796 
   872 
   797 
   873 
   798 }
   874 }