aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/syndtr/goleveldb/leveldb/storage/file_storage_plan9.go
blob: bab62bfcee8bc102256ef18675439cbd92158f30 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com>
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package storage

import (
    "os"
    "path/filepath"
)

type plan9FileLock struct {
    f *os.File
}

func (fl *plan9FileLock) release() error {
    return fl.f.Close()
}

func newFileLock(path string, readOnly bool) (fl fileLock, err error) {
    var (
        flag int
        perm os.FileMode
    )
    if readOnly {
        flag = os.O_RDONLY
    } else {
        flag = os.O_RDWR
        perm = os.ModeExclusive
    }
    f, err := os.OpenFile(path, flag, perm)
    if os.IsNotExist(err) {
        f, err = os.OpenFile(path, flag|os.O_CREATE, perm|0644)
    }
    if err != nil {
        return
    }
    fl = &plan9FileLock{f: f}
    return
}

func rename(oldpath, newpath string) error {
    if _, err := os.Stat(newpath); err == nil {
        if err := os.Remove(newpath); err != nil {
            return err
        }
    }

    _, fname := filepath.Split(newpath)
    return os.Rename(oldpath, fname)
}

func syncDir(name string) error {
    f, err := os.Open(name)
    if err != nil {
        return err
    }
    defer f.Close()
    if err := f.Sync(); err != nil {
        return err
    }
    return nil
}