Exemplo n.º 1
0
func CopySingle(srcFs filesystem.FileSystem, srcRaw string, destFs filesystem.FileSystem, destRaw string) error {
	fromFile, _, err := utils.MakeFile(srcRaw)
	toFile := utils.MkToFile(srcRaw, destRaw, fromFile)

	if err != nil {
		logOutput("Error opening dest file: %v", err)
		return fmt.Errorf("Error opening dest file: %v", err)
	}

	if fromFile.IsDir() {
		logOutput("Mkdir %s -> %s\n", fromFile.Path(), toFile.Path())
		destFs.MkDir(toFile)
	} else {
		logOutput("Copying file: %s -> %s\n", fromFile.Path(), toFile.Path())
		bytes, err := srcFs.Read(fromFile)
		err = destFs.Write(toFile, bytes, fromFile.Mode())
		if err != nil {
			logOutput("Error copying file %s: %v", fromFile.Path(), err)
		}
	}

	return nil
}
Exemplo n.º 2
0
func Sync(srcRaw string, destRaw string, opts *Options) error {
	options = opts

	// Remove from src/dest strings
	src := utils.ExtractURL(srcRaw).Path
	dest := utils.ExtractURL(destRaw).Path

	// Get a handle on the to/from Filesystems
	fromFile, fromFs, err := utils.MakeFile(srcRaw)

	if err != nil {
		return err
	}
	if fromFile.IsDir() {
		toFile, toFs, err := utils.MakeFile(destRaw)

		var leftMap filesystem.FileMap
		var rightMap filesystem.FileMap
		var done sync.WaitGroup
		done.Add(2)
		go func() {
			leftMap = fromFs.FileMap(fromFile)
			done.Done()
		}()
		go func() {
			rightMap = toFs.FileMap(toFile)
			done.Done()
		}()
		done.Wait()
		diff, err := filesystem.FileMapDiff(
			leftMap, rightMap, filesystem.ModifiedComparator)

		if err == nil {
			for _, file := range diff {

				if ignoreFile(file.Path(), options.Exclude) {
					continue
				}
				toFile = utils.MkToFile(src, dest, file)

				if err == nil {
					if file.IsDir() {
						logOutput("Mkdir: %s -> %s\n", file.Path(), toFile.Path())
						toFs.MkDir(toFile)
					} else {
						logOutput("Copying file: %s -> %s\n", file.Path(), toFile.Path())
						bytes, err := fromFs.Read(file)
						err = toFs.Write(toFile, bytes, file.Mode())
						if err != nil {
							logOutput("Error copying file %s: %v", file.Path(), err)
						}
					}
				}
			}
		} else {
			logOutput("Error: %v\n", err)
		}
	} else {
		toFile := utils.MkToFile(src, dest, fromFile)
		toFs, err := utils.GetFileSystemFromFile(destRaw)
		if err != nil {
			logOutput("Error opening dest file: %v", err)
			return fmt.Errorf("Error opening dest file: %v", err)
		}

		bytes, err := fromFs.Read(fromFile)
		if err != nil {
			logOutput("Error reading from source file: %s", err.Error())
			return fmt.Errorf("Error reading source file: %v", err)
		}
		err = toFs.Write(toFile, bytes, toFile.Mode())
		if err != nil {
			logOutput("Error writing to remote path: %s", err.Error())
			return fmt.Errorf("Error write to remote path: %v", err)
		}
	}
	return err
}