在将文件上传到其他地方后,我试图将文件从“目录”复制到“ oldDir”并删除“目录”中的原始文件。除了无法删除原始文件,我可以执行所有步骤。
我检查了文件和根目录的权限代码均为-rwxrwxrwx,这对我删除文件应该没问题。
func UpdateItemAndRemoveOldOne (sConfig sftp.Config) (err error) {
var (
path = "./newDir/"
oldPath = "./oldDir/"
file *s.File
b []byte
config db.Config
)
ids, err := db.FindIDs()
if err != nil {
log.Println("Cannot get the list of items")
return
}
dir, err := sConfig.Client.ReadDir("./newDir")
if err != nil {
log.Println("Cannot read directory")
return
}
oldDir, err := sConfig.Client.ReadDir("./oldDir")
if err != nil {
log.Println("Cannot read old directory")
return
}
for _, f := range dir {
name := f.Name()
exist := false
for _, oldF := range oldDir {
if name == oldF.Name() {
exist = true
break
}
}
if exist {
continue
}
if segments := strings.Split(name, "."); len(segments) != 3 {
log.Println("Invalid file name:", name)
} else if config, err = db.FindConfigByDevID(strings.ToLower(segments[1])); err != nil {
log.Println("Cannot find the item by last 4 char of id, err:", err)
} else if file, err = sConfig.Client.Open(path + name); err != nil {
log.Println("Cannot open "+path+name+" , err:", err)
} else if b, err = ioutil.ReadAll(file); err != nil {
log.Println("Cannot read "+path+name+" , err:", err)
} else if err = closeFile(file); err != nil {
log.Println(file + " cannot closed")
} else if err = upload(getUploadPath(1, config.ID)+name, b); err != nil {
log.Println("Cannot upload file:", name, ", err:", err)
} else if file, err = sConfig.Client.Create(oldPath + name); err != nil {
log.Println("Cannot create file after uploaded:", name, ", err:", err)
} else if _, err = file.Write(b); err != nil {
log.Println("Cannot write file after uploaded:", name, ", err:", err)
} else if err = closeFile(file); err != nil {
log.Println(file + " cannot closed")
} else if _, err = sCongif.Client.Remove(path + name); err != nil {
log.Println("Cannot remove file after uploaded:", name, ", err:", err)
} else {
log.Println("uploaded:", name)
}
}
return
}
我有错误sftp:“权限被拒绝”(SSH_FX_PERMISSION_DENIED) 我尝试仅测试删除功能,并且可以删除一些文件,但是某些文件仍然出现此错误,但是我很确定它们具有相同的文件许可权代码。