From 5a98f4e47c4c03a5871fa42ec6578f533181b1ee Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Mon, 16 Jun 2014 09:33:52 +0200 Subject: [PATCH] Mark repos with missing dir as invalid on startup (fixes #311) --- cmd/syncthing/main.go | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index 475501279..44114c26f 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -279,11 +279,28 @@ func main() { m := model.NewModel(confDir, &cfg, "syncthing", Version) - for _, repo := range cfg.Repositories { +nextRepo: + for i, repo := range cfg.Repositories { if repo.Invalid != "" { continue } + repo.Directory = expandTilde(repo.Directory) + + // Safety check. If the cached index contains files but the repository + // doesn't exist, we have a problem. We would assume that all files + // have been deleted which might not be the case, so abort instead. + + id := fmt.Sprintf("%x", sha1.Sum([]byte(repo.Directory))) + idxFile := filepath.Join(confDir, id+".idx.gz") + if _, err := os.Stat(idxFile); err == nil { + if fi, err := os.Stat(repo.Directory); err != nil || !fi.IsDir() { + cfg.Repositories[i].Invalid = "repo directory missing" + continue nextRepo + } + } + + ensureDir(repo.Directory, -1) m.AddRepo(repo) } @@ -327,29 +344,6 @@ func main() { l.Infoln("Populating repository index") m.LoadIndexes(confDir) - - for _, repo := range cfg.Repositories { - if repo.Invalid != "" { - continue - } - - dir := expandTilde(repo.Directory) - - // Safety check. If the cached index contains files but the repository - // doesn't exist, we have a problem. We would assume that all files - // have been deleted which might not be the case, so abort instead. - - if files, _, _ := m.LocalSize(repo.ID); files > 0 { - if fi, err := os.Stat(dir); err != nil || !fi.IsDir() { - l.Warnf("Configured repository %q has index but directory %q is missing; not starting.", repo.ID, repo.Directory) - l.Fatalf("Ensure that directory is present or remove repository from configuration.") - } - } - - // Ensure that repository directories exist for newly configured repositories. - ensureDir(dir, -1) - } - m.CleanRepos() m.ScanRepos() m.SaveIndexes(confDir)