From 8841095509c30c4352339989b692a54188790cce Mon Sep 17 00:00:00 2001 From: mmichalek Date: Thu, 10 Dec 2015 09:20:56 -0500 Subject: [PATCH] Fixes for OfflinePush and OfflinePull. --- symmetric-client-clib/inc/core/SymEngine.h | 2 ++ .../inc/service/NodeCommunicationService.h | 3 +- .../inc/service/OfflinePullService.h | 7 ++-- .../inc/service/OfflinePushService.h | 10 +++--- .../inc/service/PullService.h | 5 ++- .../inc/service/PushService.h | 4 ++- .../transport/file/FileIncomingTransport.h | 1 + .../transport/file/FileOutgoingTransport.h | 1 + symmetric-client-clib/inc/util/FileUtils.h | 32 +++++++++++++++++ symmetric-client-clib/src/core/SymEngine.c | 14 +++++--- .../src/service/NodeCommunicationService.c | 8 +++-- .../src/service/OfflinePullService.c | 8 +++-- .../src/service/OfflinePushService.c | 10 +++--- .../src/service/PullService.c | 6 ++-- .../src/service/PushService.c | 5 +-- .../transport/file/FileIncomingTransport.c | 15 ++++---- .../transport/file/FileOutgoingTransport.c | 31 ++++++++++++---- symmetric-client-clib/src/util/FileUtils.c | 36 +++++++++++++++++++ 18 files changed, 157 insertions(+), 41 deletions(-) create mode 100644 symmetric-client-clib/inc/util/FileUtils.h create mode 100644 symmetric-client-clib/src/util/FileUtils.c diff --git a/symmetric-client-clib/inc/core/SymEngine.h b/symmetric-client-clib/inc/core/SymEngine.h index 0ac69e6b1e..b4a9d850eb 100644 --- a/symmetric-client-clib/inc/core/SymEngine.h +++ b/symmetric-client-clib/inc/core/SymEngine.h @@ -35,6 +35,7 @@ #include "service/ParameterService.h" #include "service/PushService.h" #include "service/NodeService.h" +#include "service/NodeCommunicationService.h" #include "service/PullService.h" #include "service/RegistrationService.h" #include "service/RouterService.h" @@ -72,6 +73,7 @@ typedef struct SymEngine { SymOfflinePushService *offlinePushService; SymOfflinePullService *offlinePullService; SymNodeService *nodeService; + SymNodeCommunicationService *nodeCommunicationService; SymIncomingBatchService *incomingBatchService; SymOutgoingBatchService *outgoingBatchService; SymAcknowledgeService *acknowledgeService; diff --git a/symmetric-client-clib/inc/service/NodeCommunicationService.h b/symmetric-client-clib/inc/service/NodeCommunicationService.h index f52db8e0a9..ede4a2f4af 100644 --- a/symmetric-client-clib/inc/service/NodeCommunicationService.h +++ b/symmetric-client-clib/inc/service/NodeCommunicationService.h @@ -37,6 +37,7 @@ typedef struct SymNodeCommunicationService { void (*destroy)(struct SymNodeCommunicationService *this); } SymNodeCommunicationService; -SymNodeCommunicationService * SymNodeCommunicationService_new(SymNodeCommunicationService *this, SymNodeService * nodeService); +SymNodeCommunicationService * SymNodeCommunicationService_new(SymNodeCommunicationService *this, + SymNodeService * nodeService, SymParameterService *parameterService); #endif diff --git a/symmetric-client-clib/inc/service/OfflinePullService.h b/symmetric-client-clib/inc/service/OfflinePullService.h index 8f5759b7ef..53743aa4ab 100644 --- a/symmetric-client-clib/inc/service/OfflinePullService.h +++ b/symmetric-client-clib/inc/service/OfflinePullService.h @@ -29,6 +29,7 @@ #include "service/DataLoaderService.h" #include "service/RegistrationService.h" #include "service/ConfigurationService.h" +#include "service/NodeCommunicationService.h" #include "util/Map.h" #include "common/Log.h" @@ -37,12 +38,14 @@ typedef struct SymOfflinePullService { SymDataLoaderService *dataLoaderService; SymRegistrationService *registrationService; SymConfigurationService *configurationService; + SymNodeCommunicationService *nodeCommunicationService; SymRemoteNodeStatuses * (*pullData)(struct SymOfflinePullService *this); void (*destroy)(struct SymOfflinePullService *this); } SymOfflinePullService; -SymOfflinePullService * SymOfflinePullService_new(SymOfflinePullService *this, SymNodeService *nodeService, SymDataLoaderService *dataLoaderService, - SymRegistrationService *registrationService, SymConfigurationService *configurationService); +SymOfflinePullService * SymOfflinePullService_new(SymOfflinePullService *this, SymNodeService *nodeService, + SymDataLoaderService *dataLoaderService, SymRegistrationService *registrationService, + SymConfigurationService *configurationService, SymNodeCommunicationService *nodeCommunicationService); #endif diff --git a/symmetric-client-clib/inc/service/OfflinePushService.h b/symmetric-client-clib/inc/service/OfflinePushService.h index 249ea8a34b..2b176429b9 100644 --- a/symmetric-client-clib/inc/service/OfflinePushService.h +++ b/symmetric-client-clib/inc/service/OfflinePushService.h @@ -32,6 +32,7 @@ #include "service/ConfigurationService.h" #include "service/DataExtractorService.h" #include "service/AcknowledgeService.h" +#include "service/NodeCommunicationService.h" #include "transport/TransportManager.h" #include "transport/file/FileOutgoingTransport.h" #include "util/List.h" @@ -45,13 +46,14 @@ typedef struct SymOfflinePushService { SymParameterService *parameterService; SymConfigurationService *configurationService; SymAcknowledgeService *acknowledgeService; + SymNodeCommunicationService *nodeCommunicationService; SymRemoteNodeStatuses * (*pushData)(struct SymOfflinePushService *this); void (*destroy)(struct SymOfflinePushService *); } SymOfflinePushService; -SymOfflinePushService * SymOfflinePushService_new(SymOfflinePushService *this, SymNodeService *nodeService, SymDataExtractorService *dataExtractorService, - SymTransportManager *transportManager, SymParameterService *parameterService, SymConfigurationService *configurationService, - SymAcknowledgeService *acknowledgeService); - +SymOfflinePushService * SymOfflinePushService_new(SymOfflinePushService *this, SymNodeService *nodeService, + SymDataExtractorService *dataExtractorService, SymTransportManager *transportManager, + SymParameterService *parameterService, SymConfigurationService *configurationService, + SymAcknowledgeService *acknowledgeService, SymNodeCommunicationService *nodeCommunicationService); #endif diff --git a/symmetric-client-clib/inc/service/PullService.h b/symmetric-client-clib/inc/service/PullService.h index c5638fd41a..c7712fa5a1 100644 --- a/symmetric-client-clib/inc/service/PullService.h +++ b/symmetric-client-clib/inc/service/PullService.h @@ -29,6 +29,7 @@ #include "service/DataLoaderService.h" #include "service/RegistrationService.h" #include "service/ConfigurationService.h" +#include "service/NodeCommunicationService.h" #include "util/Map.h" #include "common/Log.h" @@ -37,11 +38,13 @@ typedef struct SymPullService { SymDataLoaderService *dataLoaderService; SymRegistrationService *registrationService; SymConfigurationService *configurationService; + SymNodeCommunicationService *nodeCommunicationService; SymRemoteNodeStatuses * (*pullData)(struct SymPullService *this); void (*destroy)(struct SymPullService *this); } SymPullService; SymPullService * SymPullService_new(SymPullService *this, SymNodeService *nodeService, SymDataLoaderService *dataLoaderService, - SymRegistrationService *registrationService, SymConfigurationService *configurationService); + SymRegistrationService *registrationService, SymConfigurationService *configurationService, + SymNodeCommunicationService *nodeCommunicationService); #endif diff --git a/symmetric-client-clib/inc/service/PushService.h b/symmetric-client-clib/inc/service/PushService.h index 4069e206e7..fdb4610b18 100644 --- a/symmetric-client-clib/inc/service/PushService.h +++ b/symmetric-client-clib/inc/service/PushService.h @@ -27,6 +27,7 @@ #include "model/RemoteNodeStatus.h" #include "model/RemoteNodeStatuses.h" #include "service/NodeService.h" +#include "service/NodeCommunicationService.h" #include "service/ParameterService.h" #include "service/ConfigurationService.h" #include "service/DataExtractorService.h" @@ -37,6 +38,7 @@ #include "common/Log.h" typedef struct SymPushService { + SymNodeCommunicationService *nodeCommunicationService; SymNodeService *nodeService; SymDataExtractorService *dataExtractorService; SymTransportManager *transportManager; @@ -49,6 +51,6 @@ typedef struct SymPushService { SymPushService * SymPushService_new(SymPushService *this, SymNodeService *nodeService, SymDataExtractorService *dataExtractorService, SymTransportManager *transportManager, SymParameterService *parameterService, SymConfigurationService *configurationService, - SymAcknowledgeService *acknowledgeService); + SymAcknowledgeService *acknowledgeService, SymNodeCommunicationService *nodeCommunicationService); #endif diff --git a/symmetric-client-clib/inc/transport/file/FileIncomingTransport.h b/symmetric-client-clib/inc/transport/file/FileIncomingTransport.h index 5b33d8a0a9..dbacb9e786 100644 --- a/symmetric-client-clib/inc/transport/file/FileIncomingTransport.h +++ b/symmetric-client-clib/inc/transport/file/FileIncomingTransport.h @@ -23,6 +23,7 @@ #include #include +#include #include #include #include diff --git a/symmetric-client-clib/inc/transport/file/FileOutgoingTransport.h b/symmetric-client-clib/inc/transport/file/FileOutgoingTransport.h index 0995a3c891..4a1a1441f7 100644 --- a/symmetric-client-clib/inc/transport/file/FileOutgoingTransport.h +++ b/symmetric-client-clib/inc/transport/file/FileOutgoingTransport.h @@ -22,6 +22,7 @@ #define SYM_FILEOUTGOINGTRANSPORT_H #include +#include #include "common/Log.h" #include "model/Node.h" #include "service/ParameterService.h" diff --git a/symmetric-client-clib/inc/util/FileUtils.h b/symmetric-client-clib/inc/util/FileUtils.h new file mode 100644 index 0000000000..974c2ba8ab --- /dev/null +++ b/symmetric-client-clib/inc/util/FileUtils.h @@ -0,0 +1,32 @@ +/** + * Licensed to JumpMind Inc under one or more contributor + * license agreements. See the NOTICE file distributed + * with this work for additional information regarding + * copyright ownership. JumpMind Inc licenses this file + * to you under the GNU General Public License, version 3.0 (GPLv3) + * (the "License"); you may not use this file except in compliance + * with the License. + * + * You should have received a copy of the GNU General Public License, + * version 3.0 (GPLv3) along with this library; if not, see + * . + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#ifndef SYM_FILEUTILS_H +#define SYM_FILEUTILS_H + +#include +#include +#include +#include +#include "common/Log.h" + +int SymFileUtils_mkdir(char* dirName); + +#endif diff --git a/symmetric-client-clib/src/core/SymEngine.c b/symmetric-client-clib/src/core/SymEngine.c index 5489afd100..d86a414672 100644 --- a/symmetric-client-clib/src/core/SymEngine.c +++ b/symmetric-client-clib/src/core/SymEngine.c @@ -182,6 +182,7 @@ SymEngine * SymEngine_new( SymEngine *this, SymProperties *properties) { this->offlineTransportManager = SymTransportManagerFactory_create(SYM_PROTOCOL_FILE, this->parameterService); this->nodeService = SymNodeService_new(NULL, this->platform); this->nodeService->lastRestartTime = SymDate_new(NULL); + this->nodeCommunicationService = SymNodeCommunicationService_new(NULL, this->nodeService, this->parameterService); this->incomingBatchService = SymIncomingBatchService_new(NULL, this->platform, this->parameterService); this->outgoingBatchService = SymOutgoingBatchService_new(NULL, this->platform, this->parameterService, this->sequenceService); this->acknowledgeService = SymAcknowledgeService_new(NULL, this->outgoingBatchService, this->platform); @@ -194,12 +195,15 @@ SymEngine * SymEngine_new( SymEngine *this, SymProperties *properties) { this->triggerRouterService, this->parameterService, this->platform); this->registrationService = SymRegistrationService_new(NULL, this->nodeService, this->dataLoaderService, this->parameterService, this->configurationService); - this->pullService = SymPullService_new(NULL, this->nodeService, this->dataLoaderService, this->registrationService, this->configurationService); + this->pullService = SymPullService_new(NULL, this->nodeService, this->dataLoaderService, this->registrationService, + this->configurationService, this->nodeCommunicationService); this->pushService = SymPushService_new(NULL, this->nodeService, this->dataExtractorService, this->transportManager, this->parameterService, - this->configurationService, this->acknowledgeService); - this->offlinePullService = SymOfflinePullService_new(NULL, this->nodeService, this->dataLoaderService, this->registrationService, this->configurationService); - this->offlinePushService = SymOfflinePushService_new(NULL, this->nodeService, this->dataExtractorService, this->offlineTransportManager, this->parameterService, - this->configurationService, this->acknowledgeService); + this->configurationService, this->acknowledgeService, this->nodeCommunicationService); + this->offlinePullService = SymOfflinePullService_new(NULL, this->nodeService, + this->dataLoaderService, this->registrationService, this->configurationService, this->nodeCommunicationService); + this->offlinePushService = SymOfflinePushService_new(NULL, this->nodeService, + this->dataExtractorService, this->offlineTransportManager, this->parameterService, + this->configurationService, this->acknowledgeService, this->nodeCommunicationService); this->purgeService = SymPurgeService_new(NULL, this->parameterService, this->dialect, this->platform); return this; diff --git a/symmetric-client-clib/src/service/NodeCommunicationService.c b/symmetric-client-clib/src/service/NodeCommunicationService.c index 9155828a2b..7ccdec00a8 100644 --- a/symmetric-client-clib/src/service/NodeCommunicationService.c +++ b/symmetric-client-clib/src/service/NodeCommunicationService.c @@ -108,10 +108,10 @@ SymList * SymNodeCommunicationService_list(SymNodeCommunicationService *this, Sy nodesToCommunicateWith = SymNodeCommunicationService_removeOfflineNodes(this, this->nodeService->findNodesToPushTo(this->nodeService)); break; case SYM_COMMUNICATION_TYPE_OFFLN_PUSH: - nodesToCommunicateWith = SymNodeCommunicationService_getNodesToCommunicateWithOffline(this, SYM_COMMUNICATION_TYPE_FILE_PUSH); + nodesToCommunicateWith = SymNodeCommunicationService_getNodesToCommunicateWithOffline(this, SYM_COMMUNICATION_TYPE_PUSH); break; case SYM_COMMUNICATION_TYPE_OFFLN_PULL: - nodesToCommunicateWith = SymNodeCommunicationService_getNodesToCommunicateWithOffline(this, SYM_COMMUNICATION_TYPE_FILE_PULL); + nodesToCommunicateWith = SymNodeCommunicationService_getNodesToCommunicateWithOffline(this, SYM_COMMUNICATION_TYPE_PULL); break; default: nodesToCommunicateWith = SymList_new(NULL); @@ -125,11 +125,13 @@ void SymNodeCommunicationService_destroy(SymNodeCommunicationService *this) { free(this); } -SymNodeCommunicationService * SymNodeCommunicationService_new(SymNodeCommunicationService *this, SymNodeService * nodeService) { +SymNodeCommunicationService * SymNodeCommunicationService_new(SymNodeCommunicationService *this, + SymNodeService * nodeService, SymParameterService *parameterService) { if (this == NULL) { this = (SymNodeCommunicationService *) calloc(1, sizeof(SymNodeCommunicationService)); } this->nodeService = nodeService; + this->parameterService = parameterService; this->list = (void *) &SymNodeCommunicationService_list; this->destroy = (void *) &SymNodeCommunicationService_destroy; return this; diff --git a/symmetric-client-clib/src/service/OfflinePullService.c b/symmetric-client-clib/src/service/OfflinePullService.c index 419c2549f6..8146618ebb 100644 --- a/symmetric-client-clib/src/service/OfflinePullService.c +++ b/symmetric-client-clib/src/service/OfflinePullService.c @@ -48,7 +48,7 @@ SymRemoteNodeStatuses * SymOfflinePullService_pullData(SymOfflinePullService *th identity = this->nodeService->findIdentity(this->nodeService); } if (identity->syncEnabled) { - SymList *nodes = this->nodeService->findNodesToPull(this->nodeService); + SymList *nodes = this->nodeCommunicationService->list(this->nodeCommunicationService, SYM_COMMUNICATION_TYPE_OFFLN_PULL); SymMap *channels = this->configurationService->getChannels(this->configurationService, 0); statuses = SymRemoteNodeStatuses_new(NULL, channels); SymIterator *iter = nodes->iterator(nodes); @@ -68,8 +68,9 @@ void SymOfflinePullService_destroy(SymOfflinePullService *this) { free(this); } -SymOfflinePullService * SymOfflinePullService_new(SymOfflinePullService *this, SymNodeService *nodeService, SymDataLoaderService *dataLoaderService, - SymRegistrationService *registrationService, SymConfigurationService *configurationService) { +SymOfflinePullService * SymOfflinePullService_new(SymOfflinePullService *this, SymNodeService *nodeService, + SymDataLoaderService *dataLoaderService, SymRegistrationService *registrationService, + SymConfigurationService *configurationService, SymNodeCommunicationService *nodeCommunicationService) { if (this == NULL) { this = (SymOfflinePullService *) calloc(1, sizeof(SymOfflinePullService)); } @@ -77,6 +78,7 @@ SymOfflinePullService * SymOfflinePullService_new(SymOfflinePullService *this, S this->dataLoaderService = dataLoaderService; this->registrationService = registrationService; this->configurationService = configurationService; + this->nodeCommunicationService = nodeCommunicationService; this->pullData = (void *) &SymOfflinePullService_pullData; this->destroy = (void *) &SymOfflinePullService_destroy; return this; diff --git a/symmetric-client-clib/src/service/OfflinePushService.c b/symmetric-client-clib/src/service/OfflinePushService.c index be0c71aa37..06881dec39 100644 --- a/symmetric-client-clib/src/service/OfflinePushService.c +++ b/symmetric-client-clib/src/service/OfflinePushService.c @@ -107,7 +107,7 @@ SymRemoteNodeStatuses * SymOfflinePushService_pushData(SymOfflinePushService *th SymRemoteNodeStatuses *statuses = SymRemoteNodeStatuses_new(NULL, channels); SymNode *identity = this->nodeService->findIdentity(this->nodeService); if (identity && identity->syncEnabled) { - SymList *nodes = this->nodeService->findNodesToPushTo(this->nodeService); + SymList *nodes = this->nodeCommunicationService->list(this->nodeCommunicationService, SYM_COMMUNICATION_TYPE_OFFLN_PUSH); if (nodes->size > 0) { SymNodeSecurity *identitySecurity = this->nodeService->findNodeSecurity(this->nodeService, identity->nodeId); if (identitySecurity) { @@ -134,9 +134,10 @@ void SymOfflinePushService_destroy(SymOfflinePushService *this) { free(this); } -SymOfflinePushService * SymOfflinePushService_new(SymOfflinePushService *this, SymNodeService *nodeService, SymDataExtractorService *dataExtractorService, - SymTransportManager *transportManager, SymParameterService *parameterService, SymConfigurationService *configurationService, - SymAcknowledgeService *acknowledgeService) { +SymOfflinePushService * SymOfflinePushService_new(SymOfflinePushService *this, SymNodeService *nodeService, + SymDataExtractorService *dataExtractorService, SymTransportManager *transportManager, + SymParameterService *parameterService, SymConfigurationService *configurationService, + SymAcknowledgeService *acknowledgeService, SymNodeCommunicationService *nodeCommunicationService) { if (this == NULL) { this = (SymOfflinePushService *) calloc(1, sizeof(SymOfflinePushService)); } @@ -146,6 +147,7 @@ SymOfflinePushService * SymOfflinePushService_new(SymOfflinePushService *this, S this->parameterService = parameterService; this->configurationService = configurationService; this->acknowledgeService = acknowledgeService; + this->nodeCommunicationService = nodeCommunicationService; this->pushData = (void *) &SymOfflinePushService_pushData; this->destroy = (void *) &SymOfflinePushService_destroy; return this; diff --git a/symmetric-client-clib/src/service/PullService.c b/symmetric-client-clib/src/service/PullService.c index 0da1b42204..d52bc48b27 100644 --- a/symmetric-client-clib/src/service/PullService.c +++ b/symmetric-client-clib/src/service/PullService.c @@ -53,7 +53,7 @@ SymRemoteNodeStatuses * SymPullService_pullData(SymPullService *this) { identity = this->nodeService->findIdentity(this->nodeService); } if (identity->syncEnabled) { - SymList *nodes = this->nodeService->findNodesToPull(this->nodeService); + SymList *nodes = this->nodeCommunicationService->list(this->nodeCommunicationService, SYM_COMMUNICATION_TYPE_PULL); SymMap *channels = this->configurationService->getChannels(this->configurationService, 0); statuses = SymRemoteNodeStatuses_new(NULL, channels); SymIterator *iter = nodes->iterator(nodes); @@ -74,7 +74,8 @@ void SymPullService_destroy(SymPullService *this) { } SymPullService * SymPullService_new(SymPullService *this, SymNodeService *nodeService, SymDataLoaderService *dataLoaderService, - SymRegistrationService *registrationService, SymConfigurationService *configurationService) { + SymRegistrationService *registrationService, SymConfigurationService *configurationService, + SymNodeCommunicationService *nodeCommunicationService) { if (this == NULL) { this = (SymPullService *) calloc(1, sizeof(SymPullService)); } @@ -82,6 +83,7 @@ SymPullService * SymPullService_new(SymPullService *this, SymNodeService *nodeSe this->dataLoaderService = dataLoaderService; this->registrationService = registrationService; this->configurationService = configurationService; + this->nodeCommunicationService = nodeCommunicationService; this->pullData = (void *) &SymPullService_pullData; this->destroy = (void *) &SymPullService_destroy; return this; diff --git a/symmetric-client-clib/src/service/PushService.c b/symmetric-client-clib/src/service/PushService.c index 7ed52115e3..7d841b7b1a 100644 --- a/symmetric-client-clib/src/service/PushService.c +++ b/symmetric-client-clib/src/service/PushService.c @@ -107,7 +107,7 @@ SymRemoteNodeStatuses * SymPushService_pushData(SymPushService *this, unsigned i SymRemoteNodeStatuses *statuses = SymRemoteNodeStatuses_new(NULL, channels); SymNode *identity = this->nodeService->findIdentity(this->nodeService); if (identity && identity->syncEnabled) { - SymList *nodes = this->nodeService->findNodesToPushTo(this->nodeService); + SymList *nodes = this->nodeCommunicationService->list(this->nodeCommunicationService, SYM_COMMUNICATION_TYPE_PUSH); if (nodes->size > 0) { SymNodeSecurity *identitySecurity = this->nodeService->findNodeSecurity(this->nodeService, identity->nodeId); if (identitySecurity) { @@ -136,7 +136,7 @@ void SymPushService_destroy(SymPushService *this) { SymPushService * SymPushService_new(SymPushService *this, SymNodeService *nodeService, SymDataExtractorService *dataExtractorService, SymTransportManager *transportManager, SymParameterService *parameterService, SymConfigurationService *configurationService, - SymAcknowledgeService *acknowledgeService) { + SymAcknowledgeService *acknowledgeService, SymNodeCommunicationService *nodeCommunicationService) { if (this == NULL) { this = (SymPushService *) calloc(1, sizeof(SymPushService)); } @@ -146,6 +146,7 @@ SymPushService * SymPushService_new(SymPushService *this, SymNodeService *nodeSe this->parameterService = parameterService; this->configurationService = configurationService; this->acknowledgeService = acknowledgeService; + this->nodeCommunicationService = nodeCommunicationService; this->pushData = (void *) &SymPushService_pushData; this->destroy = (void *) &SymPushService_destroy; return this; diff --git a/symmetric-client-clib/src/transport/file/FileIncomingTransport.c b/symmetric-client-clib/src/transport/file/FileIncomingTransport.c index 051bbab597..2e80e0f437 100644 --- a/symmetric-client-clib/src/transport/file/FileIncomingTransport.c +++ b/symmetric-client-clib/src/transport/file/FileIncomingTransport.c @@ -96,25 +96,28 @@ long SymFileIncomingTransport_process(SymFileIncomingTransport *this, SymDataPro processor->close(processor); fclose(file); + file = NULL; if (success) { if (SymStringUtils_isNotBlank(this->offlineArchiveDir)) { + SymFileUtils_mkdir(this->offlineArchiveDir); char *archivePath = SymStringUtils_format("%s/%s", this->offlineArchiveDir, fileName); int result = rename(path, archivePath); - if (result) { - SymLog_warn("Failed to archive '%s' to '%s'", path, archivePath); + if (result != 0) { + SymLog_warn("Failed to archive '%s' to '%s' %s", path, archivePath, strerror(errno)); } } else { int result = remove(path); - if (result) { - SymLog_warn("Failed to delete '%s'", path); + if (result != 0) { + SymLog_warn("Failed to delete '%s' %s", path, strerror(errno)); } } } else if (SymStringUtils_isNotBlank(this->offlineErrorDir)) { + SymFileUtils_mkdir(this->offlineErrorDir); char *errorPath = SymStringUtils_format("%s/%s", this->offlineErrorDir, fileName); int result = rename(path, errorPath); - if (result) { - SymLog_warn("Failed to archive '%s' to '%s'", path, errorPath); + if (result != 0) { + SymLog_warn("Failed to archive '%s' to '%s' %s", path, errorPath, strerror(errno)); } } diff --git a/symmetric-client-clib/src/transport/file/FileOutgoingTransport.c b/symmetric-client-clib/src/transport/file/FileOutgoingTransport.c index c1fe6be8be..9680ec48ba 100644 --- a/symmetric-client-clib/src/transport/file/FileOutgoingTransport.c +++ b/symmetric-client-clib/src/transport/file/FileOutgoingTransport.c @@ -25,35 +25,52 @@ char *SymFileOutgoingTransport_getFileName(SymFileOutgoingTransport *this) { time(¤tTimeMillis); currentTimeMillis *= 1000; - return SymStringUtils_format("%s/%s-%s_to_%s-%s_%ld.tmp", this->offlineOutgoingDir, + return SymStringUtils_format("%s/%s-%s_to_%s-%s_%ld", this->offlineOutgoingDir, this->localNode->nodeGroupId, this->localNode->nodeId, this->remoteNode->nodeGroupId, this->remoteNode->nodeId, currentTimeMillis); } long SymFileOutgoingTransport_process(SymFileOutgoingTransport *this, SymDataProcessor *processor) { + SymFileUtils_mkdir(this->offlineOutgoingDir); + processor->open(processor); int BUFFER_SIZE = 2048; char inputBuffer[BUFFER_SIZE]; - char* fileName = SymFileOutgoingTransport_getFileName(this); - SymLog_debug("Writing file %s", fileName); + char* fileNameBase = SymFileOutgoingTransport_getFileName(this); + char *tmpFileName = SymStringUtils_format("%s%s", fileNameBase, ".tmp"); + char *csvFileName = SymStringUtils_format("%s%s", fileNameBase, ".csv"); + + SymLog_debug("Writing file %s", tmpFileName); long result = SYM_TRANSPORT_SC_SERVICE_UNAVAILABLE; - FILE *file = fopen(SymFileOutgoingTransport_getFileName(this), "w"); + FILE *file = fopen(tmpFileName, "w"); if (file) { int size; while ((size = processor->process(processor, inputBuffer, 1, BUFFER_SIZE)) > 0) { - fprintf(file, "%.*s", size, inputBuffer); + int result = fprintf(file, "%.*s", size, inputBuffer); + if (result < 0) { + SymLog_warn("failed to write to file %s rc=%d", tmpFileName, result); + } } fclose(file); result = SYM_TRANSPORT_OK; } else { - SymLog_error("Failed to open file for writing. %s", fileName); + SymLog_error("Failed to open file for writing. %s", fileNameBase); result = SYM_TRANSPORT_SC_SERVICE_UNAVAILABLE; } - free(fileName); + + SymLog_debug("Rename '%s' to '%s'", tmpFileName, csvFileName); + int renameResult = rename(tmpFileName, csvFileName); + if (renameResult != 0) { + SymLog_warn("Failed to rename '%s' to '%s' %s", tmpFileName, csvFileName, strerror(errno)); + } + + free(csvFileName); + free(tmpFileName); + free(fileNameBase); SymList *batchIds = processor->getBatchesProcessed(processor); SymStringBuilder *buff = SymStringBuilder_new(NULL); diff --git a/symmetric-client-clib/src/util/FileUtils.c b/symmetric-client-clib/src/util/FileUtils.c new file mode 100644 index 0000000000..6e3c9f1d3d --- /dev/null +++ b/symmetric-client-clib/src/util/FileUtils.c @@ -0,0 +1,36 @@ +/** + * Licensed to JumpMind Inc under one or more contributor + * license agreements. See the NOTICE file distributed + * with this work for additional information regarding + * copyright ownership. JumpMind Inc licenses this file + * to you under the GNU General Public License, version 3.0 (GPLv3) + * (the "License"); you may not use this file except in compliance + * with the License. + * + * You should have received a copy of the GNU General Public License, + * version 3.0 (GPLv3) along with this library; if not, see + * . + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#include "util/FileUtils.h" + +int SymFileUtils_mkdir(char* dirName) { + + struct stat st = {0}; + int result = 0; + + if (stat(dirName, &st) == -1) { + result = mkdir(dirName, 0777); + if (result != 0) { + SymLog_warn("Failed to create dir '%s' %s", dirName, strerror(errno)); + } + } + + return result; +}