mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
pkg/fileutil: expose PrivateFileMode/DirMode
This commit is contained in:
parent
4570eddc2c
commit
47d5257622
@ -25,9 +25,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
privateFileMode = 0600
|
// PrivateFileMode grants owner to read/write a file.
|
||||||
// owner can make/remove files inside the directory
|
PrivateFileMode = 0600
|
||||||
privateDirMode = 0700
|
// PrivateDirMode grants owner to make/remove files inside the directory.
|
||||||
|
PrivateDirMode = 0700
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -38,7 +39,7 @@ var (
|
|||||||
// to dir. It returns nil if dir is writable.
|
// to dir. It returns nil if dir is writable.
|
||||||
func IsDirWriteable(dir string) error {
|
func IsDirWriteable(dir string) error {
|
||||||
f := path.Join(dir, ".touch")
|
f := path.Join(dir, ".touch")
|
||||||
if err := ioutil.WriteFile(f, []byte(""), privateFileMode); err != nil {
|
if err := ioutil.WriteFile(f, []byte(""), PrivateFileMode); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return os.Remove(f)
|
return os.Remove(f)
|
||||||
@ -62,7 +63,7 @@ func ReadDir(dirpath string) ([]string, error) {
|
|||||||
// TouchDirAll is similar to os.MkdirAll. It creates directories with 0700 permission if any directory
|
// TouchDirAll is similar to os.MkdirAll. It creates directories with 0700 permission if any directory
|
||||||
// does not exists. TouchDirAll also ensures the given directory is writable.
|
// does not exists. TouchDirAll also ensures the given directory is writable.
|
||||||
func TouchDirAll(dir string) error {
|
func TouchDirAll(dir string) error {
|
||||||
err := os.MkdirAll(dir, privateDirMode)
|
err := os.MkdirAll(dir, PrivateDirMode)
|
||||||
if err != nil && err != os.ErrExist {
|
if err != nil && err != os.ErrExist {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
|
func TryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
|
||||||
if err := os.Chmod(path, syscall.DMEXCL|0600); err != nil {
|
if err := os.Chmod(path, syscall.DMEXCL|PrivateFileMode); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
f, err := os.Open(path, flag, perm)
|
f, err := os.Open(path, flag, perm)
|
||||||
@ -32,7 +32,7 @@ func TryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func LockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
|
func LockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
|
||||||
if err := os.Chmod(path, syscall.DMEXCL|0600); err != nil {
|
if err := os.Chmod(path, syscall.DMEXCL|PrivateFileMode); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
|
@ -35,13 +35,13 @@ func TestLockAndUnlock(t *testing.T) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// lock the file
|
// lock the file
|
||||||
l, err := LockFile(f.Name(), os.O_WRONLY, 0600)
|
l, err := LockFile(f.Name(), os.O_WRONLY, PrivateFileMode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// try lock a locked file
|
// try lock a locked file
|
||||||
if _, err = TryLockFile(f.Name(), os.O_WRONLY, 0600); err != ErrLocked {
|
if _, err = TryLockFile(f.Name(), os.O_WRONLY, PrivateFileMode); err != ErrLocked {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,7 +51,7 @@ func TestLockAndUnlock(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// try lock the unlocked file
|
// try lock the unlocked file
|
||||||
dupl, err := TryLockFile(f.Name(), os.O_WRONLY, 0600)
|
dupl, err := TryLockFile(f.Name(), os.O_WRONLY, PrivateFileMode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("err = %v, want %v", err, nil)
|
t.Errorf("err = %v, want %v", err, nil)
|
||||||
}
|
}
|
||||||
@ -59,7 +59,7 @@ func TestLockAndUnlock(t *testing.T) {
|
|||||||
// blocking on locked file
|
// blocking on locked file
|
||||||
locked := make(chan struct{}, 1)
|
locked := make(chan struct{}, 1)
|
||||||
go func() {
|
go func() {
|
||||||
bl, blerr := LockFile(f.Name(), os.O_WRONLY, 0600)
|
bl, blerr := LockFile(f.Name(), os.O_WRONLY, PrivateFileMode)
|
||||||
if blerr != nil {
|
if blerr != nil {
|
||||||
t.Fatal(blerr)
|
t.Fatal(blerr)
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,7 @@ func PurgeFile(dirname string, suffix string, max uint, interval time.Duration,
|
|||||||
sort.Strings(newfnames)
|
sort.Strings(newfnames)
|
||||||
for len(newfnames) > int(max) {
|
for len(newfnames) > int(max) {
|
||||||
f := path.Join(dirname, newfnames[0])
|
f := path.Join(dirname, newfnames[0])
|
||||||
l, err := TryLockFile(f, os.O_WRONLY, 0600)
|
l, err := TryLockFile(f, os.O_WRONLY, PrivateFileMode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -102,7 +102,7 @@ func TestPurgeFileHoldingLockFile(t *testing.T) {
|
|||||||
|
|
||||||
// create a purge barrier at 5
|
// create a purge barrier at 5
|
||||||
p := path.Join(dir, fmt.Sprintf("%d.test", 5))
|
p := path.Join(dir, fmt.Sprintf("%d.test", 5))
|
||||||
l, err := LockFile(p, os.O_WRONLY, 0600)
|
l, err := LockFile(p, os.O_WRONLY, PrivateFileMode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user