-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MFW-4961: Update w/ filesystem locator. (#394)
* Update w/ filesystem locator. version: bug
- Loading branch information
1 parent
841c90c
commit 04b3d0d
Showing
5 changed files
with
212 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package settings | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// FilenameLocator finds files on the local filesytem, allowing the | ||
// system to be in hybrid or non-hybid mode and concealing the | ||
// diffrences. | ||
type FilenameLocator struct { | ||
fileExists func(filename string) bool | ||
} | ||
|
||
const ( | ||
// prefix for generic filepaths in hybrid mode | ||
hybridModeGenericPrefix = "/mfw" | ||
|
||
// kernel forwarding mode/BST container mode path prefix. | ||
kernelModeSettingsPrefix = "/etc/config" | ||
|
||
// prefix specifically for config files in hybrid mode | ||
hybridModeSettingsPrefix = "/mnt/flash/mfw-settings" | ||
) | ||
|
||
// FileExists returns true if we can Stat the filename. We don't | ||
// distinguish between various kinds of errors, but do log them, on | ||
// the theory that if you can't Stat the filename, for most purposes, | ||
// that is the same as it not existing, and isn't a common case. | ||
func FileExists(fname string) bool { | ||
if _, err := os.Stat(fname); err != nil { | ||
if !os.IsNotExist(err) { | ||
logger.Warn("Unexpected error code from os.Stat: %s", | ||
err) | ||
} | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func (f *FilenameLocator) findEOSFileName(filename string) (string, error) { | ||
if strings.HasPrefix(filename, kernelModeSettingsPrefix) { | ||
newFileName := strings.Replace( | ||
filename, | ||
kernelModeSettingsPrefix, | ||
hybridModeSettingsPrefix, | ||
1) | ||
if !f.fileExists(newFileName) { | ||
return "", fmt.Errorf("unable to find config file: %s", filename) | ||
} | ||
return newFileName, nil | ||
} else { | ||
newFileName := filepath.Join( | ||
hybridModeGenericPrefix, | ||
filename) | ||
if !f.fileExists(newFileName) { | ||
return "", fmt.Errorf( | ||
"unable to locate file: %s", filename) | ||
} | ||
return newFileName, nil | ||
} | ||
} | ||
|
||
// LocateFile locates the input filename on the filesystem, | ||
// automatically translating it to hybrid mode filenames when needed. | ||
func (f *FilenameLocator) LocateFile(filename string) (string, error) { | ||
if f.fileExists(filename) { | ||
return filename, nil | ||
} | ||
return f.findEOSFileName(filename) | ||
|
||
} | ||
|
||
var defaultLocator = &FilenameLocator{ | ||
fileExists: FileExists, | ||
} | ||
|
||
// LocateFile calls FilenameLocator.LocateFile on the default filename | ||
// locator. | ||
func LocateFile(filename string) (string, error) { | ||
return defaultLocator.LocateFile(filename) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package settings | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/untangle/golang-shared/testing/mocks" | ||
) | ||
|
||
type fileExistsFake struct { | ||
rvals []bool | ||
} | ||
|
||
func (f *fileExistsFake) doesExist(fname string) bool { | ||
rval := f.rvals[0] | ||
f.rvals = f.rvals[1:] | ||
return rval | ||
} | ||
func TestFilenameLocator(t *testing.T) { | ||
existFake := &fileExistsFake{} | ||
locator := FilenameLocator{ | ||
fileExists: existFake.doesExist} | ||
tests := []struct { | ||
filename string | ||
existResults []bool | ||
returnValue string | ||
returnErr error | ||
}{ | ||
{ | ||
filename: "/etc/config/settings.json", | ||
existResults: []bool{false, true}, | ||
returnValue: "/mnt/flash/mfw-settings/settings.json", | ||
}, | ||
{ | ||
filename: "/usr/share/geoip", | ||
existResults: []bool{false, true}, | ||
returnValue: "/mfw/usr/share/geoip", | ||
}, | ||
{ | ||
filename: "/etc/config/appstate.json", | ||
existResults: []bool{false, true}, | ||
returnValue: "/mnt/flash/mfw-settings/appstate.json", | ||
}, | ||
{ | ||
filename: "/etc/config/settings.json", | ||
existResults: []bool{true, true}, | ||
returnValue: "/etc/config/settings.json", | ||
}, | ||
{ | ||
filename: "/etc/config/appstate.json", | ||
existResults: []bool{true, true}, | ||
returnValue: "/etc/config/appstate.json", | ||
}, | ||
{ | ||
filename: "/etc/config/appstate.json", | ||
existResults: []bool{false, false}, | ||
returnValue: "", | ||
returnErr: fmt.Errorf("/etc/config/appstate.json"), | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
existFake.rvals = test.existResults | ||
result, err := locator.LocateFile(test.filename) | ||
assert.Equal(t, result, test.returnValue) | ||
if test.returnErr == nil { | ||
assert.NoError(t, err) | ||
} else { | ||
assert.Regexp(t, test.returnErr.Error(), err.Error(), | ||
"errors should match") | ||
} | ||
} | ||
|
||
} | ||
|
||
func TestFileExists(t *testing.T) { | ||
thisFile, err := os.Executable() | ||
logger = mocks.NewMockLogger() | ||
assert.NoError(t, err) | ||
assert.True(t, FileExists(thisFile)) | ||
assert.False(t, | ||
FileExists("/some-file/that-should/definitely-not/exist-anywhere")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters