Merge remote-tracking branch 'syncthing/pr/1373' into fix-1373

* syncthing/pr/1373:
  Alter files into directories and the other way around
This commit is contained in:
Jakob Borg 2015-03-01 10:55:34 +01:00
commit af5c36d2a8
2 changed files with 59 additions and 4 deletions

View File

@ -91,10 +91,22 @@ func testFileTypeChange(t *testing.T) {
// A directory that we will replace with a file later // A directory that we will replace with a file later
err = os.Mkdir("s1/emptyDirToReplace", 0755)
if err != nil {
t.Fatal(err)
}
// A directory with files that we will replace with a file later
err = os.Mkdir("s1/dirToReplace", 0755) err = os.Mkdir("s1/dirToReplace", 0755)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
fd, err = os.Create("s1/dirToReplace/emptyFile")
if err != nil {
t.Fatal(err)
}
fd.Close()
// Verify that the files and directories sync to the other side // Verify that the files and directories sync to the other side
@ -165,15 +177,33 @@ func testFileTypeChange(t *testing.T) {
// Replace file with directory // Replace file with directory
os.RemoveAll("s1/fileToReplace") err = os.RemoveAll("s1/fileToReplace")
if err != nil {
t.Fatal(err)
}
err = os.Mkdir("s1/fileToReplace", 0755) err = os.Mkdir("s1/fileToReplace", 0755)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
// Replace directory with file // Replace empty directory with file
os.RemoveAll("s1/dirToReplace") err = os.RemoveAll("s1/emptyDirToReplace")
if err != nil {
t.Fatal(err)
}
fd, err = os.Create("s1/emptyDirToReplace")
if err != nil {
t.Fatal(err)
}
fd.Close()
// Clear directory and replace with file
err = os.RemoveAll("s1/dirToReplace")
if err != nil {
t.Fatal(err)
}
fd, err = os.Create("s1/dirToReplace") fd, err = os.Create("s1/dirToReplace")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -23,6 +23,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"math/rand" "math/rand"
"os" "os"
@ -113,8 +114,10 @@ func alterFiles(dir string) error {
return nil return nil
} }
info, err = os.Stat(path);
if err != nil { if err != nil {
return err // Something we deleted while walking. Ignore.
return nil
} }
if strings.HasPrefix(filepath.Base(path), "test-") { if strings.HasPrefix(filepath.Base(path), "test-") {
@ -166,6 +169,28 @@ func alterFiles(dir string) error {
if err != nil { if err != nil {
return err return err
} }
case r < 0.3 && comps > 2 && rand.Float64() < 0.2:
if !info.Mode().IsRegular() {
err = removeAll(path)
if err != nil {
return err
}
d1 := []byte("I used to be a dir: "+path)
err := ioutil.WriteFile(path, d1, 0644)
if err != nil {
return err
}
} else {
err := os.Remove(path)
if err != nil {
return err
}
err = os.MkdirAll(path, 0755)
if err != nil {
return err
}
generateFiles(path, 100, 20, "../LICENSE")
}
case r < 0.3 && comps > 1 && (info.Mode().IsRegular() || rand.Float64() < 0.2): case r < 0.3 && comps > 1 && (info.Mode().IsRegular() || rand.Float64() < 0.2):
rpath := filepath.Dir(path) rpath := filepath.Dir(path)
if rand.Float64() < 0.2 { if rand.Float64() < 0.2 {