1

I'm following this simple go web app tutorial, and came across this method:

func (p *Page) save() error {
    filename := p.Title + ".txt"
    return ioutil.WriteFile(filename, p.Body, 0600)
}

About the value 0600 it says:

The octal integer literal 0600, passed as the third parameter to WriteFile, indicates that the file should be created with read-write permissions for the current user only. (See the Unix man page open(2) for details.)

Are these values stored anywhere, maybe in the os or ioutil packages as some sort of constant/enum type value with meaningful names? Or are we expected to remember what each value means (or implement our own named constants)?

Bassie
  • 9,529
  • 8
  • 68
  • 159

1 Answers1

3

These are the standard values for unix filesystem permissions.

The three digits correspond to:

  • owner
  • group
  • other (aka: everyone)

The individual values are made up of the following bit ORed together:

  • 1: execute
  • 2: write
  • 4: read

In the 0600 example, we have:

  • 0: means octal representation
  • 6: write | read for user
  • 0: nothing for group
  • 0: nothing for other

More on wikipedia

Marc
  • 19,394
  • 6
  • 47
  • 51
  • Thanks for the info, although it doesn't really answer my question of whether or not these named values exist in go, but it seems that from the dupe question the answer is "no" – Bassie May 08 '20 at 15:47
  • 1
    I'm not sure whether they are enumerated in the code or not, they technically don't need to be, just passed through to syscalls. – Marc May 08 '20 at 15:50
  • 1
    You'd need 3^7 constants = 2187 constants to describe all values (not counting special modes). I can't imagine that many constants would be easier than just using a uint32. – Jonathan Hall May 08 '20 at 15:50
  • 1
    I don't think he was asking about all possible combinations, just the individual bits. – Marc May 08 '20 at 15:50
  • @Marc: So your theory is that he wants to use constants + bitwise operations? That seems even _more_ difficult. – Jonathan Hall May 08 '20 at 15:51
  • @Flimzy I just want to be able to write something like `perms.ReadWrite` instead of `0600` as that is clearly more readable. Is that so crazy? I assume making my own constant will not be a problem! – Bassie May 08 '20 at 15:52
  • 1
    @Bassie: `ReadWrite` is not the same as `0600`. `0600` would be more like `UserReadWriteGroupNoneOthersNone` – Jonathan Hall May 08 '20 at 15:53
  • @Flimzy I see - so there are too many different types of readwrite/permission types to have a reasonably sized enum containing them all ? – Bassie May 08 '20 at 15:54
  • 1
    @Bassie: As I just explained above, there are 2187 _regular_ modes (plus some special ones). So yes, there are far too many to have a reasonably sized enum. – Jonathan Hall May 08 '20 at 15:54
  • @Flimzy Understood, that makes sense thank you! – Bassie May 08 '20 at 15:55
  • 1
    You could certainly write a little helper to generate a human-readable string. But the common way to do this is not `ReadWriteExecuteUser`, it's the `rwx` version of the permissions. eg: yours would be: `-rw-------` – Marc May 08 '20 at 15:55
  • 1
    @Flimzy you can or them... `UserReadWrite | UserExecute | GroupRead | OtherRead` for `0744` would be tolerable and would only require around a dozen constants to cover the bottom nine bits :) – hobbs May 12 '20 at 23:15