title: File permissions from MacOS 12.1 x64 syscall?
body: <p>I'm learning file I/O in MacOS [Monterey] x86-64 assembly. [Assembling with nasm, using Mach-O 64 output.] The example program I've been adapting for Mac from <a href="https://rads.stackoverflow.com/amzn/click/com/1484250753" rel="nofollow noreferrer" rel="nofollow noreferrer">this textbook</a> is producing strange output. The root of the problem seems to be that this function fragment, in the source file &quot;file.asm&quot;:</p>
  <pre><code>global createFile
  createFile:                       ; fileName testfile.txt already in rdi
      mov     rax,      0x02000005
      mov     rsi,      0x0202
      mov     rdx,      0777
      syscall
      
  </code></pre>
  <p>can create the expected file &quot;testfile.txt&quot;, and permit the first expected Hello World line to be written, <em>but</em>, unlike the rest of the program expects, <strong>it cannot give me or any subsequent functions write permissions to &quot;testfile.txt&quot;.</strong></p>
  <p>If I run this program as myself, and then try to edit the file it created [testfile.txt], I will get a Permission denied and be told the file is read-only. Likewise, later functions in &quot;file.asm&quot; that are supposed to be able to write to &quot;testfile.txt&quot; will fail, although they don't throw any errors.</p>
  <p>But, if I run the program once as myself, and then a <em>second</em> time as sudo, then and only then will a subsequent function which is supposed to be able to overwrite &quot;testfile.txt&quot;, succeed.</p>
  <p>I've tried appending</p>
  <pre><code>    mov     rdi,       fileName
      mov     rax,       0x0200000f  ;[BSD/MacOS syscall for chmod]
      mov     rsi,       0777
      syscall
  </code></pre>
  <p>to createFile, but it did nothing. Do I need to be working with entire directories when chmod-ing? Is there any way to give myself, and the rest of &quot;file.asm&quot;, write access to this file right upon creating it?</p>