From 82c4b05fe47da9eaddc9028b6f622e8403732ccc Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 7 Jul 2013 13:04:19 -0400 Subject: [PATCH] File, Directory Permissions - Added default and override permissions for files and directories. --- src/Directory.cpp | 4 ++-- src/Directory.h | 2 +- src/File.cpp | 10 +++++++--- src/File.h | 4 ++-- test/directory.t.cpp | 20 +++++++++++++++++++- test/file.t.cpp | 20 +++++++++++++++++++- 6 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/Directory.cpp b/src/Directory.cpp index 07f9d2db5..1b04ff583 100644 --- a/src/Directory.cpp +++ b/src/Directory.cpp @@ -85,9 +85,9 @@ Directory& Directory::operator= (const Directory& other) } //////////////////////////////////////////////////////////////////////////////// -bool Directory::create () +bool Directory::create (int mode /* = 0755 */) { - return mkdir (_data.c_str (), 0755) == 0 ? true : false; + return mkdir (_data.c_str (), mode) == 0 ? true : false; } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/Directory.h b/src/Directory.h index a0dd33acf..1532d8f14 100644 --- a/src/Directory.h +++ b/src/Directory.h @@ -42,7 +42,7 @@ public: Directory& operator= (const Directory&); - virtual bool create (); + virtual bool create (int mode = 0755); virtual bool remove () const; std::vector list (); diff --git a/src/File.cpp b/src/File.cpp index 0655a7cd7..ca5146ca6 100644 --- a/src/File.cpp +++ b/src/File.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -90,10 +91,11 @@ File& File::operator= (const File& other) } //////////////////////////////////////////////////////////////////////////////// -bool File::create () +bool File::create (int mode /* = 0640 */) { if (open ()) { + fchmod (_h, mode); close (); return true; } @@ -361,12 +363,14 @@ time_t File::btime () const } //////////////////////////////////////////////////////////////////////////////// -bool File::create (const std::string& name) +bool File::create (const std::string& name, int mode /* = 0640 */) { - std::ofstream out (expand (name).c_str ()); + std::string full_name = expand (name); + std::ofstream out (full_name.c_str ()); if (out.good ()) { out.close (); + chmod (full_name.c_str (), mode); return true; } diff --git a/src/File.h b/src/File.h index ffbec1ab9..940e682d7 100644 --- a/src/File.h +++ b/src/File.h @@ -45,7 +45,7 @@ public: File& operator= (const File&); - virtual bool create (); + virtual bool create (int mode = 0640); virtual bool remove () const; bool open (); @@ -72,7 +72,7 @@ public: virtual time_t ctime () const; virtual time_t btime () const; - static bool create (const std::string&); + static bool create (const std::string&, int mode = 0640); static std::string read (const std::string&); static bool read (const std::string&, std::string&); static bool read (const std::string&, std::vector &); diff --git a/test/directory.t.cpp b/test/directory.t.cpp index 9b3f6f19e..faba20029 100644 --- a/test/directory.t.cpp +++ b/test/directory.t.cpp @@ -35,7 +35,7 @@ Context context; int main (int argc, char** argv) { - UnitTest t (37); + UnitTest t (49); Directory tmp ("tmp"); tmp.create (); @@ -124,6 +124,24 @@ int main (int argc, char** argv) t.is (d9._data, "/", "parent /one --> /"); t.notok (d9.up (), "parent / --> false"); + // Test permissions. + Directory d10 ("tmp/dir.perm"); + d10.create (0750); + t.ok (d10.exists (), "Directory::create perm file exists"); + mode_t m = d10.mode (); + t.ok (m & S_IFDIR, "Directory::mode tmp/dir.perm S_IFDIR good"); + t.ok (m & S_IRUSR, "Directory::mode tmp/dir.perm r-------- good"); + t.ok (m & S_IWUSR, "Directory::mode tmp/dir.perm -w------- good"); + t.ok (m & S_IXUSR, "Directory::mode tmp/dir.perm --x------ good"); + t.ok (m & S_IRGRP, "Directory::mode tmp/dir.perm ---r----- good"); + t.notok (m & S_IWGRP, "Directory::mode tmp/dir.perm ----w---- good"); + t.ok (m & S_IXGRP, "Directory::mode tmp/dir.perm -----x--- good"); + t.notok (m & S_IROTH, "Directory::mode tmp/dir.perm ------r-- good"); + t.notok (m & S_IWOTH, "Directory::mode tmp/dir.perm -------w- good"); + t.notok (m & S_IXOTH, "Directory::mode tmp/dir.perm --------x good"); + d10.remove (); + t.notok (d10.exists (), "Directory::remove temp/dir.perm file no longer exists"); + tmp.remove (); t.notok (tmp.exists (), "tmp dir removed."); diff --git a/test/file.t.cpp b/test/file.t.cpp index b675a6c20..d19fd9cf8 100644 --- a/test/file.t.cpp +++ b/test/file.t.cpp @@ -35,7 +35,7 @@ Context context; int main (int argc, char** argv) { - UnitTest t (15); + UnitTest t (27); Directory tmp ("tmp"); tmp.create (); @@ -70,6 +70,24 @@ int main (int argc, char** argv) t.ok (f7.remove (), "File::remove tmp/file.t.3.txt good"); t.notok (f7.exists (), "File::remove new file no longer exists"); + // Test permissions. + File f8 ("tmp/file.t.perm.txt"); + f8.create (0744); + t.ok (f8.exists (), "File::create perm file exists"); + mode_t m = f8.mode (); + t.ok (m & S_IFREG, "File::mode tmp/file.t.perm.txt S_IFREG good"); + t.ok (m & S_IRUSR, "File::mode tmp/file.t.perm.txt r-------- good"); + t.ok (m & S_IWUSR, "File::mode tmp/file.t.perm.txt -w------- good"); + t.ok (m & S_IXUSR, "File::mode tmp/file.t.perm.txt --x------ good"); + t.ok (m & S_IRGRP, "File::mode tmp/file.t.perm.txt ---r----- good"); + t.notok (m & S_IWGRP, "File::mode tmp/file.t.perm.txt ----w---- good"); + t.notok (m & S_IXGRP, "File::mode tmp/file.t.perm.txt -----x--- good"); + t.ok (m & S_IROTH, "File::mode tmp/file.t.perm.txt ------r-- good"); + t.notok (m & S_IWOTH, "File::mode tmp/file.t.perm.txt -------w- good"); + t.notok (m & S_IXOTH, "File::mode tmp/file.t.perm.txt --------x good"); + f8.remove (); + t.notok (f8.exists (), "File::remove perm file no longer exists"); + tmp.remove (); t.notok (tmp.exists (), "tmp dir removed.");