diff --git a/Sources/PrivMXEndpointSwift/Errors/PrivMXEndpointError.swift b/Sources/PrivMXEndpointSwift/Errors/PrivMXEndpointError.swift index 0a8469e..1521c02 100644 --- a/Sources/PrivMXEndpointSwift/Errors/PrivMXEndpointError.swift +++ b/Sources/PrivMXEndpointSwift/Errors/PrivMXEndpointError.swift @@ -268,6 +268,7 @@ public enum PrivMXEndpointError : Error{ /// Falied to delete KVDB Entries. case failedDeletingKvdbEntries(privmx.InternalError) + case failedSyncingFile(privmx.InternalError) /// Gets the Message of the error. /// @@ -384,6 +385,7 @@ public enum PrivMXEndpointError : Error{ .failedCheckingifStringIsBase64(let err), .failedTrimmingString(let err), .failedSplittingString(let err), + .failedSyncingFile(let err), .failedGeneratingBIP39(let err): return String(err.message) } @@ -505,6 +507,7 @@ public enum PrivMXEndpointError : Error{ .failedCheckingifStringIsBase64(let err), .failedTrimmingString(let err), .failedSplittingString(let err), + .failedSyncingFile(let err), .failedGeneratingBIP39(let err): return err.code.value } @@ -625,6 +628,7 @@ public enum PrivMXEndpointError : Error{ .failedCheckingifStringIsBase64(let err), .failedTrimmingString(let err), .failedSplittingString(let err), + .failedSyncingFile(let err), .failedGeneratingBIP39(let err): return String(err.name) } @@ -745,6 +749,7 @@ public enum PrivMXEndpointError : Error{ .failedCheckingifStringIsBase64(let err), .failedTrimmingString(let err), .failedSplittingString(let err), + .failedSyncingFile(let err), .failedGeneratingBIP39(let err): return String(err.description) } @@ -865,6 +870,7 @@ public enum PrivMXEndpointError : Error{ .failedCheckingifStringIsBase64(let err), .failedTrimmingString(let err), .failedSplittingString(let err), + .failedSyncingFile(let err), .failedGeneratingBIP39(let err): if let scope = err.scope.value{ return String(scope) diff --git a/Sources/PrivMXEndpointSwift/Stores/StoreApi.swift b/Sources/PrivMXEndpointSwift/Stores/StoreApi.swift index 96b36d7..5ae2a84 100644 --- a/Sources/PrivMXEndpointSwift/Stores/StoreApi.swift +++ b/Sources/PrivMXEndpointSwift/Stores/StoreApi.swift @@ -460,17 +460,18 @@ public class StoreApi{ /// Writes a chunk of data to an open file on the platform. /// - /// - Parameters: - /// - handle: The handle to the open file. - /// - dataChunk: The chunk of data to be written to the file. + /// - Parameter handle: The handle to the open file. + /// - Parameter dataChunk: The chunk of data to be written to the file. + /// - Parameter truncate: truncate the file from: current pos + dataChunk size /// /// - Throws: `PrivMXEndpointError.failedWritingToFile` if writing to the file fails. public func writeToFile( handle: privmx.StoreFileHandle, - dataChunk: privmx.endpoint.core.Buffer + dataChunk: privmx.endpoint.core.Buffer, + truncate: Bool = false ) throws -> Void{ - let res = api.writeToFile(handle,dataChunk) + let res = api.writeToFile(handle,dataChunk,truncate) guard res.error.value == nil else { throw PrivMXEndpointError.failedWritingToFile(res.error.value!) } @@ -490,6 +491,20 @@ public class StoreApi{ } } + /// Synchronize file handle data with newset data on serwer. + /// + /// - Parameter handle: Store File handle to sync + /// + /// - Throws: if the operation fails. + public func syncFile( + handle: privmx.StoreFileHandle + ) throws -> Void { + let res = api.syncFile(handle) + guard res.error.value == nil else { + throw PrivMXEndpointError.failedSyncingFile(res.error.value!) + } + } + /// Subscribe for the Store events on the given subscription query. /// /// - Parameter subscriptionQueries: list of queries diff --git a/Sources/PrivMXEndpointSwiftNative/NativeStoreApiWrapper.cpp b/Sources/PrivMXEndpointSwiftNative/NativeStoreApiWrapper.cpp index 17c7676..94532bb 100644 --- a/Sources/PrivMXEndpointSwiftNative/NativeStoreApiWrapper.cpp +++ b/Sources/PrivMXEndpointSwiftNative/NativeStoreApiWrapper.cpp @@ -370,7 +370,8 @@ ResultWithError NativeStoreApiWrapper::readFromFile(StoreFileHandl } ResultWithError NativeStoreApiWrapper::writeToFile(StoreFileHandle handle, - const core::Buffer& dataChunk){ + const core::Buffer& dataChunk, + bool truncate = false){ ResultWithError res; try{ getapi()->writeToFile(handle, dataChunk); @@ -501,6 +502,31 @@ ResultWithError NativeStoreApiWrapper::deleteStore(const std::st return res; } +ResultWithError NativeStoreApiWrapper::syncFile(const StoreFileHandle handle){ + ResultWithError res; + try { + getapi()->syncFile(handle); + }catch(core::Exception& err){ + res.error = { + .name = err.getName(), + .code = err.getCode(), + .scope = err.getScope(), + .description = err.getDescription(), + .message = err.what() + }; + }catch (std::exception & err) { + res.error ={ + .name = "std::Exception", + .message = err.what() + }; + }catch (...) { + res.error ={ + .name = "Unknown Exception", + .message = "Failed to work" + }; + } + return res; +} ResultWithError NativeStoreApiWrapper::subscribeFor(const SubscriptionQueryVector& subscriptionQueries){ ResultWithError res; diff --git a/Sources/PrivMXEndpointSwiftNative/include/NativeStoreApiWrapper.hpp b/Sources/PrivMXEndpointSwiftNative/include/NativeStoreApiWrapper.hpp index 2e55089..0319150 100644 --- a/Sources/PrivMXEndpointSwiftNative/include/NativeStoreApiWrapper.hpp +++ b/Sources/PrivMXEndpointSwiftNative/include/NativeStoreApiWrapper.hpp @@ -193,7 +193,8 @@ class NativeStoreApiWrapper{ * @return `ResultWithError` structure for error handling. */ ResultWithError writeToFile(const StoreFileHandle handle, - const endpoint::core::Buffer& dataChunk); + const endpoint::core::Buffer& dataChunk, + bool truncate); /** * Reads from an opened file. @@ -238,6 +239,8 @@ class NativeStoreApiWrapper{ */ ResultWithError deleteFile(const std::string& fileId); + ResultWithError syncFile(const StoreFileHandle handle); + /** * Subscribe for the Thread events on the given subscription query. *