# Filemode() / stat().mode: How to interpret the resulting UInt64-number

**URL:** <https://discourse.julialang.org/t/filemode-stat-mode-how-to-interpret-the-resulting-uint64-number/76213>\
**Category:** New to Julia\
**Tags:** question, filesystem\
**Created:** [February 11, 2022, 9:17am UTC](https://discourse.julialang.org/t/filemode-stat-mode-how-to-interpret-the-resulting-uint64-number/76213 "2022-02-11T09:17:04Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [February 11, 2022, 9:17am UTC](https://discourse.julialang.org/t/filemode-stat-mode-how-to-interpret-the-resulting-uint64-number/76213/1 "2022-02-11T09:17:04Z")

</div>

I wonder how to interpret the output of stat(), one example is the field “mode”.  
Unfortunately, the documentation is not very helpful for me, how can I transfer this in something like “`(-rw-rw-rw-)`”, or even more comfortable? Is there package around that helps?  
Here my script snippet to figure out what is behind the output of “`filemode() / stat().mode`”

```julia
s_fn = raw"C:\tmp\somefile.txt"
if isfile(s_fn)
    println("stat(", s_fn, "):")
    println(stat(s_fn))
else
    error("File: \"", s_fn, "\" does not exist!")
end
mode_of_file = stat(s_fn).mode; # alternative: filemode()
typeof_of_file = typeof(mode_of_file); # UInt64
typeof_converted_to_string = string(mode_of_file)

```

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [February 11, 2022, 9:55am UTC](https://discourse.julialang.org/t/filemode-stat-mode-how-to-interpret-the-resulting-uint64-number/76213/2 "2022-02-11T09:55:25Z")

</div>

Julia has a builtin for that, although it is not exported

julia\> Base.Filesystem.filemode\_string(filemode(“error.png”))  
“-rw-rw-rw-”

It’s in here if you want the code

[https://github.com/JuliaLang/julia/blob/master/base/stat.jl](https://github.com/JuliaLang/julia/blob/master/base/stat.jl)

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [February 11, 2022, 10:08am UTC](https://discourse.julialang.org/t/filemode-stat-mode-how-to-interpret-the-resulting-uint64-number/76213/3 "2022-02-11T10:08:43Z")

</div>

@lawless-m: Thanks, it works! So also for the other fields, it is useful to read the source code of “`stat.jl`”

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [February 11, 2022, 12:27pm UTC](https://discourse.julialang.org/t/filemode-stat-mode-how-to-interpret-the-resulting-uint64-number/76213/4 "2022-02-11T12:27:10Z")

</div>

And how do I interpret the bitfield which is the output of “`uperm()`”?  
I have read the documentation of the package “`BitsFields`” but I still do not understand,  
if this package is useful to read the UInt8-bitfield of “`uperm()`”

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [February 11, 2022, 1:36pm UTC](https://discourse.julialang.org/t/filemode-stat-mode-how-to-interpret-the-resulting-uint64-number/76213/5 "2022-02-11T13:36:24Z")

</div>

`StatStruct`s (returned by [`stat`](https://docs.julialang.org/en/v1/base/file/#Base.stat)) have nice display methods since Julia 1.7:

```julia
julia> stat("Project.toml")
StatStruct for "Project.toml"
   size: 504 bytes
 device: 66310
  inode: 11668897
   mode: 0o100664 (-rw-rw-r--)
  nlink: 1
    uid: 1000 (fredrik)
    gid: 1000 (fredrik)
   rdev: 0
  blksz: 4096
 blocks: 8
  mtime: 2022-02-01T17:39:35+0100 (9 days ago)
  ctime: 2022-02-01T17:39:35+0100 (9 days ago)

```

(for which the internal function linked to above is used).

The `.mode` field of the struct is stored as a `UInt`, but if you format it in base 8 it perhaps look more familiar:

```julia
julia> stat("Project.toml").mode
0x00000000000081b4

julia> string(stat("Project.toml").mode; base = 8)
"100664"

```

So 664, in base 8, means `-rw-rw-r--`, see e.g. [wikipedia.org/File-system\_permissions#Numeric\_notation](https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation). (I am not sure what the first `1` mean here though?)

> [@ellocco](#):
>
> And how do I interpret the bitfield which is the output of “ `uperm()` ”?

Same thing here, if you format it as bits (base 2) you get

```julia
julia> uperm("Project.toml")
0x06

julia> bitstring(uperm("Project.toml"))
"00000110"

```

so my permissions on this file is `110`, i.e. read and write, but not execute.

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [February 11, 2022, 4:38pm UTC](https://discourse.julialang.org/t/filemode-stat-mode-how-to-interpret-the-resulting-uint64-number/76213/6 "2022-02-11T16:38:13Z")

</div>

> [@fredrikekre](#):
>
> so my permissions on this file is `110` , i.e. read and write, but not execute.

@fredrikekre: Thank you!

The documentation about the bitfield of `uperm()` is strange to me, for me it should look like:

```julia
| Value | Description |
|:------|:-------------------|
| 06 | Read Permission |
| 07 | Write Permission |
| 08 | Execute Permission |

```

or the other way round:

```julia
| Value | Description |
|:------|:-------------------|
| 03 | Read Permission |
| 02 | Write Permission |
| 01 | Execute Permission |

```

Is `| 0`_4_` | Read Permission |` perhaps a typo?

Here a code snippet to clarify the situation for me:

```julia
s_bitstring = bitstring(uperm("Project.toml"))
if s_bitstring[8] == '1'
    println("File: \"Project.toml\" is executable!")
end
if s_bitstring[7] == '1'
    println("File: \"Project.toml\" has write permission.")
end
if s_bitstring[6] == '1'
    println("File: \"Project.toml\" has read permission.")
end

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [February 11, 2022, 7:19pm UTC](https://discourse.julialang.org/t/filemode-stat-mode-how-to-interpret-the-resulting-uint64-number/76213/7 "2022-02-11T19:19:11Z")

</div>

`1`, `2`, and `4` are the integer values (base 10) of the bit patterns (i.e. base 2) `001`, `010`, and `100`, respectively:

```julia
julia> parse(Int, "001"; base=2)
1

julia> parse(Int, "010"; base=2)
2

julia> parse(Int, "100"; base=2)
4

```

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [February 11, 2022, 11:12pm UTC](https://discourse.julialang.org/t/filemode-stat-mode-how-to-interpret-the-resulting-uint64-number/76213/8 "2022-02-11T23:12:39Z")

</div>

@fredrikekre: thanks a lot! I hope this topic will help also others to understand the meaning of this part of the Julia documentation 🙂

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [February 14, 2022, 7:22am UTC](https://discourse.julialang.org/t/filemode-stat-mode-how-to-interpret-the-resulting-uint64-number/76213/9 "2022-02-14T07:22:13Z")

</div>

I would document it this way to make the concept clear:

```julia
| BitMask | Int | Description |
|:---------|:----|:-------------------|
|'00000100'| 4 | Read Permission |
|'00000010'| 2 | Write Permission |
|'00000001'| 1 | Execute Permission |

```

Meanwhile I found the function to calculate the BitVector:

```julia
upermBitVec = BitVector(digits(uperm("Project.toml"), base=2, pad=8))

```

Opposite Bit Order (from right → left):

```julia
upermBitVecRight2Left = BitVector(digits(uperm("Project.toml"), base=2, pad=8) |> reverse )

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [February 14, 2022, 10:05am UTC](https://discourse.julialang.org/t/filemode-stat-mode-how-to-interpret-the-resulting-uint64-number/76213/10 "2022-02-14T10:05:10Z")

</div>

That could be a good improvement to the docs, feel free to contribute it. The docstring is here: [https://github.com/JuliaLang/julia/blob/7ca1a9047aaaa6a9a31e5025f4728fc1d86a5cf8/base/stat.jl#L406-L417](https://github.com/JuliaLang/julia/blob/7ca1a9047aaaa6a9a31e5025f4728fc1d86a5cf8/base/stat.jl#L406-L417).

---

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [February 14, 2022, 12:41pm UTC](https://discourse.julialang.org/t/filemode-stat-mode-how-to-interpret-the-resulting-uint64-number/76213/11 "2022-02-14T12:41:19Z")

</div>

In another project, it was quite complicated for me to reach the point to suggest modifications,  
I hope it is more easy for Julia. I will have a look. 🙂

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [February 14, 2022, 3:44pm UTC](https://discourse.julialang.org/t/filemode-stat-mode-how-to-interpret-the-resulting-uint64-number/76213/12 "2022-02-14T15:44:26Z")

</div>

[https://github.com/JuliaLang/julia/blob/f7b2c3d646e6fbf2ecba9f944a1848a7b632cb05/CONTRIBUTING.md#improving-documentation](https://github.com/JuliaLang/julia/blob/f7b2c3d646e6fbf2ecba9f944a1848a7b632cb05/CONTRIBUTING.md#improving-documentation)
