예제 #1
0
func FileCreate(s *sftp.Client, name string) (*File, error) {
	fd, err := s.Create(name)
	if err != nil {
		return &File{}, err
	}
	return &File{fd: fd}, nil
}
예제 #2
0
// Scp one cpFile to destination.
// If destination is not exist, be regarded as a dir default, then make it.
// If destination is a dir, make file in it.
// If destination is a file, cover it.
func (t *SSHClient) scp2Dest(client *sftp.Client, cpFile *CpFile) (string, bool) {
	filename := cpFile.Filename
	dest := cpFile.Destination
	content := cpFile.Content

	// Dest dir not exist, make it.
	destType := "file"
	if fi, err := client.Lstat(dest); err != nil { // Dest not exist,make as a dir.
		if err := client.Mkdir(dest); err != nil {
			return fmt.Sprintf("mkdir %s, %s", dest, err.Error()), false
		}
	} else {
		if fi.IsDir() == true {
			destType = "dir"
		}
	}
	destF := dest
	if destType == "dir" { // dir, build filepath
		destF = fmt.Sprintf("%s/%s", dest, filename)
	}

	f, err := client.Create(destF)
	if err != nil {
		return fmt.Sprintf("create: %s, %s", destF, err.Error()), false
	}
	if _, err := f.Write(content); err != nil {
		return fmt.Sprintf("write: %s, %s", destF, err.Error()), false
	}
	return fmt.Sprintf("cp: %s, %s => %s", filename, t.host, dest), true
}
예제 #3
0
파일: sftp.go 프로젝트: cnhans/pugo-static
func (ft *SftpTask) uploadAllFiles(client *sftp.Client, ctx *builder.Context) error {
	var (
		createdDirs = make(map[string]bool)
		err         error
	)
	return ctx.Diff.Walk(func(name string, entry *builder.DiffEntry) error {
		rel, _ := filepath.Rel(ctx.DstDir, name)
		rel = filepath.ToSlash(rel)

		if entry.Behavior == builder.DIFF_REMOVE {
			log15.Debug("Deploy.Sftp.Delete", "file", rel)
			return client.Remove(path.Join(ft.opt.Directory, rel))
		}

		// create directory recursive
		dirs := getDirs(path.Dir(rel))
		if len(dirs) > 0 {
			for i := len(dirs) - 1; i >= 0; i-- {
				dir := dirs[i]
				if !createdDirs[dir] {
					if err = client.Mkdir(path.Join(ft.opt.Directory, dir)); err == nil {
						createdDirs[dir] = true
					}
				}
			}
		}

		// upload file
		f, err := os.Open(name)
		if err != nil {
			return err
		}
		defer f.Close()

		f2, err := client.Create(path.Join(ft.opt.Directory, rel))
		if err != nil {
			return err
		}
		defer f2.Close()

		if _, err = io.Copy(f2, f); err != nil {
			return err
		}
		log15.Debug("Deploy.Sftp.Stor", "file", rel)
		return nil
	})
}
예제 #4
0
파일: sftp.go 프로젝트: cnhans/pugo-static
func (ft *SftpTask) uploadDiffFiles(client *sftp.Client, ctx *builder.Context) error {
	return ctx.Diff.Walk(func(name string, entry *builder.DiffEntry) error {
		rel, _ := filepath.Rel(ctx.DstDir, name)
		rel = filepath.ToSlash(rel)

		if entry.Behavior == builder.DIFF_REMOVE {
			log15.Debug("Deploy.Sftp.Delete", "file", rel)
			return client.Remove(path.Join(ft.opt.Directory, rel))
		}

		target := path.Join(ft.opt.Directory, rel)
		if entry.Behavior == builder.DIFF_KEEP {
			if fi, _ := client.Stat(target); fi != nil {
				// entry file should be older than uploaded file
				if entry.Time.Sub(fi.ModTime()).Seconds() < 0 {
					return nil
				}
			}
		}

		dirs := getDirs(path.Dir(rel))
		for i := len(dirs) - 1; i >= 0; i-- {
			client.Mkdir(path.Join(ft.opt.Directory, dirs[i]))
		}

		// upload file
		f, err := os.Open(name)
		if err != nil {
			return err
		}
		defer f.Close()

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

		if _, err = io.Copy(f2, f); err != nil {
			return err
		}
		log15.Debug("Deploy.Sftp.Stor", "file", rel)
		return nil
	})
}
예제 #5
0
func sftpUploadFile(path string, input io.Reader, client *sftp.Client, fi *os.FileInfo) error {
	log.Printf("[DEBUG] sftp: uploading %s", path)

	f, err := client.Create(path)
	if err != nil {
		return err
	}
	defer f.Close()

	if _, err = io.Copy(f, input); err != nil {
		return err
	}

	if fi != nil && (*fi).Mode().IsRegular() {
		mode := (*fi).Mode().Perm()
		err = client.Chmod(path, mode)
		if err != nil {
			return err
		}
	}

	return nil
}
예제 #6
-1
파일: action.go 프로젝트: iwat/sftpsync
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
	}
}
예제 #7
-7
파일: ssh.go 프로젝트: treescale/treescale
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
}
예제 #8
-8
파일: upload.go 프로젝트: artyom/upload
func uploadFile(name, dir string, sc *sftp.Client) error {
	f, err := os.Open(name)
	if err != nil {
		return err
	}
	defer f.Close()
	of, err := sc.Create(path.Join(dir, filepath.Base(name)))
	if err != nil {
		return err
	}
	defer of.Close()
	bufp := copyBufPool.Get().(*[]byte)
	defer copyBufPool.Put(bufp)
	if _, err := io.CopyBuffer(of, f, *bufp); err != nil {
		return err
	}
	return of.Close()
}