Changeset 709


Ignore:
Timestamp:
Dec 20, 2006, 10:49:02 PM (17 years ago)
Author:
Jari Häkkinen
Message:

Addresses #1 and #80. Issues fixed, but bogus params to functions exists. cleanup needed.

Location:
trunk/yat/utility
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/utility/FileIO.cc

    r687 r709  
    2626#include "FileIO.h"
    2727
     28#include <cerrno>
    2829#include <iostream>
     30#include <string>
     31
     32#include <sys/stat.h>
    2933#include <unistd.h>
    3034
     
    3438namespace utility {
    3539
     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
    3649  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) : "." );
    4256    }
     57
    4358    int mode=0;
    4459    for (u_int i=0; i<bits.length(); i++)
     
    5469            break;
    5570      }
    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_;
    5785  }
    5886
  • trunk/yat/utility/FileIO.h

    r687 r709  
    2727*/
    2828
     29#include "Exception.h"
     30
    2931#include <string>
    30 
     32#include <sys/stat.h>
    3133#include <unistd.h>
    3234
     
    4244
    4345    ///
     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    ///
    4456    /// Check if access permissions match \a mode. \a mode must be
    4557    /// 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.
    4663    ///
    4764    /// @return On success (all requested permissions granted), zero
     
    5067    /// is returned, and errno is set appropriately.
    5168    ///
    52     /// @see access(2)
    53     ///
    54     /// @note Checking for write permissions will fail if the file
    55     /// does not exists. This should be fixed so that when a file does
    56     /// not exist, permissions to create the file should be returned.
    57     ///
    5869    int access_rights(const std::string& path,const std::string& bits) const;
    5970
    6071    ///
    61     /// Check whether \a file exists.
     72    /// @brief Check whether \a file exists.
    6273    ///
    6374    /// @return True if \a file exists, false otherwise.
    6475    ///
    65     /// @see access(2)
     76    bool file_exists(const std::string& file) const;
     77
    6678    ///
    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.
    6980    ///
    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_;
    7292  };
    7393
Note: See TracChangeset for help on using the changeset viewer.