First steps -pref-dir

This commit is contained in:
Etienne Charlier
2017-06-05 23:16:11 +02:00
parent 36044e13c0
commit c88e148d59
10 changed files with 194 additions and 31 deletions

View File

@@ -9,6 +9,7 @@ import (
"path"
"io/ioutil"
"reflect"
"os"
)
// Preference stores options for each storage.
@@ -25,9 +26,52 @@ type Preference struct {
var Preferences [] Preference
func LoadPreferences(repository string) (bool) {
// Compute .duplicacy directory path name:
// - if .duplicacy is a directory -> compute absolute path name and return it
// - if .duplicacy is a file -> assumed this file contains the real path name of .duplicacy
// - if pointed directory does not exits... return error
func GetDotDuplicacyPathName( repository string) (duplicacyDirectory string){
dotDuplicacy := path.Join(repository, DUPLICACY_DIRECTORY) //TOKEEP
stat, err := os.Stat(dotDuplicacy)
if err != nil && !os.IsNotExist(err) {
LOG_ERROR("DOT_DUPLICACY_PATH", "Failed to retrieve the information about the directory %s: %v",
repository, err)
return ""
}
if stat != nil && stat.IsDir() {
// $repository/.duplicacy exists and is a directory --> we found the .duplicacy directory
return path.Clean(dotDuplicacy)
}
if stat != nil && stat.Mode().IsRegular() {
b, err := ioutil.ReadFile(dotDuplicacy) // just pass the file name
if err != nil {
LOG_ERROR("DOT_DUPLICACY_PATH", "Failed to read file %s: %v",
dotDuplicacy, err)
return ""
}
dot_duplicacy := string(b) // convert content to a 'string'
stat, err := os.Stat(dot_duplicacy)
if err != nil && !os.IsNotExist(err) {
LOG_ERROR("DOT_DUPLICACY_PATH", "Failed to retrieve the information about the directory %s: %v",
repository, err)
return ""
}
if stat != nil && stat.IsDir() {
// If expression read from .duplicacy file is a directory --> we found the .duplicacy directory
return path.Clean( dot_duplicacy)
}
}
return ""
}
description, err := ioutil.ReadFile(path.Join(repository, DUPLICACY_DIRECTORY, "preferences"))
func LoadPreferences(repository string) (bool) {
duplicacyDirectory := GetDotDuplicacyPathName(repository)
description, err := ioutil.ReadFile(path.Join(duplicacyDirectory, "preferences"))
if err != nil {
LOG_ERROR("PREFERENCE_OPEN", "Failed to read the preference file from repository %s: %v", repository, err)
return false
@@ -53,8 +97,9 @@ func SavePreferences(repository string) (bool) {
LOG_ERROR("PREFERENCE_MARSHAL", "Failed to marshal the repository preferences: %v", err)
return false
}
preferenceFile := path.Join(repository, DUPLICACY_DIRECTORY, "/preferences")
duplicacyDirectory := GetDotDuplicacyPathName(repository)
preferenceFile := path.Join(duplicacyDirectory, "/preferences")
err = ioutil.WriteFile(preferenceFile, description, 0644)
if err != nil {
LOG_ERROR("PREFERENCE_WRITE", "Failed to save the preference file %s: %v", preferenceFile, err)