// StreamCopy copy file from source filesystem to destination filesystem with a stream loop
512292
func StreamCopy(sourcefs, destfs filesystem.Filespace, subPath string) (err error) {
	var (
		reader filesystem.Reader
		writer filesystem.Writer
	)
	if reader, err = sourcefs.Reader(subPath); err != nil {
		return err
	}
	if writer, err = destfs.Writer(subPath); err != nil {
		reader.Close()
		return err
	}
	if _, err = io.Copy(writer, reader); err != nil {
		writer.Close()
		reader.Close()
		return err
	}
	if err = writer.Close(); err != nil {
		reader.Close()
		return err
	}
	if err = reader.Close(); err != nil {
		return err
	}
	return nil
}
1325840
func CopyFile(target, source string) error {
	src, err := os.Open(source)
	if err != nil {
		return errors.Wrapf(err, "failed to open source %s", source)
	}
	defer src.Close()
	tgt, err := os.Create(target)
	if err != nil {
		return errors.Wrapf(err, "failed to open target %s", target)
	}
	defer tgt.Close()

	return copyFileContent(tgt, src)
}
1078199
func (s *StreamArchive) StreamFile(relPath string, fi os.FileInfo, data []byte) error {
	if fi.IsDir() {
		fh, err := zip.FileInfoHeader(fi)
		if err != nil {
			return err
		}
		fh.Name = relPath + "/"
		if _, err = s.Writer.CreateHeader(fh); err != nil {
			return err
		}
	} else {
		fh, err := zip.FileInfoHeader(fi)
		if err != nil {
			return err
		}
		fh.Name = filepath.Join(relPath, fi.Name())
		fh.Method = zip.Deflate
		fw, err := s.Writer.CreateHeader(fh)
		if err != nil {
			return err
		} else if _, err = fw.Write(data); err != nil {
			return err
		}
	}
	return nil
}
245237
func (cst *ClientSessionTransport) Copy(ctx context.Context, id fscache.RemoteIdentifier, dest string, cu filesync.CacheUpdater) error {
	csi, ok := id.(*ClientSessionSourceIdentifier)
	if !ok {
		return errors.New("invalid identifier for client session")
	}

	return filesync.FSSync(ctx, csi.caller, filesync.FSSendRequestOpt{
		IncludePatterns: csi.includePatterns,
		DestDir:         dest,
		CacheUpdater:    cu,
	})
}
761360
func Copy(src, dst string) (err error) {
	var cmd *exec.Cmd
	src, dst = Abs(src), Abs(dst)
	// Determine the command we need to use.
	if runtime.GOOS == "windows" {
		// *NOTE* Not sure this will work correctly, we don't have Windows to test.
		if IsFile(src) {
			cmd = exec.Command("copy", src, dst)
		} else {
			cmd = exec.Command("xcopy", src, dst, "/S /E")
		}
	} else {
		cmd = exec.Command("rsync", "-a", src, dst)
	}

	if stdout, err := cmd.StdoutPipe(); err == nil {
		if stderr, err := cmd.StderrPipe(); err == nil {
			// Start capturing the stdout/err.
			err = cmd.Start()
			io.Copy(os.Stdout, stdout)
			buffer := new(bytes.Buffer)
			buffer.ReadFrom(stderr)
			cmd.Wait()
			if cmd.ProcessState.String() != "exit status 0" {
				err = fmt.Errorf("\t%s\n", buffer.String())
			}
		}
	}
	return
}
214985
func copyFile(src, dst string, relPath string, edit editFunc) error {
	data, err := ioutil.ReadFile(src)
	if err != nil {
		return fmt.Errorf("failed to read source: %s", err)
	}

	if edit != nil {
		data, err = edit(relPath, data)
		if err != nil {
			return fmt.Errorf("edit error: %s", err)
		}

	}
	if data == nil {
		return nil
	}
	return ioutil.WriteFile(dst, data, 0644)
}
440517
func (w *wrapper) RawCopy() Configuration {
	w.mut.Lock()
	defer w.mut.Unlock()
	return w.cfg.Copy()
}
119995
func (d *driver) copy(ctx context.Context, sourcePath string, destPath string) error {
	// S3 can copy objects up to 5 GB in size with a single PUT Object - Copy
	// operation. For larger objects, the multipart upload API must be used.
	//
	// Empirically, multipart copy is fastest with 32 MB parts and is faster
	// than PUT Object - Copy for objects larger than 32 MB.

	fileInfo, err := d.Stat(ctx, sourcePath)
	if err != nil {
		return parseError(sourcePath, err)
	}

	if fileInfo.Size() <= d.MultipartCopyThresholdSize {
		_, err := d.S3.CopyObject(&s3.CopyObjectInput{
			Bucket:               aws.String(d.Bucket),
			Key:                  aws.String(d.s3Path(destPath)),
			ContentType:          d.getContentType(),
			ACL:                  d.getACL(),
			ServerSideEncryption: d.getEncryptionMode(),
			SSEKMSKeyId:          d.getSSEKMSKeyID(),
			StorageClass:         d.getStorageClass(),
			CopySource:           aws.String(d.Bucket + "/" + d.s3Path(sourcePath)),
		})
		if err != nil {
			return parseError(sourcePath, err)
		}
		return nil
	}

	createResp, err := d.S3.CreateMultipartUpload(&s3.CreateMultipartUploadInput{
		Bucket:               aws.String(d.Bucket),
		Key:                  aws.String(d.s3Path(destPath)),
		ContentType:          d.getContentType(),
		ACL:                  d.getACL(),
		SSEKMSKeyId:          d.getSSEKMSKeyID(),
		ServerSideEncryption: d.getEncryptionMode(),
		StorageClass:         d.getStorageClass(),
	})
	if err != nil {
		return err
	}

	numParts := (fileInfo.Size() + d.MultipartCopyChunkSize - 1) / d.MultipartCopyChunkSize
	completedParts := make([]*s3.CompletedPart, numParts)
	errChan := make(chan error, numParts)
	limiter := make(chan struct{}, d.MultipartCopyMaxConcurrency)

	for i := range completedParts {
		i := int64(i)
		go func() {
			limiter <- struct{}{}
			firstByte := i * d.MultipartCopyChunkSize
			lastByte := firstByte + d.MultipartCopyChunkSize - 1
			if lastByte >= fileInfo.Size() {
				lastByte = fileInfo.Size() - 1
			}
			uploadResp, err := d.S3.UploadPartCopy(&s3.UploadPartCopyInput{
				Bucket:          aws.String(d.Bucket),
				CopySource:      aws.String(d.Bucket + "/" + d.s3Path(sourcePath)),
				Key:             aws.String(d.s3Path(destPath)),
				PartNumber:      aws.Int64(i + 1),
				UploadId:        createResp.UploadId,
				CopySourceRange: aws.String(fmt.Sprintf("bytes=%d-%d", firstByte, lastByte)),
			})
			if err == nil {
				completedParts[i] = &s3.CompletedPart{
					ETag:       uploadResp.CopyPartResult.ETag,
					PartNumber: aws.Int64(i + 1),
				}
			}
			errChan <- err
			<-limiter
		}()
	}

	for range completedParts {
		err := <-errChan
		if err != nil {
			return err
		}
	}

	_, err = d.S3.CompleteMultipartUpload(&s3.CompleteMultipartUploadInput{
		Bucket:          aws.String(d.Bucket),
		Key:             aws.String(d.s3Path(destPath)),
		UploadId:        createResp.UploadId,
		MultipartUpload: &s3.CompletedMultipartUpload{Parts: completedParts},
	})
	return err
}
135048
func (f *fsClient) Copy(source string, size int64, progress io.Reader, srcSSE, tgtSSE encrypt.ServerSide, metadata map[string]string) *probe.Error {
	destination := f.PathURL.Path
	rc, e := readFile(source)
	if e != nil {
		err := f.toClientError(e, destination)
		return err.Trace(destination)
	}
	defer rc.Close()

	_, err := f.put(rc, size, map[string][]string{}, progress)
	if err != nil {
		return err.Trace(destination, source)
	}
	return nil
}
1342322
func (e *fileExtractor) copyFile(ctx context.Context, containerID, path string) ([]byte, error) {
	var buf bytes.Buffer
	if err := e.client.CopyFromContainer(ctx, docker.CopyFromContainerOptions{
		Container:    containerID,
		Resource:     path,
		OutputStream: &buf,
	}); err != nil {
		return nil, err
	}

	// Open the tar archive for reading.
	r := bytes.NewReader(buf.Bytes())

	return firstFile(tar.NewReader(r))
}
291849
func Copy(src, dest string) error {
	if IsDir(src) {
		return CopyDirectory(src, dest)
	}
	return CopyFile(src, dest)
}
492866
func (c SimpleFSClient) SimpleFSCopy(ctx context.Context, __arg SimpleFSCopyArg) (err error) {
	err = c.Cli.Call(ctx, "keybase.1.SimpleFS.simpleFSCopy", []interface{}{__arg}, nil)
	return
}
1232656
func copyFile(source, target string) error {
	os.Remove(target)
	fileRead, err := os.Open(source)
	if err != nil {
		return err
	}
	defer fileRead.Close()

	fileWrite, err := os.Create(target)
	if err != nil {
		return err
	}
	defer fileWrite.Close()

	_, err = io.Copy(fileWrite, fileRead)
	if err != nil {
		return err
	}
	return nil
}
298793
func CopyFile(src, dst string) error {
	s, err := os.Open(src)
	if err != nil {
		return err
	}
	defer s.Close()
	d, err := os.Create(dst)
	if err != nil {
		return err
	}
	if _, err := io.Copy(d, s); err != nil {
		d.Close()
		return err
	}
	return d.Close()
}
233518
func Copy(ctx context.Context, src, dst string, fn SessionFunc) (err error) {
	// Check context
	if err = ctx.Err(); err != nil {
		return
	}

	// Stat src
	var statSrc os.FileInfo
	if statSrc, err = os.Stat(src); err != nil {
		err = errors.Wrapf(err, "stating %s failed", src)
		return
	}

	// Dir
	if statSrc.IsDir() {
		if err = filepath.Walk(src, func(path string, info os.FileInfo, errWalk error) (err error) {
			// Check error
			if errWalk != nil {
				err = errWalk
				return
			}

			// Do not process root
			if src == path {
				return
			}

			// Copy
			var p = filepath.Join(dst, strings.TrimPrefix(path, filepath.Clean(src)))
			if err = Copy(ctx, path, p, fn); err != nil {
				err = errors.Wrapf(err, "copying %s to %s failed", path, p)
				return

			}
			return
		}); err != nil {
			return
		}
		return
	}

	// Create ssh session
	var s *ssh.Session
	var c *astidefer.Closer
	if s, c, err = fn(); err != nil {
		err = errors.Wrap(err, "main: creating ssh session failed")
		return
	}
	defer c.Close()

	// Create the destination folder
	if err = s.Run("mkdir -p " + filepath.Dir(dst)); err != nil {
		err = errors.Wrapf(err, "astissh: creating %s failed", filepath.Dir(dst))
		return
	}

	// Open file
	var f *os.File
	if f, err = os.Open(src); err != nil {
		err = errors.Wrapf(err, "astissh: opening %s failed", src)
		return
	}
	defer f.Close()

	// Create ssh session
	if s, c, err = fn(); err != nil {
		err = errors.Wrap(err, "main: creating ssh session failed")
		return
	}
	defer c.Close()

	// Create stdin pipe
	var stdin io.WriteCloser
	if stdin, err = s.StdinPipe(); err != nil {
		err = errors.Wrap(err, "astissh: creating stdin pipe failed")
		return
	}
	defer stdin.Close()

	// Start "scp" command
	astilog.Debugf("astissh: copying %s to %s", filepath.Base(dst), filepath.Dir(dst))
	if err = s.Start("scp -qt " + filepath.Dir(dst)); err != nil {
		err = errors.Wrapf(err, "astissh: scp to %s failed", dst)
		return
	}

	// Send metadata
	if _, err = fmt.Fprintln(stdin, fmt.Sprintf("C%04o", statSrc.Mode().Perm()), statSrc.Size(), filepath.Base(dst)); err != nil {
		err = errors.Wrapf(err, "astissh: sending metadata failed")
		return
	}

	// Copy
	if _, err = astiio.Copy(ctx, f, stdin); err != nil {
		err = errors.Wrap(err, "astissh: copying failed")
		return
	}

	// Send close
	if _, err = fmt.Fprint(stdin, "\x00"); err != nil {
		err = errors.Wrap(err, "astissh: sending close failed")
		return
	}

	// Close stdin
	stdin.Close()

	// Wait
	if err = s.Wait(); err != nil {
		err = errors.Wrap(err, "astissh: waiting failed")
		return
	}
	return
}
796018
func (fs *Filespace) CopyFile(src, dest string) error {
	srcNode, err := fs.root.GetByPath(src)
	if err != nil {
		return err
	}
	if srcNode.IsDir() {
		return fmt.Errorf("Source node must be a file")
	}
	file := srcNode.(*File)
	copiedFile, err := file.Copy()
	if err != nil {
		return err
	}
	destDirName := path.Dir(dest)
	err = fs.MkdirAll(destDirName, 0777)
	if err != nil {
		return err
	}
	destNode, err := fs.root.GetByPath(destDirName)
	if err != nil {
		return err
	}
	if !destNode.IsDir() {
		return fmt.Errorf("Destination path exist and there is a file " + dest)
	}
	copiedFile.name = path.Base(dest)
	var destDir = destNode.(*Dir)
	destDir.AddNode(copiedFile)
	return nil
}
619185
func (d *Diff) DeepCopy() *Diff {
	copy, err := copystructure.Config{Lock: true}.Copy(d)
	if err != nil {
		panic(err)
	}

	return copy.(*Diff)
}