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
Merge pull request #1022 from qri-io/remove-no-wd
  • Loading branch information
b5 authored Nov 18, 2019
2 parents c6361bf + f00792b commit 7fd1d5c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
30 changes: 30 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,32 @@ 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.
if err = run.ExecCommand("qri remove --revisions=all me/remove_no_wd"); err != nil {
t.Error(err)
}
}
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.Debugf("could not get status 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.Debugf("could not remove %s, maybe already removed or renamed", ref.FSIPath)
err = nil
} else {
return err
}
}
}
} else {
Expand Down

0 comments on commit 7fd1d5c

Please sign in to comment.