clonetree.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // Use and distribution licensed under the Apache license version 2.
  3. //
  4. // See the COPYING file in the root project directory for full text.
  5. //
  6. package snapshot
  7. import (
  8. "errors"
  9. "io/ioutil"
  10. "os"
  11. "path/filepath"
  12. "strings"
  13. )
  14. // Attempting to tar up pseudofiles like /proc/cpuinfo is an exercise in
  15. // futility. Notably, the pseudofiles, when read by syscalls, do not return the
  16. // number of bytes read. This causes the tar writer to write zero-length files.
  17. //
  18. // Instead, it is necessary to build a directory structure in a tmpdir and
  19. // create actual files with copies of the pseudofile contents
  20. // CloneTreeInto copies all the pseudofiles that ghw will consume into the root
  21. // `scratchDir`, preserving the hieratchy.
  22. func CloneTreeInto(scratchDir string) error {
  23. err := setupScratchDir(scratchDir)
  24. if err != nil {
  25. return err
  26. }
  27. fileSpecs := ExpectedCloneContent()
  28. return CopyFilesInto(fileSpecs, scratchDir, nil)
  29. }
  30. // ExpectedCloneContent return a slice of glob patterns which represent the pseudofiles
  31. // ghw cares about.
  32. // The intended usage of this function is to validate a clone tree, checking that the
  33. // content matches the expectations.
  34. // Beware: the content is host-specific, because the content pertaining some subsystems,
  35. // most notably PCI, is host-specific and unpredictable.
  36. func ExpectedCloneContent() []string {
  37. fileSpecs := ExpectedCloneStaticContent()
  38. fileSpecs = append(fileSpecs, ExpectedCloneNetContent()...)
  39. fileSpecs = append(fileSpecs, ExpectedClonePCIContent()...)
  40. fileSpecs = append(fileSpecs, ExpectedCloneGPUContent()...)
  41. return fileSpecs
  42. }
  43. // ValidateClonedTree checks the content of a cloned tree, whose root is `clonedDir`,
  44. // against a slice of glob specs which must be included in the cloned tree.
  45. // Is not wrong, and this functions doesn't enforce this, that the cloned tree includes
  46. // more files than the necessary; ghw will just ignore the files it doesn't care about.
  47. // Returns a slice of glob patters expected (given) but not found in the cloned tree,
  48. // and the error during the validation (if any).
  49. func ValidateClonedTree(fileSpecs []string, clonedDir string) ([]string, error) {
  50. missing := []string{}
  51. for _, fileSpec := range fileSpecs {
  52. matches, err := filepath.Glob(filepath.Join(clonedDir, fileSpec))
  53. if err != nil {
  54. return missing, err
  55. }
  56. if len(matches) == 0 {
  57. missing = append(missing, fileSpec)
  58. }
  59. }
  60. return missing, nil
  61. }
  62. // CopyFileOptions allows to finetune the behaviour of the CopyFilesInto function
  63. type CopyFileOptions struct {
  64. // IsSymlinkFn allows to control the behaviour when handling a symlink.
  65. // If this hook returns true, the source file is treated as symlink: the cloned
  66. // tree will thus contain a symlink, with its path adjusted to match the relative
  67. // path inside the cloned tree. If return false, the symlink will be deferred.
  68. // The easiest use case of this hook is if you want to avoid symlinks in your cloned
  69. // tree (having duplicated content). In this case you can just add a function
  70. // which always return false.
  71. IsSymlinkFn func(path string, info os.FileInfo) bool
  72. // ShouldCreateDirFn allows to control if empty directories listed as clone
  73. // content should be created or not. When creating snapshots, empty directories
  74. // are most often useless (but also harmless). Because of this, directories are only
  75. // created as side effect of copying the files which are inside, and thus directories
  76. // are never empty. The only notable exception are device driver on linux: in this
  77. // case, for a number of technical/historical reasons, we care about the directory
  78. // name, but not about the files which are inside.
  79. // Hence, this is the only case on which ghw clones empty directories.
  80. ShouldCreateDirFn func(path string, info os.FileInfo) bool
  81. }
  82. // CopyFilesInto copies all the given glob files specs in the given `destDir` directory,
  83. // preserving the directory structure. This means you can provide a deeply nested filespec
  84. // like
  85. // - /some/deeply/nested/file*
  86. // and you DO NOT need to build the tree incrementally like
  87. // - /some/
  88. // - /some/deeply/
  89. // ...
  90. // all glob patterns supported in `filepath.Glob` are supported.
  91. func CopyFilesInto(fileSpecs []string, destDir string, opts *CopyFileOptions) error {
  92. if opts == nil {
  93. opts = &CopyFileOptions{
  94. IsSymlinkFn: isSymlink,
  95. ShouldCreateDirFn: isDriversDir,
  96. }
  97. }
  98. for _, fileSpec := range fileSpecs {
  99. trace("copying spec: %q\n", fileSpec)
  100. matches, err := filepath.Glob(fileSpec)
  101. if err != nil {
  102. return err
  103. }
  104. if err := copyFileTreeInto(matches, destDir, opts); err != nil {
  105. return err
  106. }
  107. }
  108. return nil
  109. }
  110. func copyFileTreeInto(paths []string, destDir string, opts *CopyFileOptions) error {
  111. for _, path := range paths {
  112. trace(" copying path: %q\n", path)
  113. baseDir := filepath.Dir(path)
  114. if err := os.MkdirAll(filepath.Join(destDir, baseDir), os.ModePerm); err != nil {
  115. return err
  116. }
  117. fi, err := os.Lstat(path)
  118. if err != nil {
  119. return err
  120. }
  121. // directories must be listed explicitly and created separately.
  122. // In the future we may want to expose this decision as hook point in
  123. // CopyFileOptions, when clear use cases emerge.
  124. destPath := filepath.Join(destDir, path)
  125. if fi.IsDir() {
  126. if opts.ShouldCreateDirFn(path, fi) {
  127. if err := os.MkdirAll(destPath, os.ModePerm); err != nil {
  128. return err
  129. }
  130. } else {
  131. trace("expanded glob path %q is a directory - skipped\n", path)
  132. }
  133. continue
  134. }
  135. if opts.IsSymlinkFn(path, fi) {
  136. trace(" copying link: %q -> %q\n", path, destPath)
  137. if err := copyLink(path, destPath); err != nil {
  138. return err
  139. }
  140. } else {
  141. trace(" copying file: %q -> %q\n", path, destPath)
  142. if err := copyPseudoFile(path, destPath); err != nil && !errors.Is(err, os.ErrPermission) {
  143. return err
  144. }
  145. }
  146. }
  147. return nil
  148. }
  149. func isSymlink(path string, fi os.FileInfo) bool {
  150. return fi.Mode()&os.ModeSymlink != 0
  151. }
  152. func isDriversDir(path string, fi os.FileInfo) bool {
  153. return strings.Contains(path, "drivers")
  154. }
  155. func copyLink(path, targetPath string) error {
  156. target, err := os.Readlink(path)
  157. if err != nil {
  158. return err
  159. }
  160. trace(" symlink %q -> %q\n", target, targetPath)
  161. if err := os.Symlink(target, targetPath); err != nil {
  162. if errors.Is(err, os.ErrExist) {
  163. return nil
  164. }
  165. return err
  166. }
  167. return nil
  168. }
  169. func copyPseudoFile(path, targetPath string) error {
  170. buf, err := ioutil.ReadFile(path)
  171. if err != nil {
  172. return err
  173. }
  174. trace("creating %s\n", targetPath)
  175. f, err := os.Create(targetPath)
  176. if err != nil {
  177. return err
  178. }
  179. if _, err = f.Write(buf); err != nil {
  180. return err
  181. }
  182. f.Close()
  183. return nil
  184. }