Exemplo n.º 1
-1
func putFile(p file, client *sftp.Client, basepath string, dryRun bool) {
	if dryRun {
		if p.offset > 0 {
			log.Info("!APPEND", "path", p.relPath, "size", p.size)
		} else {
			log.Info("!PUT", "path", p.relPath, "size", p.size)
		}
		return
	}

	var remote *sftp.File
	var err error
	if p.offset > 0 {
		log.Info("APPEND", "path", p.relPath, "size", p.size)
		remote, err = client.OpenFile(basepath+"/"+p.relPath, os.O_RDWR|os.O_APPEND)
	} else {
		log.Info("PUT", "path", p.relPath, "size", p.size)
		remote, err = client.Create(basepath + "/" + p.relPath)
	}
	if err != nil {
		log.Warn("PUT", "path", p.relPath, "size", p.size, "err", err)
		return
	}

	defer remote.Close()

	local, err := os.Open(p.path)
	if err != nil {
		log.Warn("PUT", "path", p.relPath, "size", p.size, "err", err)
		return
	}

	defer local.Close()

	if p.offset > 0 {
		_, err := local.Seek(p.offset, os.SEEK_SET)
		if err != nil {
			log.Warn("Seek error", "err", err)
			return
		}
	}

	_, err = io.Copy(remote, local)
	if err != nil {
		log.Warn("PUT", "path", p.relPath, "size", p.size, "err", err)
		return
	}
}
Exemplo n.º 2
-7
func (ssc *SSHConfig) CopyFile(local_path, remote_path string) (err tree_lib.TreeError) {
	var (
		sft       *sftp.Client
		f         *sftp.File
		file_data []byte
	)
	err.From = tree_lib.FROM_SSH_COPY_FILE
	sft, err.Err = sftp.NewClient(ssc.conn)
	if !err.IsNull() {
		return
	}
	defer sft.Close()

	file_data, err.Err = ioutil.ReadFile(local_path)
	if !err.IsNull() {
		return
	}

	f, err.Err = sft.Create(remote_path)
	if !err.IsNull() {
		return
	}

	_, err.Err = f.Write(file_data)
	f.Close()
	sft.Close()
	return
}