From 5cfce6acd0f21ec548f5b167ec767444886f8070 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arturo=20Filast=C3=B2?= Date: Fri, 8 Jan 2021 14:14:14 +0100 Subject: [PATCH] Implement informed consent migration using the config file --- internal/ooni/ooni.go | 22 ++++++++++++++++------ internal/utils/paths.go | 21 +++++++++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/internal/ooni/ooni.go b/internal/ooni/ooni.go index 3f32669..f77c515 100644 --- a/internal/ooni/ooni.go +++ b/internal/ooni/ooni.go @@ -257,14 +257,24 @@ func InitDefaultConfig(home string) (*config.Config, error) { if err != nil { return nil, err } - err = ioutil.WriteFile( - configPath, - data, - 0644, - ) - if err != nil { + if err = ioutil.WriteFile(configPath, data, 0644); err != nil { return nil, err } + // If the user did the informed consent procedure in + // probe-legacy, migrate it over. + if utils.DidLegacyInformedConsent() { + c, err := config.ReadConfig(configPath) + if err != nil { + return nil, err + } + c.Lock() + c.InformedConsent = true + c.Unlock() + if err := c.Write(); err != nil { + return nil, err + } + } + return InitDefaultConfig(home) } return nil, err diff --git a/internal/utils/paths.go b/internal/utils/paths.go index dbc7e4e..568c455 100644 --- a/internal/utils/paths.go +++ b/internal/utils/paths.go @@ -41,6 +41,13 @@ func DBDir(home string, name string) string { return filepath.Join(home, "db", fmt.Sprintf("%s.sqlite3", name)) } +// FileExists returns true if the specified path exists and is a +// regular file. +func FileExists(path string) bool { + stat, err := os.Stat(path) + return err == nil && stat.Mode().IsRegular() +} + // ResultTimestamp is a windows friendly timestamp const ResultTimestamp = "2006-01-02T150405.999999999Z0700" @@ -75,3 +82,17 @@ func GetOONIHome() (string, error) { path := filepath.Join(home, ".ooniprobe") return path, nil } + +// DidLegacyInformedConsent checks if the user did the informed consent procedure in probe-legacy +func DidLegacyInformedConsent() bool { + home, err := homedir.Dir() + if err != nil { + return false + } + + path := filepath.Join(filepath.Join(home, ".ooni"), "initialized") + if FileExists(path) { + return true + } + return false +}