fileutil.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 implements utility functions related to files and paths.
  15. package fileutil
  16. import (
  17. "fmt"
  18. "io"
  19. "io/ioutil"
  20. "os"
  21. "path/filepath"
  22. "sort"
  23. "github.com/coreos/pkg/capnslog"
  24. )
  25. const (
  26. // PrivateFileMode grants owner to read/write a file.
  27. PrivateFileMode = 0600
  28. // PrivateDirMode grants owner to make/remove files inside the directory.
  29. PrivateDirMode = 0700
  30. )
  31. var (
  32. plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "pkg/fileutil")
  33. )
  34. // IsDirWriteable checks if dir is writable by writing and removing a file
  35. // to dir. It returns nil if dir is writable.
  36. func IsDirWriteable(dir string) error {
  37. f := filepath.Join(dir, ".touch")
  38. if err := ioutil.WriteFile(f, []byte(""), PrivateFileMode); err != nil {
  39. return err
  40. }
  41. return os.Remove(f)
  42. }
  43. // ReadDir returns the filenames in the given directory in sorted order.
  44. func ReadDir(dirpath string) ([]string, error) {
  45. dir, err := os.Open(dirpath)
  46. if err != nil {
  47. return nil, err
  48. }
  49. defer dir.Close()
  50. names, err := dir.Readdirnames(-1)
  51. if err != nil {
  52. return nil, err
  53. }
  54. sort.Strings(names)
  55. return names, nil
  56. }
  57. // TouchDirAll is similar to os.MkdirAll. It creates directories with 0700 permission if any directory
  58. // does not exists. TouchDirAll also ensures the given directory is writable.
  59. func TouchDirAll(dir string) error {
  60. // If path is already a directory, MkdirAll does nothing
  61. // and returns nil.
  62. err := os.MkdirAll(dir, PrivateDirMode)
  63. if err != nil {
  64. // if mkdirAll("a/text") and "text" is not
  65. // a directory, this will return syscall.ENOTDIR
  66. return err
  67. }
  68. return IsDirWriteable(dir)
  69. }
  70. // CreateDirAll is similar to TouchDirAll but returns error
  71. // if the deepest directory was not empty.
  72. func CreateDirAll(dir string) error {
  73. err := TouchDirAll(dir)
  74. if err == nil {
  75. var ns []string
  76. ns, err = ReadDir(dir)
  77. if err != nil {
  78. return err
  79. }
  80. if len(ns) != 0 {
  81. err = fmt.Errorf("expected %q to be empty, got %q", dir, ns)
  82. }
  83. }
  84. return err
  85. }
  86. func Exist(name string) bool {
  87. _, err := os.Stat(name)
  88. return err == nil
  89. }
  90. // ZeroToEnd zeros a file starting from SEEK_CUR to its SEEK_END. May temporarily
  91. // shorten the length of the file.
  92. func ZeroToEnd(f *os.File) error {
  93. // TODO: support FALLOC_FL_ZERO_RANGE
  94. off, err := f.Seek(0, io.SeekCurrent)
  95. if err != nil {
  96. return err
  97. }
  98. lenf, lerr := f.Seek(0, io.SeekEnd)
  99. if lerr != nil {
  100. return lerr
  101. }
  102. if err = f.Truncate(off); err != nil {
  103. return err
  104. }
  105. // make sure blocks remain allocated
  106. if err = Preallocate(f, lenf, true); err != nil {
  107. return err
  108. }
  109. _, err = f.Seek(off, io.SeekStart)
  110. return err
  111. }