mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-06-26 10:54:26 +02:00
File, Directory Permissions
- Added default and override permissions for files and directories.
This commit is contained in:
parent
061bf4882e
commit
82c4b05fe4
6 changed files with 50 additions and 10 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
|
@ -42,7 +42,7 @@ public:
|
|||
|
||||
Directory& operator= (const Directory&);
|
||||
|
||||
virtual bool create ();
|
||||
virtual bool create (int mode = 0755);
|
||||
virtual bool remove () const;
|
||||
|
||||
std::vector <std::string> list ();
|
||||
|
|
10
src/File.cpp
10
src/File.cpp
|
@ -28,6 +28,7 @@
|
|||
#include <cmake.h>
|
||||
#include <fstream>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/file.h>
|
||||
#include <pwd.h>
|
||||
#include <unistd.h>
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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 <std::string>&);
|
||||
|
|
|
@ -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.");
|
||||
|
||||
|
|
|
@ -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.");
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue