fileutil.go 3.1 KB

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