How can I set the immutable attribute for a file with Go?
I need a function that set/unset this attribute on a file, similar to chattr +i file
I found this: IoctlSetInt
how to use it?
file, err := os.Open("file")
if err != nil {
fmt.Printf("%s\n", err)
return
}
err = unix.IoctlSetInt(int(file.Fd()), unix.PERF_EVENT_IOC_MODIFY_ATTRIBUTES, unix.STATX_ATTR_IMMUTABLE)
if err != nil {
fmt.Printf("%s\n", err)
}
error: inappropriate ioctl for device
SOLUTION
I found a pice of code in snapd's repo.
I'm not sure you're using the right ioctl here... At least chattr and the solution of Setting Immutable Flag using ioctl() in C use FS_IOC_SETFLAGS
(you can see what chattr does using strace
). You can have a look at /usr/include/linux/fs.h
, notably the big comment about "Inode flags", for more information.