| 1 |
// Copyright (c) 2014 The fileutil Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// Package fileutil collects some file utility functions. |
| 6 |
package fileutil // import "modernc.org/fileutil" |
| 7 |
|
| 8 |
import ( |
| 9 |
"fmt" |
| 10 |
"io" |
| 11 |
"os" |
| 12 |
"path/filepath" |
| 13 |
"runtime" |
| 14 |
"strconv" |
| 15 |
"sync" |
| 16 |
"time" |
| 17 |
) |
| 18 |
|
| 19 |
// GoMFile is a concurrent access safe version of MFile. |
| 20 |
type GoMFile struct { |
| 21 |
mfile *MFile |
| 22 |
mutex sync.Mutex |
| 23 |
} |
| 24 |
|
| 25 |
// NewGoMFile return a newly created GoMFile. |
| 26 |
func NewGoMFile(fname string, flag int, perm os.FileMode, delta_ns int64) (m *GoMFile, err error) { |
| 27 |
m = &GoMFile{} |
| 28 |
if m.mfile, err = NewMFile(fname, flag, perm, delta_ns); err != nil { |
| 29 |
m = nil |
| 30 |
} |
| 31 |
return |
| 32 |
} |
| 33 |
|
| 34 |
func (m *GoMFile) File() (file *os.File, err error) { |
| 35 |
m.mutex.Lock() |
| 36 |
defer m.mutex.Unlock() |
| 37 |
return m.mfile.File() |
| 38 |
} |
| 39 |
|
| 40 |
func (m *GoMFile) SetChanged() { |
| 41 |
m.mutex.Lock() |
| 42 |
defer m.mutex.Unlock() |
| 43 |
m.mfile.SetChanged() |
| 44 |
} |
| 45 |
|
| 46 |
func (m *GoMFile) SetHandler(h MFileHandler) { |
| 47 |
m.mutex.Lock() |
| 48 |
defer m.mutex.Unlock() |
| 49 |
m.mfile.SetHandler(h) |
| 50 |
} |
| 51 |
|
| 52 |
// MFileHandler resolves modifications of File. |
| 53 |
// Possible File context is expected to be a part of the handler's closure. |
| 54 |
type MFileHandler func(*os.File) error |
| 55 |
|
| 56 |
// MFile represents an os.File with a guard/handler on change/modification. |
| 57 |
// Example use case is an app with a configuration file which can be modified at any time |
| 58 |
// and have to be reloaded in such event prior to performing something configurable by that |
| 59 |
// file. The checks are made only on access to the MFile file by |
| 60 |
// File() and a time threshold/hysteresis value can be chosen on creating a new MFile. |
| 61 |
type MFile struct { |
| 62 |
file *os.File |
| 63 |
handler MFileHandler |
| 64 |
t0 int64 |
| 65 |
delta int64 |
| 66 |
ctime int64 |
| 67 |
} |
| 68 |
|
| 69 |
// NewMFile returns a newly created MFile or Error if any. |
| 70 |
// The fname, flag and perm parameters have the same meaning as in os.Open. |
| 71 |
// For meaning of the delta_ns parameter please see the (m *MFile) File() docs. |
| 72 |
func NewMFile(fname string, flag int, perm os.FileMode, delta_ns int64) (m *MFile, err error) { |
| 73 |
m = &MFile{} |
| 74 |
m.t0 = time.Now().UnixNano() |
| 75 |
if m.file, err = os.OpenFile(fname, flag, perm); err != nil { |
| 76 |
return |
| 77 |
} |
| 78 |
|
| 79 |
var fi os.FileInfo |
| 80 |
if fi, err = m.file.Stat(); err != nil { |
| 81 |
return |
| 82 |
} |
| 83 |
|
| 84 |
m.ctime = fi.ModTime().UnixNano() |
| 85 |
m.delta = delta_ns |
| 86 |
runtime.SetFinalizer(m, func(m *MFile) { |
| 87 |
m.file.Close() |
| 88 |
}) |
| 89 |
return |
| 90 |
} |
| 91 |
|
| 92 |
// SetChanged forces next File() to unconditionally handle modification of the wrapped os.File. |
| 93 |
func (m *MFile) SetChanged() { |
| 94 |
m.ctime = -1 |
| 95 |
} |
| 96 |
|
| 97 |
// SetHandler sets a function to be invoked when modification of MFile is to be processed. |
| 98 |
func (m *MFile) SetHandler(h MFileHandler) { |
| 99 |
m.handler = h |
| 100 |
} |
| 101 |
|
| 102 |
// File returns an os.File from MFile. If time elapsed between the last invocation of this function |
| 103 |
// and now is at least delta_ns ns (a parameter of NewMFile) then the file is checked for |
| 104 |
// change/modification. For delta_ns == 0 the modification is checked w/o getting os.Time(). |
| 105 |
// If a change is detected a handler is invoked on the MFile file. |
| 106 |
// Any of these steps can produce an Error. If that happens the function returns nil, Error. |
| 107 |
func (m *MFile) File() (file *os.File, err error) { |
| 108 |
var now int64 |
| 109 |
|
| 110 |
mustCheck := m.delta == 0 |
| 111 |
if !mustCheck { |
| 112 |
now = time.Now().UnixNano() |
| 113 |
mustCheck = now-m.t0 > m.delta |
| 114 |
} |
| 115 |
|
| 116 |
if mustCheck { // check interval reached |
| 117 |
var fi os.FileInfo |
| 118 |
if fi, err = m.file.Stat(); err != nil { |
| 119 |
return |
| 120 |
} |
| 121 |
|
| 122 |
if fi.ModTime().UnixNano() != m.ctime { // modification detected |
| 123 |
if m.handler == nil { |
| 124 |
return nil, fmt.Errorf("no handler set for modified file %q", m.file.Name()) |
| 125 |
} |
| 126 |
if err = m.handler(m.file); err != nil { |
| 127 |
return |
| 128 |
} |
| 129 |
|
| 130 |
m.ctime = fi.ModTime().UnixNano() |
| 131 |
} |
| 132 |
m.t0 = now |
| 133 |
} |
| 134 |
|
| 135 |
return m.file, nil |
| 136 |
} |
| 137 |
|
| 138 |
// Read reads buf from r. It will either fill the full buf or fail. |
| 139 |
// It wraps the functionality of an io.Reader which may return less bytes than requested, |
| 140 |
// but may block if not all data are ready for the io.Reader. |
| 141 |
func Read(r io.Reader, buf []byte) (err error) { |
| 142 |
have := 0 |
| 143 |
remain := len(buf) |
| 144 |
got := 0 |
| 145 |
for remain > 0 { |
| 146 |
if got, err = r.Read(buf[have:]); err != nil { |
| 147 |
return |
| 148 |
} |
| 149 |
|
| 150 |
remain -= got |
| 151 |
have += got |
| 152 |
} |
| 153 |
return |
| 154 |
} |
| 155 |
|
| 156 |
// "os" and/or "syscall" extensions |
| 157 |
|
| 158 |
// FadviseAdvice is used by Fadvise. |
| 159 |
type FadviseAdvice int |
| 160 |
|
| 161 |
// FAdviseAdvice values. |
| 162 |
const ( |
| 163 |
// $ grep FADV /usr/include/bits/fcntl.h |
| 164 |
POSIX_FADV_NORMAL FadviseAdvice = iota // No further special treatment. |
| 165 |
POSIX_FADV_RANDOM // Expect random page references. |
| 166 |
POSIX_FADV_SEQUENTIAL // Expect sequential page references. |
| 167 |
POSIX_FADV_WILLNEED // Will need these pages. |
| 168 |
POSIX_FADV_DONTNEED // Don't need these pages. |
| 169 |
POSIX_FADV_NOREUSE // Data will be accessed once. |
| 170 |
) |
| 171 |
|
| 172 |
// TempFile creates a new temporary file in the directory dir with a name |
| 173 |
// ending with suffix, basename starting with prefix, opens the file for |
| 174 |
// reading and writing, and returns the resulting *os.File. If dir is the |
| 175 |
// empty string, TempFile uses the default directory for temporary files (see |
| 176 |
// os.TempDir). Multiple programs calling TempFile simultaneously will not |
| 177 |
// choose the same file. The caller can use f.Name() to find the pathname of |
| 178 |
// the file. It is the caller's responsibility to remove the file when no |
| 179 |
// longer needed. |
| 180 |
// |
| 181 |
// NOTE: This function differs from ioutil.TempFile. |
| 182 |
func TempFile(dir, prefix, suffix string) (f *os.File, err error) { |
| 183 |
if dir == "" { |
| 184 |
dir = os.TempDir() |
| 185 |
} |
| 186 |
|
| 187 |
nconflict := 0 |
| 188 |
for i := 0; i < 10000; i++ { |
| 189 |
name := filepath.Join(dir, prefix+nextInfix()+suffix) |
| 190 |
f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600) |
| 191 |
if os.IsExist(err) { |
| 192 |
if nconflict++; nconflict > 10 { |
| 193 |
randmu.Lock() |
| 194 |
rand = reseed() |
| 195 |
randmu.Unlock() |
| 196 |
} |
| 197 |
continue |
| 198 |
} |
| 199 |
break |
| 200 |
} |
| 201 |
return |
| 202 |
} |
| 203 |
|
| 204 |
// Random number state. |
| 205 |
// We generate random temporary file names so that there's a good |
| 206 |
// chance the file doesn't exist yet - keeps the number of tries in |
| 207 |
// TempFile to a minimum. |
| 208 |
var rand uint32 |
| 209 |
var randmu sync.Mutex |
| 210 |
|
| 211 |
func reseed() uint32 { |
| 212 |
return uint32(time.Now().UnixNano() + int64(os.Getpid())) |
| 213 |
} |
| 214 |
|
| 215 |
func nextInfix() string { |
| 216 |
randmu.Lock() |
| 217 |
r := rand |
| 218 |
if r == 0 { |
| 219 |
r = reseed() |
| 220 |
} |
| 221 |
r = r*1664525 + 1013904223 // constants from Numerical Recipes |
| 222 |
rand = r |
| 223 |
randmu.Unlock() |
| 224 |
return strconv.Itoa(int(1e9 + r%1e9))[1:] |
| 225 |
} |