fileutil.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package fileutil
  15. import (
  16. "fmt"
  17. "io"
  18. "io/ioutil"
  19. "os"
  20. "path/filepath"
  21. "github.com/coreos/pkg/capnslog"
  22. )
  23. const (
  24. // PrivateFileMode grants owner to read/write a file.
  25. PrivateFileMode = 0600
  26. // PrivateDirMode grants owner to make/remove files inside the directory.
  27. PrivateDirMode = 0700
  28. )
  29. var plog = capnslog.NewPackageLogger("go.etcd.io/etcd", "pkg/fileutil")
  30. // IsDirWriteable checks if dir is writable by writing and removing a file
  31. // to dir. It returns nil if dir is writable.
  32. func IsDirWriteable(dir string) error {
  33. f := filepath.Join(dir, ".touch")
  34. if err := ioutil.WriteFile(f, []byte(""), PrivateFileMode); err != nil {
  35. return err
  36. }
  37. return os.Remove(f)
  38. }
  39. // TouchDirAll is similar to os.MkdirAll. It creates directories with 0700 permission if any directory
  40. // does not exists. TouchDirAll also ensures the given directory is writable.
  41. func TouchDirAll(dir string) error {
  42. // If path is already a directory, MkdirAll does nothing
  43. // and returns nil.
  44. err := os.MkdirAll(dir, PrivateDirMode)
  45. if err != nil {
  46. // if mkdirAll("a/text") and "text" is not
  47. // a directory, this will return syscall.ENOTDIR
  48. return err
  49. }
  50. return IsDirWriteable(dir)
  51. }
  52. // CreateDirAll is similar to TouchDirAll but returns error
  53. // if the deepest directory was not empty.
  54. func CreateDirAll(dir string) error {
  55. err := TouchDirAll(dir)
  56. if err == nil {
  57. var ns []string
  58. ns, err = ReadDir(dir)
  59. if err != nil {
  60. return err
  61. }
  62. if len(ns) != 0 {
  63. err = fmt.Errorf("expected %q to be empty, got %q", dir, ns)
  64. }
  65. }
  66. return err
  67. }
  68. // Exist returns true if a file or directory exists.
  69. func Exist(name string) bool {
  70. _, err := os.Stat(name)
  71. return err == nil
  72. }
  73. // ZeroToEnd zeros a file starting from SEEK_CUR to its SEEK_END. May temporarily
  74. // shorten the length of the file.
  75. func ZeroToEnd(f *os.File) error {
  76. // TODO: support FALLOC_FL_ZERO_RANGE
  77. off, err := f.Seek(0, io.SeekCurrent)
  78. if err != nil {
  79. return err
  80. }
  81. lenf, lerr := f.Seek(0, io.SeekEnd)
  82. if lerr != nil {
  83. return lerr
  84. }
  85. if err = f.Truncate(off); err != nil {
  86. return err
  87. }
  88. // make sure blocks remain allocated
  89. if err = Preallocate(f, lenf, true); err != nil {
  90. return err
  91. }
  92. _, err = f.Seek(off, io.SeekStart)
  93. return err
  94. }