1 // A UnixVolume is a Volume backed by a locally mounted disk.
19 // A UnixVolume stores and retrieves blocks in a local directory.
20 type UnixVolume struct {
21 root string // path to the volume's root directory
27 func (v *UnixVolume) Touch(loc string) error {
29 return MethodDisabledError
32 f, err := os.OpenFile(p, os.O_RDWR|os.O_APPEND, 0644)
39 defer v.mutex.Unlock()
41 if e := lockfile(f); e != nil {
45 now := time.Now().Unix()
46 utime := syscall.Utimbuf{now, now}
47 return syscall.Utime(p, &utime)
50 func (v *UnixVolume) Mtime(loc string) (time.Time, error) {
52 if fi, err := os.Stat(p); err != nil {
53 return time.Time{}, err
55 return fi.ModTime(), nil
59 // Get retrieves a block identified by the locator string "loc", and
60 // returns its contents as a byte slice.
62 // If the block could not be found, opened, or read, Get returns a nil
63 // slice and whatever non-nil error was returned by Stat or ReadFile.
64 func (v *UnixVolume) Get(loc string) ([]byte, error) {
65 path := v.blockPath(loc)
66 if _, err := os.Stat(path); err != nil {
71 defer v.mutex.Unlock()
73 buf, err := ioutil.ReadFile(path)
77 // Put stores a block of data identified by the locator string
78 // "loc". It returns nil on success. If the volume is full, it
79 // returns a FullError. If the write fails due to some other error,
80 // that error is returned.
81 func (v *UnixVolume) Put(loc string, block []byte) error {
83 return MethodDisabledError
88 bdir := v.blockDir(loc)
89 if err := os.MkdirAll(bdir, 0755); err != nil {
90 log.Printf("%s: could not create directory %s: %s",
95 tmpfile, tmperr := ioutil.TempFile(bdir, "tmp"+loc)
97 log.Printf("ioutil.TempFile(%s, tmp%s): %s", bdir, loc, tmperr)
100 bpath := v.blockPath(loc)
104 defer v.mutex.Unlock()
106 if _, err := tmpfile.Write(block); err != nil {
107 log.Printf("%s: writing to %s: %s\n", v, bpath, err)
109 os.Remove(tmpfile.Name())
112 if err := tmpfile.Close(); err != nil {
113 log.Printf("closing %s: %s\n", tmpfile.Name(), err)
114 os.Remove(tmpfile.Name())
117 if err := os.Rename(tmpfile.Name(), bpath); err != nil {
118 log.Printf("rename %s %s: %s\n", tmpfile.Name(), bpath, err)
119 os.Remove(tmpfile.Name())
125 // Status returns a VolumeStatus struct describing the volume's
128 func (v *UnixVolume) Status() *VolumeStatus {
129 var fs syscall.Statfs_t
132 if fi, err := os.Stat(v.root); err == nil {
133 devnum = fi.Sys().(*syscall.Stat_t).Dev
135 log.Printf("%s: os.Stat: %s\n", v, err)
139 err := syscall.Statfs(v.root, &fs)
141 log.Printf("%s: statfs: %s\n", v, err)
144 // These calculations match the way df calculates disk usage:
145 // "free" space is measured by fs.Bavail, but "used" space
146 // uses fs.Blocks - fs.Bfree.
147 free := fs.Bavail * uint64(fs.Bsize)
148 used := (fs.Blocks - fs.Bfree) * uint64(fs.Bsize)
149 return &VolumeStatus{v.root, devnum, free, used}
152 // IndexTo writes (to the given Writer) a list of blocks found on this
153 // volume which begin with the specified prefix. If the prefix is an
154 // empty string, IndexTo writes a complete list of blocks.
156 // Each block is given in the format
158 // locator+size modification-time {newline}
162 // e4df392f86be161ca6ed3773a962b8f3+67108864 1388894303
163 // e4d41e6fd68460e0e3fc18cc746959d2+67108864 1377796043
164 // e4de7a2810f5554cd39b36d8ddb132ff+67108864 1388701136
166 func (v *UnixVolume) IndexTo(prefix string, w io.Writer) error {
167 return filepath.Walk(v.root,
168 func(path string, info os.FileInfo, err error) error {
170 log.Printf("%s: IndexTo Walk error at %s: %s",
174 basename := filepath.Base(path)
176 !strings.HasPrefix(basename, prefix) &&
177 !strings.HasPrefix(prefix, basename) {
178 // Skip directories that do not match
179 // prefix. We know there is nothing
180 // interesting inside.
181 return filepath.SkipDir
184 !IsValidLocator(basename) ||
185 !strings.HasPrefix(basename, prefix) {
188 _, err = fmt.Fprintf(w, "%s+%d %d\n",
189 basename, info.Size(), info.ModTime().Unix())
194 func (v *UnixVolume) Delete(loc string) error {
195 // Touch() must be called before calling Write() on a block. Touch()
196 // also uses lockfile(). This avoids a race condition between Write()
197 // and Delete() because either (a) the file will be deleted and Touch()
198 // will signal to the caller that the file is not present (and needs to
199 // be re-written), or (b) Touch() will update the file's timestamp and
200 // Delete() will read the correct up-to-date timestamp and choose not to
204 return MethodDisabledError
208 defer v.mutex.Unlock()
210 p := v.blockPath(loc)
211 f, err := os.OpenFile(p, os.O_RDWR|os.O_APPEND, 0644)
216 if e := lockfile(f); e != nil {
221 // If the block has been PUT in the last blob_signature_ttl
222 // seconds, return success without removing the block. This
223 // protects data from garbage collection until it is no longer
224 // possible for clients to retrieve the unreferenced blocks
225 // anyway (because the permission signatures have expired).
226 if fi, err := os.Stat(p); err != nil {
229 if time.Since(fi.ModTime()) < blob_signature_ttl {
236 // blockDir returns the fully qualified directory name for the directory
237 // where loc is (or would be) stored on this volume.
238 func (v *UnixVolume) blockDir(loc string) string {
239 return filepath.Join(v.root, loc[0:3])
242 // blockPath returns the fully qualified pathname for the path to loc
244 func (v *UnixVolume) blockPath(loc string) string {
245 return filepath.Join(v.blockDir(loc), loc)
248 // IsFull returns true if the free space on the volume is less than
249 // MIN_FREE_KILOBYTES.
251 func (v *UnixVolume) IsFull() (isFull bool) {
252 fullSymlink := v.root + "/full"
254 // Check if the volume has been marked as full in the last hour.
255 if link, err := os.Readlink(fullSymlink); err == nil {
256 if ts, err := strconv.Atoi(link); err == nil {
257 fulltime := time.Unix(int64(ts), 0)
258 if time.Since(fulltime).Hours() < 1.0 {
264 if avail, err := v.FreeDiskSpace(); err == nil {
265 isFull = avail < MIN_FREE_KILOBYTES
267 log.Printf("%s: FreeDiskSpace: %s\n", v, err)
271 // If the volume is full, timestamp it.
273 now := fmt.Sprintf("%d", time.Now().Unix())
274 os.Symlink(now, fullSymlink)
279 // FreeDiskSpace returns the number of unused 1k blocks available on
282 func (v *UnixVolume) FreeDiskSpace() (free uint64, err error) {
283 var fs syscall.Statfs_t
284 err = syscall.Statfs(v.root, &fs)
286 // Statfs output is not guaranteed to measure free
287 // space in terms of 1K blocks.
288 free = fs.Bavail * uint64(fs.Bsize) / 1024
293 func (v *UnixVolume) String() string {
294 return fmt.Sprintf("[UnixVolume %s]", v.root)
297 func (v *UnixVolume) Writable() bool {
301 // lockfile and unlockfile use flock(2) to manage kernel file locks.
302 func lockfile(f *os.File) error {
303 return syscall.Flock(int(f.Fd()), syscall.LOCK_EX)
306 func unlockfile(f *os.File) error {
307 return syscall.Flock(int(f.Fd()), syscall.LOCK_UN)