dsfsdf5646 2018-05-09 16:23
浏览 617

ioutils.WriteFile()不尊重权限

I'm trying to use ioutils.WriteFile() but for some reason it's ignoring the 0777 permissions I'm giving it.

package main

import (
    "io/ioutil"
    "os"
)

func main() {

    // normal permissions
    if err := ioutil.WriteFile("cant-touch-this-0644", []byte{}, 0644); err != nil {
            panic(err)
    }


    // full permissions
    if err := ioutil.WriteFile("cant-touch-this-0777", []byte{}, 0777); err != nil {
            panic(err)
    }


    // normal permissions + chmod to full
    if err := ioutil.WriteFile("cant-touch-this-mixed", []byte{}, 0755); err != nil {
            panic(err)
    }

    if err := os.Chmod("cant-touch-this-mixed", 0777); err != nil {
            panic(err)
    }
}

The output I get from this is:

$ ls -l
-rw-r--r--  1 edson edson    0 May  9 17:19  cant-touch-this-0644
-rwxr-xr-x  1 edson edson    0 May  9 17:19  cant-touch-this-0777
-rwxrwxrwx  1 edson edson    0 May  9 17:19  cant-touch-this-mixed

Which means:

  • The first scenario (0644) worked
  • The second (0777) was ignored
  • The only way I can get full 0777 permissions is by using a os.Chmod (like in the third scenario)

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • donglefu6195 2019-03-30 10:07
    关注

    Permission deny.

    Try:

    sudo go run main.go
    

    Or change user to root and then execute.

    How to change user to root:

    sudo su -
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制