Jump to content

using <sys/stat.h> to look at Permissions

- - - - -

  • Please log in to reply
2 replies to this topic

#1
DavidO

DavidO

    Newbie

  • Members
  • PipPip
  • 14 posts
Later on in my current project I am going to be able to search for a file according to certain permissions(Description is below).

Right now I have figured out everything using <sys/stat.h> (except for name i used dirent.h) but I can not figure out how to determine permissions using st_mode (S_IRWXU : read, write, execute/search by owner, S_IRWXG : read, write, execute/search by group, S_IRWXO : read, write, execute/search by others ). If anyone could help that would be amazing!

(80%) Basic find function implementation:


The find function should have following format:


find <search path> <option> <target>

- search path is the path from which the program starts looking for the target.

- options is dedicated to type of search on files in the search path. In the following table

different options (arguments) with their corresponding comments are shown.

- target is the search target.

“find” Function

Category Sub-Category Option Format Example


File Attributes (20%)

File Name -name string (char *) myfile

File Size -size long int (B) 12323

You do not have to handle wild cards.


Ownership (20%)

User ID -uid integer 150

Group ID -gid integer 170


Time Attributes (20%)

Last Access Time -atime “%T-%D” “11:32:16-01/11/08”

Last Modify Time -mtime “%T-%D” “11:32:16-01/11/08”

Last Status Change Time -ctime “%T-%D” “11:32:16-01/11/08”


File Permissions (20%) Permission Number -perm integer 666


Note that permissions in this project are represented by permission number which contains three

octal digits where first digit is dedicated to the file owner, the second one for the group members

and the third one is assigned to the other users. Each digit is calculated based on the following

table.

Octal digit Text equivalent Binary value Meaning

0 --- 000 No access is granted

1 --x 001 Execute access is allowed only

2 -w- 010 Write access is allowed only

3 -wx 011 Write and execute access are allowed

4 r-- 100 Read access is allowed only

5 r-x 101 Read and execute access are allowed

6 rw- 110 Read and write access are allowed

7 rwx 111 Everything is allowed

For instance, number 644 represents following permission policy:

owner: read and write permissions,

group: only read permissions,

others: only read permissions.

"Encapsulation is a little like Las Vegas. What happens in a class, stays in a class." - ArcaneCode
Languages: C/C++, Python, Java/JavaScript, Ruby, SQL , HTML5/CSS

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
It would not intentionally change the format you are used to, they are just bit masks.

printf("%o", buffer.st_mode & 07777); //i.e. 644, ignoring higher masks such as IS_FIFO

Given this sample table:
S_IRUSR    00400    owner has read permission
S_IWUSR    00200    owner has write permission
S_IXUSR    00100    owner has execute permission

You can add the following permissions for owner:
0 |= S_IRUSR | S_IWUSR | S_IXUSR //+= 0400 + 0200 + 0100 = 0700

Or extract a specific permission:
owner_flags = flags & S_IRWXU
if(owner_flags & (S_IRUSR | S_IWUSR) == 1) { 
    //o+rw
}

They are simply a set of macros, you do not require them.

Alexander.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
DavidO

DavidO

    Newbie

  • Members
  • PipPip
  • 14 posts
Alright thanks that helps a lot! I was able to figure out most of the project using google but for some reason I was not finding a lot of clear documentation on permissions.
"Encapsulation is a little like Las Vegas. What happens in a class, stays in a class." - ArcaneCode
Languages: C/C++, Python, Java/JavaScript, Ruby, SQL , HTML5/CSS




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users