Changeset 709
- Timestamp:
- Dec 20, 2006, 10:49:02 PM (17 years ago)
- Location:
- trunk/yat/utility
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/yat/utility/FileIO.cc
r687 r709 26 26 #include "FileIO.h" 27 27 28 #include <cerrno> 28 29 #include <iostream> 30 #include <string> 31 32 #include <sys/stat.h> 29 33 #include <unistd.h> 30 34 … … 34 38 namespace utility { 35 39 40 41 FileIO::FileIO(const std::string& path) 42 : no_file_(false), path_(path) 43 { 44 if ( stat(path_.c_str(),&stat_) && !(no_file_=(errno==ENOENT)) ) 45 throw IO_error(std::string("stat(2) call failed with errno: "+errno)); 46 } 47 48 36 49 int FileIO::access_rights(const std::string& path, 37 const std::string& bits) const { 38 if (int value=access(path.c_str(),F_OK)) { 39 std::cerr << "access_rights: File '" << path << "' does not exist." 40 << std::endl; 41 return value; 50 const std::string& bits) const 51 { 52 std::string tryme=path_; 53 if (no_file_) { 54 std::string::size_type pos=path_.find_last_of('/'); 55 tryme = ( (pos!=std::string::npos) ? path_.substr(0,pos) : "." ); 42 56 } 57 43 58 int mode=0; 44 59 for (u_int i=0; i<bits.length(); i++) … … 54 69 break; 55 70 } 56 return access(path.c_str(),mode); 71 72 return access(tryme.c_str(),mode); 73 } 74 75 76 bool FileIO::file_exists(const std::string& file) const 77 { 78 return !no_file_; 79 } 80 81 82 const std::string& FileIO::path(void) const 83 { 84 return path_; 57 85 } 58 86 -
trunk/yat/utility/FileIO.h
r687 r709 27 27 */ 28 28 29 #include "Exception.h" 30 29 31 #include <string> 30 32 #include <sys/stat.h> 31 33 #include <unistd.h> 32 34 … … 42 44 43 45 /// 46 /// \brief Constructor 47 /// 48 /// \exception IO_error if underlying stat(2) call fails in other 49 /// ways than file does not exist. 50 /// 51 explicit FileIO(const std::string& path); 52 53 /// 54 /// @brief Check file/directory permissions 55 /// 44 56 /// Check if access permissions match \a mode. \a mode must be 45 57 /// given as r, w, x, or combinations of these letters. 58 /// 59 /// @note Checking permissions on a non-existent file will return 60 /// the permissions for the parent directory, but only the parent 61 /// directory. If the parent directory does not exist then fail is 62 /// returned. 46 63 /// 47 64 /// @return On success (all requested permissions granted), zero … … 50 67 /// is returned, and errno is set appropriately. 51 68 /// 52 /// @see access(2)53 ///54 /// @note Checking for write permissions will fail if the file55 /// does not exists. This should be fixed so that when a file does56 /// not exist, permissions to create the file should be returned.57 ///58 69 int access_rights(const std::string& path,const std::string& bits) const; 59 70 60 71 /// 61 /// Check whether \a file exists.72 /// @brief Check whether \a file exists. 62 73 /// 63 74 /// @return True if \a file exists, false otherwise. 64 75 /// 65 /// @see access(2) 76 bool file_exists(const std::string& file) const; 77 66 78 /// 67 /// @todo Probably stat(2) should be used in favour of 68 /// access(2). Change the implementation. 79 /// @brief Get path associated with the object. 69 80 /// 70 inline bool file_exists(const std::string& file) const 71 { return !access(file.c_str(),F_OK); } 81 const std::string& path(void) const; 82 83 private: 84 /// 85 /// \brief The copy constructor, not implemented 86 /// 87 FileIO(const FileIO&); 88 89 bool no_file_; 90 std::string path_; 91 struct stat stat_; 72 92 }; 73 93
Note: See TracChangeset
for help on using the changeset viewer.