steve
steve

Reputation: 183

unable to obtain all permission bits

On my shell, I have a file and I change the permissions of this file with sudo chmod 4755 <file>. Upon calling lstat on this file, I'm seeing correct information about its permissions, specifically that it has 4755 as its permission mode.

In my golang program, is there a reason why I'm not getting the correct permission mode bits? Could it be that I'm just formatting the result I get from FileInfo().Mode().Perm() incorrectly? Could it be that the upper 3 bits are "special"?

Thanks for the help!

Upvotes: 2

Views: 318

Answers (1)

CorbinMc
CorbinMc

Reputation: 588

Short Answer: The three upper bits ARE special and need to be accessed separately.

Long Answer: The documentation explains that the 9 (out of usually 12) least significant bits are considered the standard Unix permissions.

The documentation also defines the behavior of the Perm() function you are calling:

func (m FileMode) Perm() FileMode

Perm returns the Unix permission bits in m.

This means that perm is not defined to return any of the additional bits you are looking for.

Furthermore, the source code shows that the Perm() function is masking the value returned from FileMode() with 0777 causing the initial three bits to be disregarded.

The ModeSetuid, ModeSetgid, and ModeSticky bits (4, 2, and 1 respectively) must each be accessed individually as constants of the FileMode type. Do this by performing your own masks.

In order to determine whether the sticky bit is set, for example, do (FileInfo().Mode() & ModeSticky) != 0. The same applies for ModeSetuid and ModSetgid.

Upvotes: 1

Related Questions