/
FileIncomingTransport.c
143 lines (121 loc) · 4.9 KB
/
FileIncomingTransport.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* 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
* <http://www.gnu.org/licenses/>.
*
* 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 "transport/file/FileIncomingTransport.h"
char* SymFileIncomingTransport_getIncomingFile(SymFileIncomingTransport *this, char *extension) {
DIR *dir;
struct dirent *dirEntries;
dir = opendir(this->offlineIncomingDir);
SymStringArray *files = SymStringArray_new(NULL);
char *startFilter = SymStringUtils_format("%s-%s", this->remoteNode->nodeGroupId, this->remoteNode->nodeId);
if (dir) {
while ((dirEntries = readdir(dir)) != NULL) {
char *entry = dirEntries->d_name;
if (SymStringUtils_startsWith(entry, startFilter) && SymStringUtils_endsWith(entry, extension)) {
files->add(files, entry);
}
}
closedir(dir);
}
else {
SymLog_error("Failed to open incoming directory '%s'", this->offlineIncomingDir);
}
char *firstFile = NULL;
if (files->size > 0) {
files->sort(files);
firstFile = files->get(files, 0);
}
// seems we have stack memory here for firstFile.
// This memory gets overwritten after it's returned.
if (firstFile) {
firstFile = SymStringUtils_format("%s", firstFile);
}
free(startFilter);
files->destroy(files);
return firstFile;
}
long SymFileIncomingTransport_process(SymFileIncomingTransport *this, SymDataProcessor *processor) {
FILE *file;
int BUFFER_SIZE = 2048;
char inputBuffer[BUFFER_SIZE];
char *fileName = SymFileIncomingTransport_getIncomingFile(this, ".csv");
if (!fileName || SymStringUtils_isBlank(fileName)) {
SymLog_info("No incoming files found at '%s'", this->offlineIncomingDir);
return SYM_TRANSPORT_SC_SERVICE_UNAVAILABLE;
}
char *path = SymStringUtils_format("%s/%s", this->offlineIncomingDir, fileName);
file = fopen(path,"r");
if (!file) {
SymLog_warn("Failed to load file '%s'", path);
return SYM_TRANSPORT_SC_SERVICE_UNAVAILABLE;
}
processor->open(processor);
unsigned short success = 1;
int count;
while ((count = fread(inputBuffer, sizeof(char), BUFFER_SIZE, file)) > 0) {
int size = processor->process(processor, inputBuffer, sizeof(char), count);
if (size == 0) {
SymLog_warn("Failed to process file %s", this->offlineIncomingDir);
success = 0;
break;
}
}
processor->close(processor);
fclose(file);
if (success) {
if (SymStringUtils_isNotBlank(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);
}
} else {
int result = remove(path);
if (result) {
SymLog_warn("Failed to delete '%s'", path);
}
}
} else if (SymStringUtils_isNotBlank(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);
}
}
return SYM_TRANSPORT_OK;
}
void SymFileIncomingTransport_destroy(SymFileIncomingTransport *this) {
free(this);
}
SymFileIncomingTransport * SymFileIncomingTransport_new(SymFileIncomingTransport *this, SymNode *remoteNode, SymNode *localNode,
char *offlineIncomingDir, char *offlineArchiveDir, char *offlineErrorDir) {
if (this == NULL) {
this = (SymFileIncomingTransport *) calloc(1, sizeof(SymFileIncomingTransport));
}
SymIncomingTransport *super = &this->super;
super->process = (void *) &SymFileIncomingTransport_process;
super->destroy = (void *) &SymFileIncomingTransport_destroy;
this->remoteNode = remoteNode;
this->localNode = localNode;
this->offlineIncomingDir = offlineIncomingDir;
this->offlineArchiveDir = offlineArchiveDir;
this->offlineErrorDir = offlineErrorDir;
return this;
}