I think a more “standard” and certainly portable way to do this is with a tmp file, and a rename/move at the end - along with a clean-up of tmp at application start (if worried about crashes).
Using the proper file and process permissions on the tmp file - since you need these to protect the final file anyway. > On Oct 12, 2018, at 12:21 AM, Sam Whited <s...@samwhited.com> wrote: > > On Thu, Oct 11, 2018, at 18:48, Greg Saylor wrote: >> 1. Create a temporary file and squirrel away the file handle >> 2. Unlink the temporary file by name >> 3. Various functions would write stuff to the file >> 4. If the programs completes to some successful state, create a hardlink to >> the file handle with the final filename > > The golang.org/x/sys/unix [1] package gives you a lower level interface that > might be familiar to you if you're used to using C. Something like this is > probably similar to what you were doing (though you'll need /proc mounted, > I'm not sure if there's a better way to do that): > > oldfd, err := unix.Open(".", unix.O_TMPFILE|unix.O_RDWR, > unix.S_IRUSR|unix.S_IWUSR) > if err != nil { > panic(err) > } > > f := os.NewFile(uintptr(oldfd), "myoldfile") > _, err = f.Write([]byte("Hello, World")) > if err != nil { > panic(err) > } > > dir, err := os.Open(".") > if err != nil { > panic(err) > } > procdir, err := os.Open("/proc/self/fd/") > if err != nil { > panic(err) > } > > err = unix.Linkat(int(procdir.Fd()), strconv.Itoa(oldfd), > int(dir.Fd()), "mynewfile", unix.AT_SYMLINK_FOLLOW) > if err != nil { > panic(err) > } > > —Sam > > [1]: https://godoc.org/golang.org/x/sys/unix > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.