Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8577141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:05:38+00:00 2026-06-11T20:05:38+00:00

I have written a function to test for the readability/writability of a folder. For

  • 0

I have written a function to test for the readability/writability of a folder.

For unit testing it, I need to produce the different cases:

  • a folder with readable and writable files
  • a folder with readable files (not writable)
  • a folder not writable and not readable.

Here is the code for the function I came to, up to this point:

void FileUtils::checkPath(std::string path, bool &readable, bool &writable)
{
   namespace bf = boost::filesystem;
   std::string filePath = path + "/test.txt";

   // remove a possibly existing test file
   remove(filePath.c_str());

   // check that the path exists
   if(!bf::is_directory(path))
   {
      readable = writable = false;
      return;
   }

   // try to write in the location
   std::ofstream outfile (filePath.c_str());
   outfile << "I can write!" << std::endl;
   outfile.close();

   if(!outfile.fail() && !outfile.bad())
   {
      writable = true;
   }

   // look for a file to read
   std::ifstream::pos_type size;
   char * memblock;
   for (bf::recursive_directory_iterator it(path); it != bf::recursive_directory_iterator(); ++it)
   {
      if (!is_directory(*it))
      {
         std::string sFilePath = it->path().string();
         std::ifstream file(sFilePath.c_str(), std::ios::in|std::ios::binary|std::ios::ate);
         if (file.is_open())
         {
           size = file.tellg();
           if(size > 0)
           {
              memblock = new char [1];
              file.seekg (0, std::ios::beg);
              file.read (memblock, 1);
              file.close();
              delete[] memblock;
              if(!file.fail() && !file.bad())
              {
                 readable = true;
              }
              break;
           }
         }
         else
         {
            // there is a non readable file in the folder
            // readable = false;
            break;
         }
      }
   }

   // delete the test file
   remove(filePath.c_str());
}

Now with the tests (done with Google tests):

TEST_F(FileUtilsTest, shouldCheckPath)
{
   // given an existing folder
   namespace fs = boost::filesystem;
   fs::create_directory("/tmp/to_be_deleted");

   bool readable = false, writable = false;

   FileUtils::checkPath("/tmp/to_be_deleted",readable, writable);

   fs::boost::filesystem::remove_all("/tmp/to_be_deleted");

   EXPECT_TRUE(readable && writable);
}

I will add more for the other cases when I will have gone further.

Now the game is open to propose a better solution 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:05:39+00:00Added an answer on June 11, 2026 at 8:05 pm

    The foolproof way to check permissions is to literally check the file mode. In the case of directory permissions, the meaning of “readable” and “writable” might be surprising:

    • Read – allows you to list the contents of the directory
    • Write – allows you to create, rename, delete files from the directory, essentially modifying the list of contents (also requires execute)
    • Execute – allows you to access (both read and write) and change properties of files within the directory

    So if you have a directory with just the execute bit set, you can still read and write to the files inside. By turning the execute bit off, you can disable access to the files. As far as the contained files are concerned, the most you can figure out from the directory permissions is:

    • --x or r-x: existing files can be read and written to
    • -wx or rwx: existing files can be read and written to, files can be created, renamed and deleted
    • otherwise: you have no access to the files at all

    To determine if a file is readable but not writeable (or vice versa) you need to check the permissions of the file itself. The directory can only tell you if the files can be accessed in general.

    You can use stat() or access() (see BЈовић’s comment) to find out the permissions for a file or directory. Since you’re already using boost, you can also use boost::filesystem::status() which simply wraps stat().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to unit testing and have written the following test: /** * @expectedException
I have written a function that sorts a big scale of data. To test
I have a haskell file test.hs. In this file I have written a function
I have written a JUnit test for a private function which is returning String.
I have written a function that extract the domain from hostname. e.g. www.domain.com ->
I have written this function to auto correct gender to M or F from
I have written a function which warn a user before refreshing the (I require
I have written a function that positions a tooltip just above a textbox. The
I have written a function below: void trans(double x,double y,double theta,double m,double n) {
I have written a function for a multilevel wordpress menu, but I'd like it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.