本文整理汇总了Golang中github.com/pkg/sftp.Client.Lstat方法的典型用法代码示例。如果您正苦于以下问题:Golang Client.Lstat方法的具体用法?Golang Client.Lstat怎么用?Golang Client.Lstat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/pkg/sftp.Client
的用法示例。
在下文中一共展示了Client.Lstat方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: sftpMkdir
func sftpMkdir(path string, client *sftp.Client, fi os.FileInfo) error {
log.Printf("[DEBUG] sftp: creating dir %s", path)
if err := client.Mkdir(path); err != nil {
// Do not consider it an error if the directory existed
remoteFi, fiErr := client.Lstat(path)
if fiErr != nil || !remoteFi.IsDir() {
return err
}
}
mode := fi.Mode().Perm()
if err := client.Chmod(path, mode); err != nil {
return err
}
return nil
}
示例2: mkdirAll
func mkdirAll(sftpClient *sftp.Client, dir string) error {
if dir == "/" {
// Must always exist
return nil
}
stat, err := sftpClient.Lstat(dir)
if err == nil {
if !stat.IsDir() {
return fmt.Errorf("not a directory: %q", dir)
}
return nil
}
parent := path.Dir(dir)
err = mkdirAll(sftpClient, parent)
if err != nil {
return err
}
err = sftpClient.Mkdir(dir)
if err != nil {
return fmt.Errorf("error creating directory %q over sftp: %v", dir, err)
}
return nil
}
示例3: scp2Dest
// 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
}