Improve warning message on open failure.

Before:

W0430 08:26:53.958887 41576 moonfire-nvr.cc:123]
driveway: Output error; sleeping before retrying: open
031e423c-2a0c-4450-b6cc-8af629606a90: Permission denied

After:

W0430 08:50:06.315666 43514 moonfire-nvr.cc:123]
driveway: Output error; sleeping before retrying: open
98592dfa-4ab4-427a-8ad0-033325f0f0b3 (within dir /home/slamb/moonfire/sample):
Permission denied
This commit is contained in:
Scott Lamb 2016-04-30 08:51:58 -07:00
parent 374975a73c
commit 713d7863de
3 changed files with 13 additions and 4 deletions

View File

@ -59,12 +59,15 @@ namespace {
class RealFile : public File { class RealFile : public File {
public: public:
explicit RealFile(int fd) : fd_(fd) {} RealFile(re2::StringPiece name, int fd)
: name_(name.data(), name.size()), fd_(fd) {}
RealFile(const RealFile &) = delete; RealFile(const RealFile &) = delete;
void operator=(const RealFile &) = delete; void operator=(const RealFile &) = delete;
~RealFile() final { Close(); } ~RealFile() final { Close(); }
const std::string &name() const { return name_; }
int Access(const char *path, int mode, int flags) final { int Access(const char *path, int mode, int flags) final {
return faccessat(fd_, path, mode, flags) < 0 ? errno : 0; return faccessat(fd_, path, mode, flags) < 0 ? errno : 0;
} }
@ -106,7 +109,7 @@ class RealFile : public File {
if (ret < 0) { if (ret < 0) {
return errno; return errno;
} }
f->reset(new RealFile(ret)); f->reset(new RealFile(StrCat(name_, "/", path), ret));
return 0; return 0;
} }
@ -145,6 +148,7 @@ class RealFile : public File {
} }
private: private:
std::string name_;
int fd_ = -1; int fd_ = -1;
}; };
@ -186,7 +190,7 @@ class RealFilesystem : public Filesystem {
if (ret < 0) { if (ret < 0) {
return errno; return errno;
} }
f->reset(new RealFile(ret)); f->reset(new RealFile(path, ret));
return 0; return 0;
} }

View File

@ -58,6 +58,9 @@ class File {
// Close the file, ignoring the result. // Close the file, ignoring the result.
virtual ~File() {} virtual ~File() {}
// A name for the file (typically assigned at open time).
virtual const std::string &name() const = 0;
// faccessat(), returning 0 on success or errno>0 on failure. // faccessat(), returning 0 on success or errno>0 on failure.
virtual int Access(const char *path, int mode, int flags) = 0; virtual int Access(const char *path, int mode, int flags) = 0;
@ -95,6 +98,7 @@ class File {
class MockFile : public File { class MockFile : public File {
public: public:
MOCK_CONST_METHOD0(name, const std::string &());
MOCK_METHOD3(Access, int(const char *, int, int)); MOCK_METHOD3(Access, int(const char *, int, int));
MOCK_METHOD0(Close, int()); MOCK_METHOD0(Close, int());

View File

@ -143,7 +143,8 @@ bool SampleFileWriter::Open(const char *filename, std::string *error_message) {
int ret = int ret =
parent_dir_->Open(filename, O_WRONLY | O_CREAT | O_EXCL, 0600, &file_); parent_dir_->Open(filename, O_WRONLY | O_CREAT | O_EXCL, 0600, &file_);
if (ret != 0) { if (ret != 0) {
*error_message = StrCat("open ", filename, ": ", strerror(ret)); *error_message = StrCat("open ", filename, " (within dir ",
parent_dir_->name(), "): ", strerror(ret));
return false; return false;
} }
return true; return true;