MCPcopy
hub / github.com/tinylib/msgp / WriteFile

Function WriteFile

msgp/file.go:69–90  ·  view source on GitHub ↗

WriteFile writes a file from 'src' using memory mapping. It overwrites the entire contents of the previous file. The mapping size is calculated using the `Msgsize()` method of 'src', so it must produce a result equal to or greater than the actual encoded size of the object. Otherwise, a fault (SIGBU

(src MarshalSizer, file *os.File)

Source from the content-addressed store, hash-verified

67// (Linux users should run a kernel and filesystem
68// that support fallocate(2) for the best results.)
69func WriteFile(src MarshalSizer, file *os.File) error {
70 sz := src.Msgsize()
71 err := fallocate(file, int64(sz))
72 if err != nil {
73 return err
74 }
75 data, err := syscall.Mmap(int(file.Fd()), 0, sz, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
76 if err != nil {
77 return err
78 }
79 adviseWrite(data)
80 chunk := data[:0]
81 chunk, err = src.MarshalMsg(chunk)
82 if err != nil {
83 return err
84 }
85 uerr := syscall.Munmap(data)
86 if uerr != nil {
87 return uerr
88 }
89 return file.Truncate(int64(len(chunk)))
90}

Callers

nothing calls this directly

Calls 4

fallocateFunction · 0.70
adviseWriteFunction · 0.70
MsgsizeMethod · 0.65
MarshalMsgMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…