diff --git a/GBase/FileHandling/FileHandler.cs b/GBase/FileHandling/FileHandler.cs index 4e1d396..58162dc 100644 --- a/GBase/FileHandling/FileHandler.cs +++ b/GBase/FileHandling/FileHandler.cs @@ -38,12 +38,26 @@ namespace GBase.FileHandling /// Initialize this /// /// The path of the database + /// /// A to cancel the asynchronous operation /// True if successful, false if not - public async Task Init(string path, CancellationToken cancellationToken) + public List Init(string path, string folderName, CancellationToken cancellationToken) { _path = path; - return true; //TODO: initialize existing files + + string directoryPath = Path.Combine(_path, folderName); + if (!Directory.Exists(directoryPath)) + return null; + + string[] files = Directory.GetFiles(directoryPath, $"*.{GBASE_TABLE_FILE_EXTENSION}"); + + foreach (var file in files) + { + FileStream entryFile = File.Open(file, FileMode.OpenOrCreate, FileAccess.ReadWrite); + _files.Add(new GBaseFile(null, entryFile, file)); + } + + return _files; } public IGBaseFile CreateEntryFile(T entry, IGBaseTable table) @@ -55,9 +69,14 @@ namespace GBase.FileHandling //create new entry file string filePath = $"{Path.Combine(directoryPath, entry.ToString())}.{GBASE_TABLE_FILE_EXTENSION}"; - FileStream entryFile = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); //TODO: Stream has to be disposed + + //check if file is already opened + IGBaseFile file = _files.FirstOrDefault(f => f.FilePath.Equals(filePath)); + if (file != null) + return file.UseFile(); - IGBaseFile file = new GBaseFile(entry, entryFile, filePath); + FileStream entryFile = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); + file = new GBaseFile(entry, entryFile, filePath); _files.Add(file); return file.UseFile(); diff --git a/GBase/Interfaces/FileHandling/IFileHandler.cs b/GBase/Interfaces/FileHandling/IFileHandler.cs index 0e9ca47..e0f9497 100644 --- a/GBase/Interfaces/FileHandling/IFileHandler.cs +++ b/GBase/Interfaces/FileHandling/IFileHandler.cs @@ -3,6 +3,7 @@ // Copyright(c) 2020 SimonG. All Rights Reserved. using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -17,9 +18,10 @@ namespace GBase.Interfaces.FileHandling /// Initialize this /// /// The path of the database + /// /// A to cancel the asynchronous operation /// True if successful, false if not - Task Init(string path, CancellationToken cancellationToken); + List Init(string path, string folderName, CancellationToken cancellationToken); IGBaseFile CreateEntryFile(T entry, IGBaseTable table);