I have seen countless examples and tutorials that show how to create a file and all of them "cheat" by just setting the permission bits of the file. I would like to know / find out how to properly instantiate os.FileMode to provide to a writer during creation / updating of a file.
A crude example is this below:
func FileWrite(path string, r io.Reader, uid, gid int, perms string) (int64, error){
w, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0664)
if err != nil {
if path == "" {
w = os.Stdout
} else {
return 0, err
}
}
defer w.Close()
size, err := io.Copy(w, r)
if err != nil {
return 0, err
}
return size, err
}
In the basic function above permission bits 0664 is set and although this may make sense sometimes I prefer to have a proper way of setting the filemode correctly. As seen above a common example would be that the UID / GID is known and already provided as int values and the perms being octal digits that were previously gathered and inserted into a db as a string.