| 1 |
package fs |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"os" |
| 6 |
"path" |
| 7 |
"path/filepath" |
| 8 |
"runtime" |
| 9 |
"slices" |
| 10 |
"strings" |
| 11 |
"sync" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/containerd/continuity/fs" |
| 15 |
"github.com/moby/patternmatcher" |
| 16 |
"github.com/pkg/errors" |
| 17 |
mode "github.com/tonistiigi/dchapes-mode" |
| 18 |
"github.com/tonistiigi/fsutil" |
| 19 |
) |
| 20 |
|
| 21 |
const defaultDirectoryMode = 0755 |
| 22 |
|
| 23 |
var bufferPool = &sync.Pool{ |
| 24 |
New: func() interface{} { |
| 25 |
buffer := make([]byte, 32*1024) |
| 26 |
return &buffer |
| 27 |
}, |
| 28 |
} |
| 29 |
|
| 30 |
func rootPath(root, p string, followLinks bool) (string, error) { |
| 31 |
p = filepath.Join("/", p) |
| 32 |
if p == "/" { |
| 33 |
return root, nil |
| 34 |
} |
| 35 |
if followLinks { |
| 36 |
return fs.RootPath(root, p) |
| 37 |
} |
| 38 |
d, f := filepath.Split(p) |
| 39 |
ppath, err := fs.RootPath(root, d) |
| 40 |
if err != nil { |
| 41 |
return "", err |
| 42 |
} |
| 43 |
return filepath.Join(ppath, f), nil |
| 44 |
} |
| 45 |
|
| 46 |
func ResolveWildcards(root, src string, followLinks bool) ([]string, error) { |
| 47 |
d1, d2 := splitWildcards(src) |
| 48 |
if d2 != "" { |
| 49 |
p, err := rootPath(root, d1, followLinks) |
| 50 |
if err != nil { |
| 51 |
return nil, err |
| 52 |
} |
| 53 |
matches, err := resolveWildcards(p, d2) |
| 54 |
if err != nil { |
| 55 |
return nil, err |
| 56 |
} |
| 57 |
for i, m := range matches { |
| 58 |
p, err := rel(root, m) |
| 59 |
if err != nil { |
| 60 |
return nil, err |
| 61 |
} |
| 62 |
matches[i] = p |
| 63 |
} |
| 64 |
return matches, nil |
| 65 |
} |
| 66 |
return []string{d1}, nil |
| 67 |
} |
| 68 |
|
| 69 |
// Copy copies files using `cp -a` semantics. |
| 70 |
// Copy is likely unsafe to be used in non-containerized environments. |
| 71 |
func Copy(ctx context.Context, srcRoot, src, dstRoot, dst string, opts ...Opt) error { |
| 72 |
var ci CopyInfo |
| 73 |
for _, o := range opts { |
| 74 |
o(&ci) |
| 75 |
} |
| 76 |
ensureDstPath := dst |
| 77 |
if d, f := filepath.Split(dst); f != "" && f != "." { |
| 78 |
ensureDstPath = d |
| 79 |
} |
| 80 |
if ensureDstPath != "" { |
| 81 |
ensureDstPath, err := fs.RootPath(dstRoot, ensureDstPath) |
| 82 |
if err != nil { |
| 83 |
return err |
| 84 |
} |
| 85 |
perm := defaultDirectoryMode |
| 86 |
if ci.Mode != nil { |
| 87 |
perm = *ci.Mode |
| 88 |
} |
| 89 |
if createdDirs, err := MkdirAll(ensureDstPath, os.FileMode(perm), ci.Chown, ci.Utime); err != nil { |
| 90 |
return err |
| 91 |
} else { |
| 92 |
defer fixCreatedParentDirs(createdDirs, ci.Utime) |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
var modeSet *mode.Set |
| 97 |
if ci.ModeStr != "" { |
| 98 |
ms, err := mode.ParseWithUmask(ci.ModeStr, 0) |
| 99 |
if err != nil { |
| 100 |
return err |
| 101 |
} |
| 102 |
modeSet = &ms |
| 103 |
} |
| 104 |
|
| 105 |
dst, err := fs.RootPath(dstRoot, filepath.Clean(dst)) |
| 106 |
if err != nil { |
| 107 |
return err |
| 108 |
} |
| 109 |
|
| 110 |
c, err := newCopier(dstRoot, ci.Chown, ci.Utime, ci.Mode, modeSet, ci.XAttrErrorHandler, ci.IncludePatterns, ci.ExcludePatterns, ci.AlwaysReplaceExistingDestPaths, ci.ChangeFunc) |
| 111 |
if err != nil { |
| 112 |
return err |
| 113 |
} |
| 114 |
srcs := []string{src} |
| 115 |
|
| 116 |
if ci.AllowWildcards { |
| 117 |
matches, err := ResolveWildcards(srcRoot, src, ci.FollowLinks) |
| 118 |
if err != nil { |
| 119 |
return err |
| 120 |
} |
| 121 |
if len(matches) == 0 { |
| 122 |
return errors.Errorf("no matches found: %s", src) |
| 123 |
} |
| 124 |
srcs = matches |
| 125 |
} |
| 126 |
|
| 127 |
for _, src := range srcs { |
| 128 |
srcFollowed, err := rootPath(srcRoot, src, ci.FollowLinks) |
| 129 |
if err != nil { |
| 130 |
return err |
| 131 |
} |
| 132 |
dst, createdDirs, err := c.prepareTargetDir(srcFollowed, src, dst, ci.CopyDirContents) |
| 133 |
if err != nil { |
| 134 |
return err |
| 135 |
} |
| 136 |
defer fixCreatedParentDirs(createdDirs, ci.Utime) |
| 137 |
if err := c.copy(ctx, srcFollowed, "", dst, false, patternmatcher.MatchInfo{}, patternmatcher.MatchInfo{}); err != nil { |
| 138 |
return err |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
return nil |
| 143 |
} |
| 144 |
|
| 145 |
func (c *copier) prepareTargetDir(srcFollowed, src, destPath string, copyDirContents bool) (string, []string, error) { |
| 146 |
fiSrc, err := os.Lstat(srcFollowed) |
| 147 |
if err != nil { |
| 148 |
return "", nil, err |
| 149 |
} |
| 150 |
|
| 151 |
fiDest, err := os.Stat(destPath) |
| 152 |
if err != nil { |
| 153 |
if !os.IsNotExist(err) { |
| 154 |
return "", nil, errors.Wrap(err, "failed to lstat destination path") |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
if (!copyDirContents && fiSrc.IsDir() && fiDest != nil) || (!fiSrc.IsDir() && fiDest != nil && fiDest.IsDir()) { |
| 159 |
destPath = filepath.Join(destPath, filepath.Base(src)) |
| 160 |
} |
| 161 |
|
| 162 |
target := filepath.Dir(destPath) |
| 163 |
|
| 164 |
if copyDirContents && fiSrc.IsDir() && fiDest == nil { |
| 165 |
target = destPath |
| 166 |
} |
| 167 |
var createdDirs []string |
| 168 |
mode := defaultDirectoryMode |
| 169 |
if c.mode != nil { |
| 170 |
mode = *c.mode |
| 171 |
} |
| 172 |
if dirs, err := MkdirAll(target, os.FileMode(mode), c.chown, c.utime); err != nil { |
| 173 |
return "", nil, err |
| 174 |
} else { |
| 175 |
createdDirs = dirs |
| 176 |
} |
| 177 |
|
| 178 |
return destPath, createdDirs, nil |
| 179 |
} |
| 180 |
|
| 181 |
type User struct { |
| 182 |
UID, GID int |
| 183 |
SID string |
| 184 |
} |
| 185 |
|
| 186 |
type Chowner func(*User) (*User, error) |
| 187 |
|
| 188 |
type XAttrErrorHandler func(dst, src, xattrKey string, err error) error |
| 189 |
|
| 190 |
type CopyInfo struct { |
| 191 |
Chown Chowner |
| 192 |
Utime *time.Time |
| 193 |
AllowWildcards bool |
| 194 |
Mode *int |
| 195 |
// ModeStr is mode in non-octal format. Overrides Mode if non-empty. |
| 196 |
ModeStr string |
| 197 |
XAttrErrorHandler XAttrErrorHandler |
| 198 |
CopyDirContents bool |
| 199 |
FollowLinks bool |
| 200 |
// Include only files/dirs matching at least one of these patterns |
| 201 |
IncludePatterns []string |
| 202 |
// Exclude files/dir matching any of these patterns (even if they match an include pattern) |
| 203 |
ExcludePatterns []string |
| 204 |
// If true, any source path that overwrite existing destination paths will always replace |
| 205 |
// the existing destination path, even if they are of different types (e.g. a directory will |
| 206 |
// replace any existing symlink or file) |
| 207 |
AlwaysReplaceExistingDestPaths bool |
| 208 |
ChangeFunc fsutil.ChangeFunc |
| 209 |
} |
| 210 |
|
| 211 |
type Opt func(*CopyInfo) |
| 212 |
|
| 213 |
func WithCopyInfo(ci CopyInfo) func(*CopyInfo) { |
| 214 |
return func(c *CopyInfo) { |
| 215 |
*c = ci |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
func WithChown(uid, gid int) Opt { |
| 220 |
return func(ci *CopyInfo) { |
| 221 |
ci.Chown = func(*User) (*User, error) { |
| 222 |
return &User{UID: uid, GID: gid}, nil |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
func AllowWildcards(ci *CopyInfo) { |
| 228 |
ci.AllowWildcards = true |
| 229 |
} |
| 230 |
|
| 231 |
func WithXAttrErrorHandler(h XAttrErrorHandler) Opt { |
| 232 |
return func(ci *CopyInfo) { |
| 233 |
ci.XAttrErrorHandler = h |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
func AllowXAttrErrors(ci *CopyInfo) { |
| 238 |
h := func(string, string, string, error) error { |
| 239 |
return nil |
| 240 |
} |
| 241 |
WithXAttrErrorHandler(h)(ci) |
| 242 |
} |
| 243 |
|
| 244 |
func WithIncludePattern(includePattern string) Opt { |
| 245 |
return func(ci *CopyInfo) { |
| 246 |
ci.IncludePatterns = append(ci.IncludePatterns, includePattern) |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
func WithExcludePattern(excludePattern string) Opt { |
| 251 |
return func(ci *CopyInfo) { |
| 252 |
ci.ExcludePatterns = append(ci.ExcludePatterns, excludePattern) |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
func WithChangeNotifier(fn fsutil.ChangeFunc) Opt { |
| 257 |
return func(ci *CopyInfo) { |
| 258 |
ci.ChangeFunc = fn |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
type copier struct { |
| 263 |
chown Chowner |
| 264 |
utime *time.Time |
| 265 |
mode *int |
| 266 |
modeSet *mode.Set |
| 267 |
inodes map[uint64]string |
| 268 |
xattrErrorHandler XAttrErrorHandler |
| 269 |
includePatternMatcher *patternmatcher.PatternMatcher |
| 270 |
excludePatternMatcher *patternmatcher.PatternMatcher |
| 271 |
parentDirs []parentDir |
| 272 |
changefn fsutil.ChangeFunc |
| 273 |
root string |
| 274 |
alwaysReplaceExistingDestPaths bool |
| 275 |
} |
| 276 |
|
| 277 |
type parentDir struct { |
| 278 |
srcPath string |
| 279 |
dstPath string |
| 280 |
copied bool |
| 281 |
} |
| 282 |
|
| 283 |
func newCopier(root string, chown Chowner, tm *time.Time, mode *int, modeSet *mode.Set, xeh XAttrErrorHandler, includePatterns, excludePatterns []string, alwaysReplaceExistingDestPaths bool, changeFunc fsutil.ChangeFunc) (*copier, error) { |
| 284 |
if xeh == nil { |
| 285 |
xeh = func(dst, src, key string, err error) error { |
| 286 |
return err |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
var includePatternMatcher *patternmatcher.PatternMatcher |
| 291 |
if len(includePatterns) != 0 { |
| 292 |
var err error |
| 293 |
includePatternMatcher, err = patternmatcher.New(includePatterns) |
| 294 |
if err != nil { |
| 295 |
return nil, errors.Wrapf(err, "invalid includepatterns: %s", includePatterns) |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
var excludePatternMatcher *patternmatcher.PatternMatcher |
| 300 |
if len(excludePatterns) != 0 { |
| 301 |
var err error |
| 302 |
excludePatternMatcher, err = patternmatcher.New(excludePatterns) |
| 303 |
if err != nil { |
| 304 |
return nil, errors.Wrapf(err, "invalid excludepatterns: %s", excludePatterns) |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
return &copier{ |
| 309 |
root: root, |
| 310 |
inodes: map[uint64]string{}, |
| 311 |
chown: chown, |
| 312 |
utime: tm, |
| 313 |
xattrErrorHandler: xeh, |
| 314 |
mode: mode, |
| 315 |
modeSet: modeSet, |
| 316 |
includePatternMatcher: includePatternMatcher, |
| 317 |
excludePatternMatcher: excludePatternMatcher, |
| 318 |
changefn: changeFunc, |
| 319 |
alwaysReplaceExistingDestPaths: alwaysReplaceExistingDestPaths, |
| 320 |
}, nil |
| 321 |
} |
| 322 |
|
| 323 |
// dest is always clean |
| 324 |
func (c *copier) copy(ctx context.Context, src, srcComponents, target string, overwriteTargetMetadata bool, parentIncludeMatchInfo, parentExcludeMatchInfo patternmatcher.MatchInfo) error { |
| 325 |
select { |
| 326 |
case <-ctx.Done(): |
| 327 |
return ctx.Err() |
| 328 |
default: |
| 329 |
} |
| 330 |
|
| 331 |
fi, err := os.Lstat(src) |
| 332 |
if err != nil { |
| 333 |
return errors.Wrapf(err, "failed to stat %s", src) |
| 334 |
} |
| 335 |
targetFi, err := os.Lstat(target) |
| 336 |
if err != nil && !os.IsNotExist(err) { |
| 337 |
return errors.Wrapf(err, "failed to stat %s", src) |
| 338 |
} |
| 339 |
|
| 340 |
include := true |
| 341 |
var ( |
| 342 |
includeMatchInfo patternmatcher.MatchInfo |
| 343 |
excludeMatchInfo patternmatcher.MatchInfo |
| 344 |
) |
| 345 |
if srcComponents != "" { |
| 346 |
matchesIncludePattern := false |
| 347 |
matchesExcludePattern := false |
| 348 |
matchesIncludePattern, includeMatchInfo, err = c.include(srcComponents, parentIncludeMatchInfo) |
| 349 |
if err != nil { |
| 350 |
return err |
| 351 |
} |
| 352 |
include = matchesIncludePattern |
| 353 |
|
| 354 |
matchesExcludePattern, excludeMatchInfo, err = c.exclude(srcComponents, parentExcludeMatchInfo) |
| 355 |
if err != nil { |
| 356 |
return err |
| 357 |
} |
| 358 |
if matchesExcludePattern { |
| 359 |
include = false |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
if include { |
| 364 |
if err := c.removeTargetIfNeeded(target, fi, targetFi); err != nil { |
| 365 |
return err |
| 366 |
} |
| 367 |
|
| 368 |
if err := c.createParentDirs(src, overwriteTargetMetadata); err != nil { |
| 369 |
return err |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
if !fi.IsDir() { |
| 374 |
if !include { |
| 375 |
return nil |
| 376 |
} |
| 377 |
|
| 378 |
if err := ensureEmptyFileTarget(target); err != nil { |
| 379 |
return err |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
copyFileInfo := include |
| 384 |
restoreFileTimestamp := false |
| 385 |
notify := true |
| 386 |
|
| 387 |
switch { |
| 388 |
case fi.IsDir(): |
| 389 |
if created, err := c.copyDirectory( |
| 390 |
ctx, src, srcComponents, target, fi, overwriteTargetMetadata, |
| 391 |
include, includeMatchInfo, excludeMatchInfo, |
| 392 |
); err != nil { |
| 393 |
return err |
| 394 |
} else if !overwriteTargetMetadata { |
| 395 |
// if we aren't supposed to overwrite existing target metadata, |
| 396 |
// then we only need to copy the new file info if we newly created |
| 397 |
// it, or restore the previous file timestamp if not |
| 398 |
copyFileInfo = created |
| 399 |
restoreFileTimestamp = !created |
| 400 |
} |
| 401 |
notify = false |
| 402 |
case (fi.Mode() & os.ModeType) == 0: |
| 403 |
link, err := getLinkSource(target, fi, c.inodes) |
| 404 |
if err != nil { |
| 405 |
return errors.Wrap(err, "failed to get hardlink") |
| 406 |
} |
| 407 |
if link != "" { |
| 408 |
if err := os.Link(link, target); err != nil { |
| 409 |
return errors.Wrap(err, "failed to create hard link") |
| 410 |
} |
| 411 |
} else if err := copyFile(src, target); err != nil { |
| 412 |
return errors.Wrap(err, "failed to copy files") |
| 413 |
} |
| 414 |
case (fi.Mode() & os.ModeSymlink) == os.ModeSymlink: |
| 415 |
link, err := os.Readlink(src) |
| 416 |
if err != nil { |
| 417 |
return errors.Wrapf(err, "failed to read link: %s", src) |
| 418 |
} |
| 419 |
if err := os.Symlink(link, target); err != nil { |
| 420 |
return errors.Wrapf(err, "failed to create symlink: %s", target) |
| 421 |
} |
| 422 |
case (fi.Mode() & os.ModeDevice) == os.ModeDevice, |
| 423 |
(fi.Mode() & os.ModeNamedPipe) == os.ModeNamedPipe, |
| 424 |
(fi.Mode() & os.ModeSocket) == os.ModeSocket: |
| 425 |
if err := copyDevice(target, fi); err != nil { |
| 426 |
return errors.Wrapf(err, "failed to create device") |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
if copyFileInfo { |
| 431 |
if err := c.copyFileInfo(fi, src, target); err != nil { |
| 432 |
return errors.Wrap(err, "failed to copy file info") |
| 433 |
} |
| 434 |
|
| 435 |
if err := copyXAttrs(target, src, c.xattrErrorHandler); err != nil { |
| 436 |
return errors.Wrap(err, "failed to copy xattrs") |
| 437 |
} |
| 438 |
} else if restoreFileTimestamp && targetFi != nil { |
| 439 |
if err := c.copyFileTimestamp(fi, target); err != nil { |
| 440 |
return errors.Wrap(err, "failed to restore file timestamp") |
| 441 |
} |
| 442 |
} |
| 443 |
if notify { |
| 444 |
if err := c.notifyChange(target, fi); err != nil { |
| 445 |
return err |
| 446 |
} |
| 447 |
} |
| 448 |
return nil |
| 449 |
} |
| 450 |
|
| 451 |
func (c *copier) notifyChange(target string, fi os.FileInfo) error { |
| 452 |
if c.changefn != nil { |
| 453 |
if err := c.changefn(fsutil.ChangeKindAdd, path.Clean(strings.TrimPrefix(target, c.root)), fi, nil); err != nil { |
| 454 |
return errors.Wrap(err, "failed to notify file change") |
| 455 |
} |
| 456 |
} |
| 457 |
return nil |
| 458 |
} |
| 459 |
|
| 460 |
func (c *copier) include(path string, parentIncludeMatchInfo patternmatcher.MatchInfo) (bool, patternmatcher.MatchInfo, error) { |
| 461 |
if c.includePatternMatcher == nil { |
| 462 |
return true, patternmatcher.MatchInfo{}, nil |
| 463 |
} |
| 464 |
|
| 465 |
m, matchInfo, err := c.includePatternMatcher.MatchesUsingParentResults(path, parentIncludeMatchInfo) |
| 466 |
if err != nil { |
| 467 |
return false, matchInfo, errors.Wrap(err, "failed to match includepatterns") |
| 468 |
} |
| 469 |
return m, matchInfo, nil |
| 470 |
} |
| 471 |
|
| 472 |
func (c *copier) exclude(path string, parentExcludeMatchInfo patternmatcher.MatchInfo) (bool, patternmatcher.MatchInfo, error) { |
| 473 |
if c.excludePatternMatcher == nil { |
| 474 |
return false, patternmatcher.MatchInfo{}, nil |
| 475 |
} |
| 476 |
|
| 477 |
m, matchInfo, err := c.excludePatternMatcher.MatchesUsingParentResults(path, parentExcludeMatchInfo) |
| 478 |
if err != nil { |
| 479 |
return false, matchInfo, errors.Wrap(err, "failed to match excludepatterns") |
| 480 |
} |
| 481 |
return m, matchInfo, nil |
| 482 |
} |
| 483 |
|
| 484 |
func (c *copier) removeTargetIfNeeded(target string, srcFi, targetFi os.FileInfo) error { |
| 485 |
if !c.alwaysReplaceExistingDestPaths { |
| 486 |
return nil |
| 487 |
} |
| 488 |
if targetFi == nil { |
| 489 |
// already doesn't exist |
| 490 |
return nil |
| 491 |
} |
| 492 |
if srcFi.IsDir() && targetFi.IsDir() { |
| 493 |
// directories are merged, not replaced |
| 494 |
return nil |
| 495 |
} |
| 496 |
return os.RemoveAll(target) |
| 497 |
} |
| 498 |
|
| 499 |
// Delayed creation of parent directories when a file or dir matches an include |
| 500 |
// pattern. |
| 501 |
func (c *copier) createParentDirs(src string, overwriteTargetMetadata bool) error { |
| 502 |
for i, parentDir := range c.parentDirs { |
| 503 |
if parentDir.copied { |
| 504 |
continue |
| 505 |
} |
| 506 |
|
| 507 |
fi, err := os.Stat(parentDir.srcPath) |
| 508 |
if err != nil { |
| 509 |
return errors.Wrapf(err, "failed to stat %s", src) |
| 510 |
} |
| 511 |
if !fi.IsDir() { |
| 512 |
return errors.Errorf("%s is not a directory", parentDir.srcPath) |
| 513 |
} |
| 514 |
|
| 515 |
created, err := copyDirectoryOnly(parentDir.dstPath, fi, overwriteTargetMetadata) |
| 516 |
if err != nil { |
| 517 |
return err |
| 518 |
} |
| 519 |
if created { |
| 520 |
if err := c.copyFileInfo(fi, parentDir.srcPath, parentDir.dstPath); err != nil { |
| 521 |
return errors.Wrap(err, "failed to copy file info") |
| 522 |
} |
| 523 |
|
| 524 |
if err := copyXAttrs(parentDir.dstPath, parentDir.srcPath, c.xattrErrorHandler); err != nil { |
| 525 |
return errors.Wrap(err, "failed to copy xattrs") |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
c.parentDirs[i].copied = true |
| 530 |
} |
| 531 |
return nil |
| 532 |
} |
| 533 |
|
| 534 |
func (c *copier) copyDirectory( |
| 535 |
ctx context.Context, |
| 536 |
src string, |
| 537 |
srcComponents string, |
| 538 |
dst string, |
| 539 |
stat os.FileInfo, |
| 540 |
overwriteTargetMetadata bool, |
| 541 |
include bool, |
| 542 |
includeMatchInfo patternmatcher.MatchInfo, |
| 543 |
excludeMatchInfo patternmatcher.MatchInfo, |
| 544 |
) (bool, error) { |
| 545 |
if !stat.IsDir() { |
| 546 |
return false, errors.Errorf("source is not directory") |
| 547 |
} |
| 548 |
|
| 549 |
created := false |
| 550 |
|
| 551 |
parentDir := parentDir{ |
| 552 |
srcPath: src, |
| 553 |
dstPath: dst, |
| 554 |
} |
| 555 |
|
| 556 |
// If this directory passed include/exclude matching directly, go ahead |
| 557 |
// and create the directory. Otherwise, delay to handle include |
| 558 |
// patterns like a/*/c where we do not want to create a/b until we |
| 559 |
// encounter a/b/c. |
| 560 |
if include { |
| 561 |
var err error |
| 562 |
created, err = copyDirectoryOnly(dst, stat, overwriteTargetMetadata) |
| 563 |
if err != nil { |
| 564 |
return created, err |
| 565 |
} |
| 566 |
if created || overwriteTargetMetadata { |
| 567 |
if err := c.notifyChange(dst, stat); err != nil { |
| 568 |
return created, err |
| 569 |
} |
| 570 |
} |
| 571 |
parentDir.copied = true |
| 572 |
} |
| 573 |
|
| 574 |
c.parentDirs = append(c.parentDirs, parentDir) |
| 575 |
|
| 576 |
defer func() { |
| 577 |
c.parentDirs = c.parentDirs[:len(c.parentDirs)-1] |
| 578 |
}() |
| 579 |
|
| 580 |
fis, err := os.ReadDir(src) |
| 581 |
if err != nil { |
| 582 |
return false, errors.Wrapf(err, "failed to read %s", src) |
| 583 |
} |
| 584 |
|
| 585 |
for _, fi := range fis { |
| 586 |
if err := c.copy( |
| 587 |
ctx, |
| 588 |
filepath.Join(src, fi.Name()), filepath.Join(srcComponents, fi.Name()), |
| 589 |
filepath.Join(dst, fi.Name()), |
| 590 |
true, includeMatchInfo, excludeMatchInfo, |
| 591 |
); err != nil { |
| 592 |
return false, err |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
return created, nil |
| 597 |
} |
| 598 |
|
| 599 |
func copyDirectoryOnly(dst string, stat os.FileInfo, overwriteTargetMetadata bool) (bool, error) { |
| 600 |
if st, err := os.Lstat(dst); err != nil { |
| 601 |
if !os.IsNotExist(err) { |
| 602 |
return false, err |
| 603 |
} |
| 604 |
if err := os.Mkdir(dst, stat.Mode()); err != nil { |
| 605 |
return false, errors.Wrapf(err, "failed to mkdir %s", dst) |
| 606 |
} |
| 607 |
return true, nil |
| 608 |
} else if !st.IsDir() { |
| 609 |
return false, errors.Errorf("cannot copy to non-directory: %s", dst) |
| 610 |
} else if overwriteTargetMetadata { |
| 611 |
if err := os.Chmod(dst, stat.Mode()); err != nil { |
| 612 |
return false, errors.Wrapf(err, "failed to chmod on %s", dst) |
| 613 |
} |
| 614 |
} |
| 615 |
return false, nil |
| 616 |
} |
| 617 |
|
| 618 |
func ensureEmptyFileTarget(dst string) error { |
| 619 |
fi, err := os.Lstat(dst) |
| 620 |
if err != nil { |
| 621 |
if os.IsNotExist(err) { |
| 622 |
return nil |
| 623 |
} |
| 624 |
return errors.Wrap(err, "failed to lstat file target") |
| 625 |
} |
| 626 |
if fi.IsDir() { |
| 627 |
return errors.Errorf("cannot replace to directory %s with file", dst) |
| 628 |
} |
| 629 |
return os.Remove(dst) |
| 630 |
} |
| 631 |
|
| 632 |
func containsWildcards(name string) bool { |
| 633 |
isWindows := runtime.GOOS == "windows" |
| 634 |
for i := 0; i < len(name); i++ { |
| 635 |
ch := name[i] |
| 636 |
if ch == '\\' && !isWindows { |
| 637 |
i++ |
| 638 |
} else if ch == '*' || ch == '?' || ch == '[' { |
| 639 |
return true |
| 640 |
} |
| 641 |
} |
| 642 |
return false |
| 643 |
} |
| 644 |
|
| 645 |
func splitWildcards(p string) (d1, d2 string) { |
| 646 |
parts := strings.Split(filepath.Join(p), string(filepath.Separator)) |
| 647 |
var p1, p2 []string |
| 648 |
var found bool |
| 649 |
for _, p := range parts { |
| 650 |
if !found && containsWildcards(p) { |
| 651 |
found = true |
| 652 |
} |
| 653 |
if p == "" { |
| 654 |
p = "/" |
| 655 |
} |
| 656 |
if !found { |
| 657 |
p1 = append(p1, p) |
| 658 |
} else { |
| 659 |
p2 = append(p2, p) |
| 660 |
} |
| 661 |
} |
| 662 |
return filepath.Join(p1...), filepath.Join(p2...) |
| 663 |
} |
| 664 |
|
| 665 |
func resolveWildcards(basePath, comp string) ([]string, error) { |
| 666 |
var out []string |
| 667 |
err := filepath.Walk(basePath, func(path string, info os.FileInfo, err error) error { |
| 668 |
if err != nil { |
| 669 |
return err |
| 670 |
} |
| 671 |
rel, err := rel(basePath, path) |
| 672 |
if err != nil { |
| 673 |
return err |
| 674 |
} |
| 675 |
if rel == "." { |
| 676 |
return nil |
| 677 |
} |
| 678 |
if match, _ := filepath.Match(comp, rel); !match { |
| 679 |
return nil |
| 680 |
} |
| 681 |
out = append(out, path) |
| 682 |
if info.IsDir() { |
| 683 |
return filepath.SkipDir |
| 684 |
} |
| 685 |
return nil |
| 686 |
}) |
| 687 |
if err != nil { |
| 688 |
return nil, err |
| 689 |
} |
| 690 |
return out, nil |
| 691 |
} |
| 692 |
|
| 693 |
// rel makes a path relative to base path. Same as `filepath.Rel` but can also |
| 694 |
// handle UUID paths in windows. |
| 695 |
func rel(basepath, targpath string) (string, error) { |
| 696 |
// filepath.Rel can't handle UUID paths in windows |
| 697 |
if runtime.GOOS == "windows" { |
| 698 |
pfx := basepath + `\` |
| 699 |
if strings.HasPrefix(targpath, pfx) { |
| 700 |
p := strings.TrimPrefix(targpath, pfx) |
| 701 |
if p == "" { |
| 702 |
p = "." |
| 703 |
} |
| 704 |
return p, nil |
| 705 |
} |
| 706 |
} |
| 707 |
return filepath.Rel(basepath, targpath) |
| 708 |
} |
| 709 |
|
| 710 |
func fixCreatedParentDirs(dirs []string, tm *time.Time) error { |
| 711 |
slices.Reverse(dirs) |
| 712 |
for _, d := range dirs { |
| 713 |
if tm != nil { |
| 714 |
if err := Utimes(d, tm); err != nil { |
| 715 |
return err |
| 716 |
} |
| 717 |
} |
| 718 |
} |
| 719 |
return nil |
| 720 |
} |