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