Catch os... errors
This commit is contained in:
parent
5ab85372da
commit
4ff0db0246
14 changed files with 123 additions and 29 deletions
|
@ -192,7 +192,10 @@ func SetEngine() (err error) {
|
|||
// WARNING: for serv command, MUST remove the output to os.stdout,
|
||||
// so use log file to instead print to stdout.
|
||||
logPath := path.Join(setting.LogRootPath, "xorm.log")
|
||||
os.MkdirAll(path.Dir(logPath), os.ModePerm)
|
||||
|
||||
if err := os.MkdirAll(path.Dir(logPath), os.ModePerm); err != nil {
|
||||
return fmt.Errorf("Fail to create dir %s: %v", logPath, err)
|
||||
}
|
||||
|
||||
f, err := os.Create(logPath)
|
||||
if err != nil {
|
||||
|
|
|
@ -218,7 +218,11 @@ func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository) (err error
|
|||
|
||||
// Clone base repo.
|
||||
tmpBasePath := path.Join(setting.AppDataPath, "tmp/repos", com.ToStr(time.Now().Nanosecond())+".git")
|
||||
os.MkdirAll(path.Dir(tmpBasePath), os.ModePerm)
|
||||
|
||||
if err := os.MkdirAll(path.Dir(tmpBasePath), os.ModePerm); err != nil {
|
||||
return fmt.Errorf("Fail to create dir %s: %v", tmpBasePath, err)
|
||||
}
|
||||
|
||||
defer os.RemoveAll(path.Dir(tmpBasePath))
|
||||
|
||||
var stderr string
|
||||
|
@ -622,8 +626,11 @@ func (pr *PullRequest) PushToBaseRepo() (err error) {
|
|||
headFile := fmt.Sprintf("refs/pull/%d/head", pr.Index)
|
||||
|
||||
// Remove head in case there is a conflict.
|
||||
os.Remove(path.Join(pr.BaseRepo.RepoPath(), headFile))
|
||||
file := path.Join(pr.BaseRepo.RepoPath(), headFile)
|
||||
|
||||
if err := os.Remove(file); err != nil {
|
||||
return fmt.Errorf("Fail to remove dir %s: %v", path.Join(pr.BaseRepo.RepoPath(), headFile), err)
|
||||
}
|
||||
if err = git.Push(headRepoPath, tmpRemoteName, fmt.Sprintf("%s:%s", pr.HeadBranch, headFile)); err != nil {
|
||||
return fmt.Errorf("Push: %v", err)
|
||||
}
|
||||
|
|
|
@ -558,8 +558,12 @@ func (repo *Repository) SavePatch(index int64, patch []byte) error {
|
|||
if err != nil {
|
||||
return fmt.Errorf("PatchPath: %v", err)
|
||||
}
|
||||
dir := filepath.Dir(patchPath)
|
||||
|
||||
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
||||
return fmt.Errorf("Fail to create dir %s: %v", dir, err)
|
||||
}
|
||||
|
||||
os.MkdirAll(filepath.Dir(patchPath), os.ModePerm)
|
||||
if err = ioutil.WriteFile(patchPath, patch, 0644); err != nil {
|
||||
return fmt.Errorf("WriteFile: %v", err)
|
||||
}
|
||||
|
@ -669,7 +673,10 @@ func MigrateRepository(u *User, opts MigrateRepoOptions) (*Repository, error) {
|
|||
|
||||
migrateTimeout := time.Duration(setting.Git.Timeout.Migrate) * time.Second
|
||||
|
||||
os.RemoveAll(repoPath)
|
||||
if err := os.RemoveAll(repoPath); err != nil {
|
||||
return repo, fmt.Errorf("Fail to remove %s: %v", repoPath, err)
|
||||
}
|
||||
|
||||
if err = git.Clone(opts.RemoteAddr, repoPath, git.CloneRepoOptions{
|
||||
Mirror: true,
|
||||
Quiet: true,
|
||||
|
@ -680,7 +687,11 @@ func MigrateRepository(u *User, opts MigrateRepoOptions) (*Repository, error) {
|
|||
|
||||
wikiRemotePath := wikiRemoteURL(opts.RemoteAddr)
|
||||
if len(wikiRemotePath) > 0 {
|
||||
os.RemoveAll(wikiPath)
|
||||
|
||||
if err := os.RemoveAll(wikiPath); err != nil {
|
||||
return repo, fmt.Errorf("Fail to remove %s: %v", wikiPath, err)
|
||||
}
|
||||
|
||||
if err = git.Clone(wikiRemotePath, wikiPath, git.CloneRepoOptions{
|
||||
Mirror: true,
|
||||
Quiet: true,
|
||||
|
@ -902,7 +913,11 @@ func initRepository(e Engine, repoPath string, u *User, repo *Repository, opts C
|
|||
|
||||
// Initialize repository according to user's choice.
|
||||
if opts.AutoInit {
|
||||
os.MkdirAll(tmpDir, os.ModePerm)
|
||||
|
||||
if err := os.MkdirAll(tmpDir, os.ModePerm); err != nil {
|
||||
return fmt.Errorf("Fail to create dir %s: %v", tmpDir, err)
|
||||
}
|
||||
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
if err = prepareRepoCommit(repo, tmpDir, repoPath, opts); err != nil {
|
||||
|
@ -1198,7 +1213,12 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) error
|
|||
}
|
||||
|
||||
// Rename remote repository to new path and delete local copy.
|
||||
os.MkdirAll(UserPath(newOwner.Name), os.ModePerm)
|
||||
dir := UserPath(newOwner.Name)
|
||||
|
||||
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
||||
return fmt.Errorf("Fail to create dir %s: %v", dir, err)
|
||||
}
|
||||
|
||||
if err = os.Rename(RepoPath(owner.Name, repo.Name), RepoPath(newOwner.Name, repo.Name)); err != nil {
|
||||
return fmt.Errorf("rename repository directory: %v", err)
|
||||
}
|
||||
|
|
|
@ -104,7 +104,11 @@ func (repo *Repository) UpdateRepoFile(doer *User, opts UpdateRepoFileOptions) (
|
|||
localPath := repo.LocalCopyPath()
|
||||
oldFilePath := path.Join(localPath, opts.OldTreeName)
|
||||
filePath := path.Join(localPath, opts.NewTreeName)
|
||||
os.MkdirAll(path.Dir(filePath), os.ModePerm)
|
||||
dir := path.Dir(filePath)
|
||||
|
||||
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
||||
return fmt.Errorf("Fail to create dir %s: %v", dir, err)
|
||||
}
|
||||
|
||||
// If it's meant to be a new file, make sure it doesn't exist.
|
||||
if opts.IsNewFile {
|
||||
|
@ -185,7 +189,12 @@ func (repo *Repository) GetDiffPreview(branch, treePath, content string) (diff *
|
|||
|
||||
localPath := repo.LocalCopyPath()
|
||||
filePath := path.Join(localPath, treePath)
|
||||
os.MkdirAll(filepath.Dir(filePath), os.ModePerm)
|
||||
dir := filepath.Dir(filePath)
|
||||
|
||||
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
||||
return nil, fmt.Errorf("Fail to create dir %s: %v", dir, err)
|
||||
}
|
||||
|
||||
if err = ioutil.WriteFile(filePath, []byte(content), 0666); err != nil {
|
||||
return nil, fmt.Errorf("WriteFile: %v", err)
|
||||
}
|
||||
|
@ -475,7 +484,10 @@ func (repo *Repository) UploadRepoFiles(doer *User, opts UploadRepoFileOptions)
|
|||
|
||||
localPath := repo.LocalCopyPath()
|
||||
dirPath := path.Join(localPath, opts.TreePath)
|
||||
os.MkdirAll(dirPath, os.ModePerm)
|
||||
|
||||
if err := os.MkdirAll(dirPath, os.ModePerm); err != nil {
|
||||
return fmt.Errorf("Fail to create dir %s: %v", dirPath, err)
|
||||
}
|
||||
|
||||
// Copy uploaded files into repository.
|
||||
for _, upload := range uploads {
|
||||
|
|
|
@ -373,7 +373,12 @@ func addKey(e Engine, key *PublicKey) (err error) {
|
|||
// Calculate fingerprint.
|
||||
tmpPath := strings.Replace(path.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()),
|
||||
"id_rsa.pub"), "\\", "/", -1)
|
||||
os.MkdirAll(path.Dir(tmpPath), os.ModePerm)
|
||||
dir := path.Dir(tmpPath)
|
||||
|
||||
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
||||
return fmt.Errorf("Fail to create dir %s: %v", dir, err)
|
||||
}
|
||||
|
||||
if err = ioutil.WriteFile(tmpPath, []byte(key.Content), 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -392,7 +392,10 @@ func (u *User) UploadAvatar(data []byte) error {
|
|||
return fmt.Errorf("updateUser: %v", err)
|
||||
}
|
||||
|
||||
os.MkdirAll(setting.AvatarUploadPath, os.ModePerm)
|
||||
if err := os.MkdirAll(setting.AvatarUploadPath, os.ModePerm); err != nil {
|
||||
return fmt.Errorf("Fail to create dir %s: %v", setting.AvatarUploadPath, err)
|
||||
}
|
||||
|
||||
fw, err := os.Create(u.CustomAvatarPath())
|
||||
if err != nil {
|
||||
return fmt.Errorf("Create: %v", err)
|
||||
|
@ -409,7 +412,10 @@ func (u *User) UploadAvatar(data []byte) error {
|
|||
// DeleteAvatar deletes the user's custom avatar.
|
||||
func (u *User) DeleteAvatar() error {
|
||||
log.Trace("DeleteAvatar[%d]: %s", u.ID, u.CustomAvatarPath())
|
||||
os.Remove(u.CustomAvatarPath())
|
||||
|
||||
if err := os.Remove(u.CustomAvatarPath()); err != nil {
|
||||
return fmt.Errorf("Fail to remove %s: %v", u.CustomAvatarPath(), err)
|
||||
}
|
||||
|
||||
u.UseCustomAvatar = false
|
||||
if err := UpdateUser(u); err != nil {
|
||||
|
@ -866,9 +872,16 @@ func deleteUser(e *xorm.Session, u *User) error {
|
|||
// FIXME: system notice
|
||||
// Note: There are something just cannot be roll back,
|
||||
// so just keep error logs of those operations.
|
||||
path := UserPath(u.Name)
|
||||
|
||||
os.RemoveAll(UserPath(u.Name))
|
||||
os.Remove(u.CustomAvatarPath())
|
||||
if err := os.RemoveAll(path); err != nil {
|
||||
return fmt.Errorf("Fail to RemoveAll %s: %v", path, err)
|
||||
}
|
||||
|
||||
avatarPath := u.CustomAvatarPath()
|
||||
if err := os.Remove(avatarPath); err != nil {
|
||||
return fmt.Errorf("Fail to remove %s: %v", avatarPath, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -112,7 +112,11 @@ func (repo *Repository) updateWikiPage(doer *User, oldTitle, title, content, mes
|
|||
return ErrWikiAlreadyExist{filename}
|
||||
}
|
||||
} else {
|
||||
os.Remove(path.Join(localPath, oldTitle+".md"))
|
||||
file := path.Join(localPath, oldTitle+".md")
|
||||
|
||||
if err := os.Remove(file); err != nil {
|
||||
return fmt.Errorf("Fail to remove %s: %v", file, err)
|
||||
}
|
||||
}
|
||||
|
||||
// SECURITY: if new file is a symlink to non-exist critical file,
|
||||
|
@ -120,7 +124,10 @@ func (repo *Repository) updateWikiPage(doer *User, oldTitle, title, content, mes
|
|||
// as a new page operation.
|
||||
// So we want to make sure the symlink is removed before write anything.
|
||||
// The new file we created will be in normal text format.
|
||||
os.Remove(filename)
|
||||
|
||||
if err := os.Remove(filename); err != nil {
|
||||
return fmt.Errorf("Fail to remove %s: %v", filename, err)
|
||||
}
|
||||
|
||||
if err = ioutil.WriteFile(filename, []byte(content), 0666); err != nil {
|
||||
return fmt.Errorf("WriteFile: %v", err)
|
||||
|
@ -168,7 +175,10 @@ func (repo *Repository) DeleteWikiPage(doer *User, title string) (err error) {
|
|||
|
||||
title = ToWikiPageName(title)
|
||||
filename := path.Join(localPath, title+".md")
|
||||
os.Remove(filename)
|
||||
|
||||
if err := os.Remove(filename); err != nil {
|
||||
return fmt.Errorf("Fail to remove %s: %v", filename, err)
|
||||
}
|
||||
|
||||
message := "Delete page '" + title + "'"
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue