Show
Ignore:
Timestamp:
08/11/09 09:56:48 (15 years ago)
Author:
vbarta
Message:

moved cross-platform directory scanning support from tests to core (osutils)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • library/tests/test_util.cpp

    r493 r497  
    22#include "stat/exp_family.h" 
    33#include <fstream> 
    4 #include <stdexcept> 
    5 #include <string> 
    6 #include <errno.h> 
    7 #include <fcntl.h> 
    8 #include <string.h> 
    9 #include <stdio.h> 
    10 #include <sys/stat.h> 
    11 #include <sys/types.h> 
    12  
    13 #ifdef WIN32 
    14 #include "dirent.h" 
    15 #include <direct.h> 
    16 #define rmdir _rmdir 
    17 #define unlink _unlink 
    18 #else 
    19 #include <dirent.h> 
    20 #include <unistd.h> 
    21 #endif 
    224 
    235namespace bdm { 
     
    3214        src.read ( buffer, sizeof ( buffer ) - 1 ); 
    3315        return std::string ( buffer ); 
    34 } 
    35  
    36 bool remove_all ( const char *path ) { 
    37         DIR *dir; 
    38         dirent *de; 
    39  
    40         bool rv = true; 
    41         if ( ( dir = opendir ( path ) ) != 0 ) { 
    42                 try { 
    43                         std::string top ( path ); 
    44                         top += "/"; 
    45  
    46                         while ( ( de = readdir ( dir ) ) != 0 ) { 
    47                                 if ( strcmp ( de->d_name, "." ) && strcmp ( de->d_name, ".." ) ) { 
    48                                         std::string subpath ( top ); 
    49                                         subpath += de->d_name; 
    50                                         remove_all ( subpath.c_str() ); 
    51                                 } 
    52                         } 
    53                 } catch ( ... ) { 
    54                         closedir ( dir ); 
    55                         throw; 
    56                 } 
    57  
    58                 closedir ( dir ); 
    59  
    60                 if ( rmdir ( path ) ) { 
    61                         std::string msg = "can't remove dir "; 
    62                         msg += path; 
    63                         throw std::runtime_error ( msg ); 
    64                 } 
    65         } else { 
    66                 if ( ( errno == ENOTDIR ) || // Linux  
    67                      ( errno == EINVAL ) ) { // Windows 
    68                         if ( unlink ( path ) ) { 
    69                                 std::string msg = "can't remove file "; 
    70                                 msg += path; 
    71                                 throw std::runtime_error ( msg ); 
    72                         } 
    73                 } else { 
    74                         if ( errno != ENOENT ) { 
    75                                 std::string msg = "can't remove "; 
    76                                 msg += path; 
    77                                 throw std::runtime_error ( msg ); 
    78                         } else { 
    79                                 // it wasn't there in the first place 
    80                                 rv = false; 
    81                         } 
    82                 } 
    83         } 
    84  
    85         return rv; 
    8616} 
    8717