copy.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. package archive
  2. import (
  3. "archive/tar"
  4. "errors"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "github.com/Sirupsen/logrus"
  11. "github.com/docker/docker/pkg/system"
  12. )
  13. // Errors used or returned by this file.
  14. var (
  15. ErrNotDirectory = errors.New("not a directory")
  16. ErrDirNotExists = errors.New("no such directory")
  17. ErrCannotCopyDir = errors.New("cannot copy directory")
  18. ErrInvalidCopySource = errors.New("invalid copy source content")
  19. )
  20. // PreserveTrailingDotOrSeparator returns the given cleaned path (after
  21. // processing using any utility functions from the path or filepath stdlib
  22. // packages) and appends a trailing `/.` or `/` if its corresponding original
  23. // path (from before being processed by utility functions from the path or
  24. // filepath stdlib packages) ends with a trailing `/.` or `/`. If the cleaned
  25. // path already ends in a `.` path segment, then another is not added. If the
  26. // clean path already ends in a path separator, then another is not added.
  27. func PreserveTrailingDotOrSeparator(cleanedPath, originalPath string) string {
  28. // Ensure paths are in platform semantics
  29. cleanedPath = normalizePath(cleanedPath)
  30. originalPath = normalizePath(originalPath)
  31. if !specifiesCurrentDir(cleanedPath) && specifiesCurrentDir(originalPath) {
  32. if !hasTrailingPathSeparator(cleanedPath) {
  33. // Add a separator if it doesn't already end with one (a cleaned
  34. // path would only end in a separator if it is the root).
  35. cleanedPath += string(filepath.Separator)
  36. }
  37. cleanedPath += "."
  38. }
  39. if !hasTrailingPathSeparator(cleanedPath) && hasTrailingPathSeparator(originalPath) {
  40. cleanedPath += string(filepath.Separator)
  41. }
  42. return cleanedPath
  43. }
  44. // assertsDirectory returns whether the given path is
  45. // asserted to be a directory, i.e., the path ends with
  46. // a trailing '/' or `/.`, assuming a path separator of `/`.
  47. func assertsDirectory(path string) bool {
  48. return hasTrailingPathSeparator(path) || specifiesCurrentDir(path)
  49. }
  50. // hasTrailingPathSeparator returns whether the given
  51. // path ends with the system's path separator character.
  52. func hasTrailingPathSeparator(path string) bool {
  53. return len(path) > 0 && os.IsPathSeparator(path[len(path)-1])
  54. }
  55. // specifiesCurrentDir returns whether the given path specifies
  56. // a "current directory", i.e., the last path segment is `.`.
  57. func specifiesCurrentDir(path string) bool {
  58. return filepath.Base(path) == "."
  59. }
  60. // SplitPathDirEntry splits the given path between its directory name and its
  61. // basename by first cleaning the path but preserves a trailing "." if the
  62. // original path specified the current directory.
  63. func SplitPathDirEntry(path string) (dir, base string) {
  64. cleanedPath := filepath.Clean(normalizePath(path))
  65. if specifiesCurrentDir(path) {
  66. cleanedPath += string(filepath.Separator) + "."
  67. }
  68. return filepath.Dir(cleanedPath), filepath.Base(cleanedPath)
  69. }
  70. // TarResource archives the resource described by the given CopyInfo to a Tar
  71. // archive. A non-nil error is returned if sourcePath does not exist or is
  72. // asserted to be a directory but exists as another type of file.
  73. //
  74. // This function acts as a convenient wrapper around TarWithOptions, which
  75. // requires a directory as the source path. TarResource accepts either a
  76. // directory or a file path and correctly sets the Tar options.
  77. func TarResource(sourceInfo CopyInfo) (content io.ReadCloser, err error) {
  78. return TarResourceRebase(sourceInfo.Path, sourceInfo.RebaseName)
  79. }
  80. // TarResourceRebase is like TarResource but renames the first path element of
  81. // items in the resulting tar archive to match the given rebaseName if not "".
  82. func TarResourceRebase(sourcePath, rebaseName string) (content io.ReadCloser, err error) {
  83. sourcePath = normalizePath(sourcePath)
  84. if _, err = os.Lstat(sourcePath); err != nil {
  85. // Catches the case where the source does not exist or is not a
  86. // directory if asserted to be a directory, as this also causes an
  87. // error.
  88. return
  89. }
  90. // Separate the source path between its directory and
  91. // the entry in that directory which we are archiving.
  92. sourceDir, sourceBase := SplitPathDirEntry(sourcePath)
  93. filter := []string{sourceBase}
  94. logrus.Debugf("copying %q from %q", sourceBase, sourceDir)
  95. return TarWithOptions(sourceDir, &TarOptions{
  96. Compression: Uncompressed,
  97. IncludeFiles: filter,
  98. IncludeSourceDir: true,
  99. RebaseNames: map[string]string{
  100. sourceBase: rebaseName,
  101. },
  102. })
  103. }
  104. // CopyInfo holds basic info about the source
  105. // or destination path of a copy operation.
  106. type CopyInfo struct {
  107. Path string
  108. Exists bool
  109. IsDir bool
  110. RebaseName string
  111. }
  112. // CopyInfoSourcePath stats the given path to create a CopyInfo
  113. // struct representing that resource for the source of an archive copy
  114. // operation. The given path should be an absolute local path. A source path
  115. // has all symlinks evaluated that appear before the last path separator ("/"
  116. // on Unix). As it is to be a copy source, the path must exist.
  117. func CopyInfoSourcePath(path string, followLink bool) (CopyInfo, error) {
  118. // normalize the file path and then evaluate the symbol link
  119. // we will use the target file instead of the symbol link if
  120. // followLink is set
  121. path = normalizePath(path)
  122. resolvedPath, rebaseName, err := ResolveHostSourcePath(path, followLink)
  123. if err != nil {
  124. return CopyInfo{}, err
  125. }
  126. stat, err := os.Lstat(resolvedPath)
  127. if err != nil {
  128. return CopyInfo{}, err
  129. }
  130. return CopyInfo{
  131. Path: resolvedPath,
  132. Exists: true,
  133. IsDir: stat.IsDir(),
  134. RebaseName: rebaseName,
  135. }, nil
  136. }
  137. // CopyInfoDestinationPath stats the given path to create a CopyInfo
  138. // struct representing that resource for the destination of an archive copy
  139. // operation. The given path should be an absolute local path.
  140. func CopyInfoDestinationPath(path string) (info CopyInfo, err error) {
  141. maxSymlinkIter := 10 // filepath.EvalSymlinks uses 255, but 10 already seems like a lot.
  142. path = normalizePath(path)
  143. originalPath := path
  144. stat, err := os.Lstat(path)
  145. if err == nil && stat.Mode()&os.ModeSymlink == 0 {
  146. // The path exists and is not a symlink.
  147. return CopyInfo{
  148. Path: path,
  149. Exists: true,
  150. IsDir: stat.IsDir(),
  151. }, nil
  152. }
  153. // While the path is a symlink.
  154. for n := 0; err == nil && stat.Mode()&os.ModeSymlink != 0; n++ {
  155. if n > maxSymlinkIter {
  156. // Don't follow symlinks more than this arbitrary number of times.
  157. return CopyInfo{}, errors.New("too many symlinks in " + originalPath)
  158. }
  159. // The path is a symbolic link. We need to evaluate it so that the
  160. // destination of the copy operation is the link target and not the
  161. // link itself. This is notably different than CopyInfoSourcePath which
  162. // only evaluates symlinks before the last appearing path separator.
  163. // Also note that it is okay if the last path element is a broken
  164. // symlink as the copy operation should create the target.
  165. var linkTarget string
  166. linkTarget, err = os.Readlink(path)
  167. if err != nil {
  168. return CopyInfo{}, err
  169. }
  170. if !system.IsAbs(linkTarget) {
  171. // Join with the parent directory.
  172. dstParent, _ := SplitPathDirEntry(path)
  173. linkTarget = filepath.Join(dstParent, linkTarget)
  174. }
  175. path = linkTarget
  176. stat, err = os.Lstat(path)
  177. }
  178. if err != nil {
  179. // It's okay if the destination path doesn't exist. We can still
  180. // continue the copy operation if the parent directory exists.
  181. if !os.IsNotExist(err) {
  182. return CopyInfo{}, err
  183. }
  184. // Ensure destination parent dir exists.
  185. dstParent, _ := SplitPathDirEntry(path)
  186. parentDirStat, err := os.Lstat(dstParent)
  187. if err != nil {
  188. return CopyInfo{}, err
  189. }
  190. if !parentDirStat.IsDir() {
  191. return CopyInfo{}, ErrNotDirectory
  192. }
  193. return CopyInfo{Path: path}, nil
  194. }
  195. // The path exists after resolving symlinks.
  196. return CopyInfo{
  197. Path: path,
  198. Exists: true,
  199. IsDir: stat.IsDir(),
  200. }, nil
  201. }
  202. // PrepareArchiveCopy prepares the given srcContent archive, which should
  203. // contain the archived resource described by srcInfo, to the destination
  204. // described by dstInfo. Returns the possibly modified content archive along
  205. // with the path to the destination directory which it should be extracted to.
  206. func PrepareArchiveCopy(srcContent io.Reader, srcInfo, dstInfo CopyInfo) (dstDir string, content io.ReadCloser, err error) {
  207. // Ensure in platform semantics
  208. srcInfo.Path = normalizePath(srcInfo.Path)
  209. dstInfo.Path = normalizePath(dstInfo.Path)
  210. // Separate the destination path between its directory and base
  211. // components in case the source archive contents need to be rebased.
  212. dstDir, dstBase := SplitPathDirEntry(dstInfo.Path)
  213. _, srcBase := SplitPathDirEntry(srcInfo.Path)
  214. switch {
  215. case dstInfo.Exists && dstInfo.IsDir:
  216. // The destination exists as a directory. No alteration
  217. // to srcContent is needed as its contents can be
  218. // simply extracted to the destination directory.
  219. return dstInfo.Path, ioutil.NopCloser(srcContent), nil
  220. case dstInfo.Exists && srcInfo.IsDir:
  221. // The destination exists as some type of file and the source
  222. // content is a directory. This is an error condition since
  223. // you cannot copy a directory to an existing file location.
  224. return "", nil, ErrCannotCopyDir
  225. case dstInfo.Exists:
  226. // The destination exists as some type of file and the source content
  227. // is also a file. The source content entry will have to be renamed to
  228. // have a basename which matches the destination path's basename.
  229. if len(srcInfo.RebaseName) != 0 {
  230. srcBase = srcInfo.RebaseName
  231. }
  232. return dstDir, RebaseArchiveEntries(srcContent, srcBase, dstBase), nil
  233. case srcInfo.IsDir:
  234. // The destination does not exist and the source content is an archive
  235. // of a directory. The archive should be extracted to the parent of
  236. // the destination path instead, and when it is, the directory that is
  237. // created as a result should take the name of the destination path.
  238. // The source content entries will have to be renamed to have a
  239. // basename which matches the destination path's basename.
  240. if len(srcInfo.RebaseName) != 0 {
  241. srcBase = srcInfo.RebaseName
  242. }
  243. return dstDir, RebaseArchiveEntries(srcContent, srcBase, dstBase), nil
  244. case assertsDirectory(dstInfo.Path):
  245. // The destination does not exist and is asserted to be created as a
  246. // directory, but the source content is not a directory. This is an
  247. // error condition since you cannot create a directory from a file
  248. // source.
  249. return "", nil, ErrDirNotExists
  250. default:
  251. // The last remaining case is when the destination does not exist, is
  252. // not asserted to be a directory, and the source content is not an
  253. // archive of a directory. It this case, the destination file will need
  254. // to be created when the archive is extracted and the source content
  255. // entry will have to be renamed to have a basename which matches the
  256. // destination path's basename.
  257. if len(srcInfo.RebaseName) != 0 {
  258. srcBase = srcInfo.RebaseName
  259. }
  260. return dstDir, RebaseArchiveEntries(srcContent, srcBase, dstBase), nil
  261. }
  262. }
  263. // RebaseArchiveEntries rewrites the given srcContent archive replacing
  264. // an occurrence of oldBase with newBase at the beginning of entry names.
  265. func RebaseArchiveEntries(srcContent io.Reader, oldBase, newBase string) io.ReadCloser {
  266. if oldBase == string(os.PathSeparator) {
  267. // If oldBase specifies the root directory, use an empty string as
  268. // oldBase instead so that newBase doesn't replace the path separator
  269. // that all paths will start with.
  270. oldBase = ""
  271. }
  272. rebased, w := io.Pipe()
  273. go func() {
  274. srcTar := tar.NewReader(srcContent)
  275. rebasedTar := tar.NewWriter(w)
  276. for {
  277. hdr, err := srcTar.Next()
  278. if err == io.EOF {
  279. // Signals end of archive.
  280. rebasedTar.Close()
  281. w.Close()
  282. return
  283. }
  284. if err != nil {
  285. w.CloseWithError(err)
  286. return
  287. }
  288. hdr.Name = strings.Replace(hdr.Name, oldBase, newBase, 1)
  289. if err = rebasedTar.WriteHeader(hdr); err != nil {
  290. w.CloseWithError(err)
  291. return
  292. }
  293. if _, err = io.Copy(rebasedTar, srcTar); err != nil {
  294. w.CloseWithError(err)
  295. return
  296. }
  297. }
  298. }()
  299. return rebased
  300. }
  301. // CopyResource performs an archive copy from the given source path to the
  302. // given destination path. The source path MUST exist and the destination
  303. // path's parent directory must exist.
  304. func CopyResource(srcPath, dstPath string, followLink bool) error {
  305. var (
  306. srcInfo CopyInfo
  307. err error
  308. )
  309. // Ensure in platform semantics
  310. srcPath = normalizePath(srcPath)
  311. dstPath = normalizePath(dstPath)
  312. // Clean the source and destination paths.
  313. srcPath = PreserveTrailingDotOrSeparator(filepath.Clean(srcPath), srcPath)
  314. dstPath = PreserveTrailingDotOrSeparator(filepath.Clean(dstPath), dstPath)
  315. if srcInfo, err = CopyInfoSourcePath(srcPath, followLink); err != nil {
  316. return err
  317. }
  318. content, err := TarResource(srcInfo)
  319. if err != nil {
  320. return err
  321. }
  322. defer content.Close()
  323. return CopyTo(content, srcInfo, dstPath)
  324. }
  325. // CopyTo handles extracting the given content whose
  326. // entries should be sourced from srcInfo to dstPath.
  327. func CopyTo(content io.Reader, srcInfo CopyInfo, dstPath string) error {
  328. // The destination path need not exist, but CopyInfoDestinationPath will
  329. // ensure that at least the parent directory exists.
  330. dstInfo, err := CopyInfoDestinationPath(normalizePath(dstPath))
  331. if err != nil {
  332. return err
  333. }
  334. dstDir, copyArchive, err := PrepareArchiveCopy(content, srcInfo, dstInfo)
  335. if err != nil {
  336. return err
  337. }
  338. defer copyArchive.Close()
  339. options := &TarOptions{
  340. NoLchown: true,
  341. NoOverwriteDirNonDir: true,
  342. }
  343. return Untar(copyArchive, dstDir, options)
  344. }
  345. // ResolveHostSourcePath decides real path need to be copied with parameters such as
  346. // whether to follow symbol link or not, if followLink is true, resolvedPath will return
  347. // link target of any symbol link file, else it will only resolve symlink of directory
  348. // but return symbol link file itself without resolving.
  349. func ResolveHostSourcePath(path string, followLink bool) (resolvedPath, rebaseName string, err error) {
  350. if followLink {
  351. resolvedPath, err = filepath.EvalSymlinks(path)
  352. if err != nil {
  353. return
  354. }
  355. resolvedPath, rebaseName = GetRebaseName(path, resolvedPath)
  356. } else {
  357. dirPath, basePath := filepath.Split(path)
  358. // if not follow symbol link, then resolve symbol link of parent dir
  359. var resolvedDirPath string
  360. resolvedDirPath, err = filepath.EvalSymlinks(dirPath)
  361. if err != nil {
  362. return
  363. }
  364. // resolvedDirPath will have been cleaned (no trailing path separators) so
  365. // we can manually join it with the base path element.
  366. resolvedPath = resolvedDirPath + string(filepath.Separator) + basePath
  367. if hasTrailingPathSeparator(path) && filepath.Base(path) != filepath.Base(resolvedPath) {
  368. rebaseName = filepath.Base(path)
  369. }
  370. }
  371. return resolvedPath, rebaseName, nil
  372. }
  373. // GetRebaseName normalizes and compares path and resolvedPath,
  374. // return completed resolved path and rebased file name
  375. func GetRebaseName(path, resolvedPath string) (string, string) {
  376. // linkTarget will have been cleaned (no trailing path separators and dot) so
  377. // we can manually join it with them
  378. var rebaseName string
  379. if specifiesCurrentDir(path) && !specifiesCurrentDir(resolvedPath) {
  380. resolvedPath += string(filepath.Separator) + "."
  381. }
  382. if hasTrailingPathSeparator(path) && !hasTrailingPathSeparator(resolvedPath) {
  383. resolvedPath += string(filepath.Separator)
  384. }
  385. if filepath.Base(path) != filepath.Base(resolvedPath) {
  386. // In the case where the path had a trailing separator and a symlink
  387. // evaluation has changed the last path component, we will need to
  388. // rebase the name in the archive that is being copied to match the
  389. // originally requested name.
  390. rebaseName = filepath.Base(path)
  391. }
  392. return resolvedPath, rebaseName
  393. }