Skip to content

Commit

Permalink
fix(remove): Can remove a dataset if its working directory is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
dustmop committed Nov 15, 2019
1 parent c6361bf commit 9a5b4d8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
28 changes: 28 additions & 0 deletions cmd/remove_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"os"
"path/filepath"
"regexp"
"strings"
"testing"
Expand Down Expand Up @@ -512,3 +513,30 @@ func TestRemoveAllAndKeepFiles(t *testing.T) {
t.Errorf("directory contents (-want +got):\n%s", diff)
}
}

// Test removing a linked dataset after the working directory has already been deleted.
func TestRemoveIfWorkingDirectoryIsNotFound(t *testing.T) {
run := NewFSITestRunner(t, "qri_test_remove_no_wd")
defer run.Delete()

workDir := run.CreateAndChdirToWorkDir("remove_no_wd")

// Init as a linked directory
run.MustExec(t, "qri init --name remove_no_wd --format csv")

// Save the new dataset
run.MustExec(t, "qri save")

// Go up one directory
parentDir := filepath.Dir(workDir)
os.Chdir(parentDir)

// Remove the working directory
err := os.RemoveAll(workDir)
if err != nil {
t.Fatal(err)
}

// Remove all should still work, even though the working directory is gone.
run.MustExec(t, "qri remove --revisions=all me/remove_no_wd")
}
21 changes: 19 additions & 2 deletions lib/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/rpc"
"os"
"path/filepath"
"strings"

"github.com/ghodss/yaml"
"github.com/qri-io/dag"
Expand Down Expand Up @@ -597,7 +598,15 @@ func (r *DatasetRequests) Remove(p *RemoveParams, res *RemoveResponse) error {
if wdErr == fsi.ErrWorkingDirectoryDirty {
return fmt.Errorf("cannot remove from dataset while working directory is dirty")
}
return wdErr
if strings.Contains(wdErr.Error(), "not a linked directory") {
// If the working directory has been removed (or renamed), could not get the
// status. However, don't let this stop the remove operation, since the files
// are already gone, and therefore won't be removed.
log.Debug("could not get stauts for %s, maybe removed or renamed", ref.FSIPath)
wdErr = nil
} else {
return wdErr
}
}
}
} else if p.KeepFiles {
Expand Down Expand Up @@ -650,7 +659,15 @@ func (r *DatasetRequests) Remove(p *RemoveParams, res *RemoveResponse) error {
// Delete the directory
err = os.Remove(ref.FSIPath)
if err != nil {
return err
if strings.Contains(err.Error(), "no such file or directory") {
// If the working directory has already been removed (or renamed), it is
// not an error that this remove operation fails, since we were trying to
// remove them anyway.
log.Debug("could not remove %s, maybe already removed or renamed", ref.FSIPath)
err = nil
} else {
return err
}
}
}
} else {
Expand Down

0 comments on commit 9a5b4d8

Please sign in to comment.