working on adding files to repo and making sure no duplicates
This commit is contained in:
@@ -33,19 +33,18 @@ func (db *DB) ConfigureDB(dbPath string) error {
|
||||
// CheckIfFileCurrentlyMonitored checks if the file is already being monitored. This is a read-only check
|
||||
// to see whether the file was correctly initialised
|
||||
// (BUG) The hash causes the same file to be in database multiple times!
|
||||
func (db *DB) CheckIfFileCurrentlyMonitored(src string, hash [16]byte) (File, error) {
|
||||
func (db *DB) CheckIfFileCurrentlyMonitored(path string) bool {
|
||||
var file File
|
||||
|
||||
//TODO: check this actually works still (don't need hash passed to this anymore)
|
||||
if err := db.One("Path", src, &file); err != nil {
|
||||
if err := db.One("Path", path, &file); err != nil {
|
||||
if err.Error() != "not found" {
|
||||
db.Err(err).Msg("Error finding file by path")
|
||||
return File{}, err
|
||||
return false
|
||||
}
|
||||
db.Warn().Msg("no file found")
|
||||
return File{}, err
|
||||
return false
|
||||
}
|
||||
return file, nil
|
||||
return true
|
||||
}
|
||||
|
||||
// RetrieveWatchedFiles all files that are in the database as "watched files"
|
||||
|
@@ -15,24 +15,17 @@ import (
|
||||
|
||||
// NewManager creates a new manager interface that contains all the needed information to make changes to the repo
|
||||
// rootPath is passed by client or server to let the manager know where to look for the .gvc folder and all the components needed
|
||||
func NewManager(rootDir string, version string, dbPath string, informer chan OperatingMessage, log *zerolog.Logger) (*Manager, error) {
|
||||
func NewManager(rootDir string, version string, dbPath string, informer chan OperatingMessage, dirPaths *FilePaths, log *zerolog.Logger) (*Manager, error) {
|
||||
log.Info().Msg("Creating new Manager...")
|
||||
dirPaths, err := checkPaths(rootDir)
|
||||
if err != nil {
|
||||
return &Manager{}, err
|
||||
}
|
||||
|
||||
// Create new patcher
|
||||
patcher := engine.Patcher{
|
||||
log,
|
||||
dirPaths.KeyFolder,
|
||||
dirPaths.DownloadFolder,
|
||||
dirPaths.SyncFolder,
|
||||
dirPaths.ThumbFolder,
|
||||
dirPaths.DiffFolder,
|
||||
}
|
||||
if err != nil {
|
||||
log.Fatal().Msgf("Error creating a patcher %s", err)
|
||||
return &Manager{}, err
|
||||
Logger: log,
|
||||
KeyFolder: dirPaths.KeyFolder,
|
||||
DownloadFolder: dirPaths.DownloadFolder,
|
||||
SyncFolder: dirPaths.SyncFolder,
|
||||
ThumbFolder: dirPaths.ThumbFolder,
|
||||
DiffFolder: dirPaths.DiffFolder,
|
||||
}
|
||||
gvcDB, err := database.OpenOrCreateDB(dbPath, log)
|
||||
if err != nil {
|
||||
@@ -52,7 +45,8 @@ func NewManager(rootDir string, version string, dbPath string, informer chan Ope
|
||||
return &m, nil
|
||||
}
|
||||
|
||||
func checkPaths(rootDir string) (filePaths *FilePaths, err error) {
|
||||
// CheckPaths just checks the .gvc folder structure
|
||||
func CheckPaths(rootDir string) (filePaths *FilePaths, err error) {
|
||||
// checking for the .gvc folder (the client (but not the server) already checks for the .gvc folder, but this checks all subdirects to make sure they are there)
|
||||
rootFolder, err := filepath.Abs(rootDir)
|
||||
if err != nil {
|
||||
@@ -110,6 +104,8 @@ func (m *Manager) AddFileToRepo(relFilePath string) error {
|
||||
//see commsManagment.go
|
||||
// f := NewFileManager()
|
||||
//DELAYED: this feature affects only large files and user experience. It can wait.
|
||||
relFilePath = strings.TrimSpace(relFilePath) //purging any odd spaces TODO: Make sure not needed
|
||||
fmt.Println("Checking for file: ", relFilePath)
|
||||
var tmpFile database.File
|
||||
filename := filepath.Base(relFilePath)
|
||||
var hash [16]byte
|
||||
@@ -123,30 +119,29 @@ func (m *Manager) AddFileToRepo(relFilePath string) error {
|
||||
if hash, err = engine.UniqueFileHash(relFilePath); err != nil {
|
||||
return err
|
||||
}
|
||||
if tmpFile, err = m.dB.CheckIfFileCurrentlyMonitored(relFilePath, hash); err != nil {
|
||||
if strings.Index(err.Error(), "not found") != -1 {
|
||||
//the file wasn't found, this is an ok error
|
||||
m.Info().Msgf("The file was [%s], so continuing to create it in the database", err)
|
||||
} else {
|
||||
return fmt.Errorf("File was not found in repo, please add file first")
|
||||
}
|
||||
tmpFile.CurrentHash = hash
|
||||
tmpFile.Name = filename
|
||||
tmpFile.Path = relFilePath
|
||||
tmpFile.CreatedAt = time.Now()
|
||||
tmpFile.Unique = base64.URLEncoding.EncodeToString([]byte(filename)) + "_" + base64.URLEncoding.EncodeToString((tmpFile.CurrentHash[:])) + "_" + strconv.FormatInt(tmpFile.CreatedAt.Unix(), 10) + "_" + filename
|
||||
//tmpFile.BkpLocation = filepath.Join(m.SyncFolder, tmpFile.Unique)
|
||||
//tmpFile.CurrentBase = tmpFile.BkpLocation
|
||||
//tmpFile.Ignore = false //we can have files in the database that are ignored. TODO: This was initially added so that 'All Files' would show up as a file (its a hack as it adds a dummy to the database)
|
||||
//we should now have a unique name for this file
|
||||
//if needs be, we can find out the real file name from the string
|
||||
//the hash will give us a reasonable indication of the similarity of the files
|
||||
//define filename of backup(s)
|
||||
_, err := m.prepareDatabaseForFile(tmpFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
alreadyTracked := m.dB.CheckIfFileCurrentlyMonitored(relFilePath)
|
||||
if alreadyTracked {
|
||||
return fmt.Errorf("file already found in tracked files, not adding: %s", relFilePath)
|
||||
}
|
||||
tmpFile = database.File{}
|
||||
|
||||
tmpFile.CurrentHash = hash
|
||||
tmpFile.Name = filename
|
||||
tmpFile.Path = relFilePath
|
||||
tmpFile.CreatedAt = time.Now()
|
||||
tmpFile.Unique = base64.URLEncoding.EncodeToString([]byte(filename)) + "_" + base64.URLEncoding.EncodeToString((tmpFile.CurrentHash[:])) + "_" + strconv.FormatInt(tmpFile.CreatedAt.Unix(), 10) + "_" + filename
|
||||
//tmpFile.BkpLocation = filepath.Join(m.SyncFolder, tmpFile.Unique)
|
||||
//tmpFile.CurrentBase = tmpFile.BkpLocation
|
||||
//tmpFile.Ignore = false //we can have files in the database that are ignored. TODO: This was initially added so that 'All Files' would show up as a file (its a hack as it adds a dummy to the database)
|
||||
//we should now have a unique name for this file
|
||||
//if needs be, we can find out the real file name from the string
|
||||
//the hash will give us a reasonable indication of the similarity of the files
|
||||
//define filename of backup(s)
|
||||
_, err = m.prepareDatabaseForFile(tmpFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m.Info().Msgf("added file: %s at path: %s with hash: %s at time: %s", filename, relFilePath, tmpFile.CurrentHash, tmpFile.CreatedAt.String)
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user