diff options
| author | Tamás Gulácsi <tgulacsi78+waterhouse@gmail.com> | 2013-01-14 21:42:35 +0100 |
|---|---|---|
| committer | Tamás Gulácsi <tgulacsi78+waterhouse@gmail.com> | 2013-01-14 21:42:35 +0100 |
| commit | f262fed19784ad85d7cfef985f3dfcc09bd7180c (patch) | |
| tree | 9296344b297bfcb50bd3e6b0721cbdd204231c22 /weed-fs/src/pkg/util | |
| parent | dd685fdd8d8ac6d28dce0d25b72115e3315a30a8 (diff) | |
| download | seaweedfs-f262fed19784ad85d7cfef985f3dfcc09bd7180c.tar.xz seaweedfs-f262fed19784ad85d7cfef985f3dfcc09bd7180c.zip | |
add "freeze" subcommand to volume
Diffstat (limited to 'weed-fs/src/pkg/util')
| -rw-r--r-- | weed-fs/src/pkg/util/file.go | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/weed-fs/src/pkg/util/file.go b/weed-fs/src/pkg/util/file.go new file mode 100644 index 000000000..bf3ea66de --- /dev/null +++ b/weed-fs/src/pkg/util/file.go @@ -0,0 +1,63 @@ +package util + +import ( + "errors" + "log" + "os" +) + +// sets file (fh if not nil, otherwise fileName) permission to mask +// it will +// AND with the permission iff direction < 0 +// OR with the permission iff direction > 0 +// otherwise it will SET the permission to the mask +func SetFilePerm(fh *os.File, fileName string, mask os.FileMode, direction int8) (err error) { + var stat os.FileInfo + if fh == nil { + stat, err = os.Stat(fileName) + } else { + stat, err = fh.Stat() + } + if err != nil { + return err + } + + mode := stat.Mode() & ^os.ModePerm + // log.Printf("mode1=%d mask=%d", mode, mask) + if direction == 0 { + mode |= mask + } else if direction > 0 { + mode |= stat.Mode().Perm() | mask + } else { + mode |= stat.Mode().Perm() & mask + } + log.Printf("pmode=%d operm=%d => nmode=%d nperm=%d", + stat.Mode(), stat.Mode()&os.ModePerm, + mode, mode&os.ModePerm) + if mode == 0 { + return errors.New("Zero FileMode") + } + if fh == nil { + err = os.Chmod(fileName, mode) + } else { + err = fh.Chmod(mode) + } + return err +} + +// returns whether the filename exists - errors doesn't mean not exists! +func FileExists(fileName string) bool { + if _, e := os.Stat(fileName); e != nil && os.IsNotExist(e) { + return false + } + return true +} + +// returns whether the filename is POSSIBLY writable +//- whether it has some kind of writable bit set +func FileIsWritable(fileName string) bool { + if stat, e := os.Stat(fileName); e == nil { + return stat.Mode().Perm()&0222 > 0 + } + return false +} |
