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

  • SEARCH
  • Home
  • 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 5947855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:01:27+00:00 2026-05-22T17:01:27+00:00

In my code I try to get the permissions for a file with _stat().

  • 0

In my code I try to get the permissions for a file with _stat(). Currently I want to run it under Windows. The method is as follows:

bool CFile::Private::checkPermissions(std::string sFilename, CFile::EOpenmode iOpenmode)
{
  std::string sErrMsg = "";
  bool bResult = true;
  struct _stat buf;
  int iResult = 0;

  // Get data associated with "crt_stat.c": 
  iResult = _stat( sFilename.c_str(), &buf );

  // Check if statistics are valid: 
  if( iResult != 0 )
  {
    switch (errno)
    {
      case ENOENT:
        sErrMsg = "File: " + sFilename + " not found.";
        break;
      case EINVAL:
        sErrMsg = "Invalid parameter to _stat(filename, &buf).";
        break;
      default:
        /* Should never be reached. */
        sErrMsg = "Unexpected error in _stat(filename, &buf).";
    }
    throw std::runtime_error(sErrMsg);
  }
  else
  {
    if((iOpenmode & CFile::Read) && (!(buf.st_mode & S_IREAD)))
    {
      bResult = false;
    }
    if((iOpenmode & CFile::Write) && (!(buf.st_mode & S_IWRITE)))
    {
      bResult = false;
    }
  }
  return bResult;
}

The only way to get ‘false’ for permission is to set the file’s attribute ‘read only’. Instead of this, set the security properties of the user (reject writing and reading) will get ‘true’ for checkPermissions(…). How to check both, the attributes and the user permissions for Windows?

Rumo

  • 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-05-22T17:01:28+00:00Added an answer on May 22, 2026 at 5:01 pm

    _stat is a function that is not native to Windows. It’s a helper function to ease the porting of UNIX programs to Windows. But the UNIX file model just doesn’t apply to Windows, so not all fields make sense. For instance, Windows has real ACL’s, not rwx bits. There’s just no way to fit all the ACL information in st_mode.

    If you want to test ACL permissions, the proper way is to just try: call CreateFile() and check GetLastError(). Trying to get file permissions up front is not reliable as they can change at any time.

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

Sidebar

Related Questions

When I try this code: class MyStuff: def average(a, b, c): # Get the
When I try to use the code below I get a duplicate variable error
I get the following error when I try and execute the code down below.
When I try to set the z variable in the code below, I get
I'm currently downloading an HTML page, using the following code: Try Dim req As
Please help me, I want to run log4net on my windows service. And it
I use the following code try to create an array of string vectors, I
We have following code: try { // some code throwing MyException } catch (MyException
I have this code try { //AN EXCEPTION IS GENERATED HERE!!! } catch {
try { // Code } catch (Exception ex) { Logger.Log(Message, ex); throw; } In

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.