forked from oe5hpm/openBCM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileio.cpp
489 lines (443 loc) · 13.2 KB
/
fileio.cpp
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/***************************************************************
BayCom(R) Packet-Radio fuer IBM PC
OpenBCM-Mailbox
---------------------------------------------------
Filetransfer base class + text- and AutoBIN-methods
---------------------------------------------------
Copyright (C) Florian Radlherr
Taubenbergstr. 32
83627 Warngau
Alle Rechte vorbehalten / All Rights Reserved
***************************************************************/
//19980315 hrx void fileio::send_autobin -> on rbin/rprg: if
// requested file's length is 0 bytes we don't send
// out the file.
//19980430 OE3DZW tmp-files in tmp-path, not in bcm-path
//19990206 OE3DZW added check for missing filename
//19990618 DH3MB Fixed send_autobin(): 0-byte-files were not closed
// Cleaned up recv_autobin()
// Adapted for new CRC classes
//19990703 DH3MB Changed "recv" to "rx" and "send" to "tx"
//19990804 DH3MB Introduced base classes for file transfer stuff
//19990807 DH3MB Extended AutoBIN-statistics
// More informative error messages on text- and autobin-rx
// Changed type of file size variables to off_t
//19991011 F1ULQ Corrected bug in rprg , filename in header was shit
//19991025 OE3DZW shorter status
//19991026 OE3DZW removed F1ULQ's change, users complained about it...
//20000108 OE3DZW fixed /ex for wt
//20000116 DK2UI (dzw) added ui-options
//20000505 DK2UI added set file time to fileio_abin::_rx()
//20000713 DK2UI changes to usefile() because missing f->path
//20000807 DK2UI accept '/EX' only at line start in text rx
//20000910 DK2UI length of file names limited to 50 char's
#include "baycom.h"
#ifdef __LINUX__
#include <utime.h>
#endif
/*---------------------------------------------------------------------------*/
fileio::fileio (void)
//****************************************************************************
//
//****************************************************************************
{
f = (file_info_t *) t_malloc(sizeof(file_info_t), "fio");
*f->fullname = 0;
*f->name = 0;
*f->path = 0;
f->size = 0;
f->offset = 0;
}
/*---------------------------------------------------------------------------*/
fileio::~fileio (void)
//****************************************************************************
//
//****************************************************************************
{
t_free(f);
}
/*---------------------------------------------------------------------------*/
char *fileio::cut_file (char *fname)
//****************************************************************************
//
//****************************************************************************
{
static char filename[FSPATHLEN + FNAMELEN + 1];
char *cp;
strcpy(filename, fname);
if (strlen(filename) > FNAMELEN)
{
if ((cp = strchr(filename, '.')) != NULL)
{
*cp++ = 0;
filename[FNAMELEN - 1 - strlen(cp)] = '~';
filename[FNAMELEN - strlen(cp)] = 0;
strcat(filename, cp);
}
else
{
filename[FNAMELEN - 1] = '~';
filename[FNAMELEN] = 0;
}
}
return filename;
}
/*---------------------------------------------------------------------------*/
void fileio::usefile (char *filename)
//****************************************************************************
//
//****************************************************************************
{
char *cp;
if (! *filename) return;
#ifdef __UNIX__
if (*filename == '/' || *filename == '\\')
strcpy(f->fullname, filename);
else
{
getcwd(f->fullname, FSPATHLEN + FNAMELEN - strlen(filename) - 1);
strcat(f->fullname, "/");
strcat(f->fullname, filename);
}
#else
strcpy(f->fullname, filename);
#endif
killbackslash(f->fullname);
if ((cp = strrchr(f->fullname, '/')) != NULL)
strcpy(f->name, cut_file(cp + 1));
else
strcpy(f->name, cut_file(f->fullname));
strcpy(f->path, f->fullname);
if ((cp = strrchr(f->path, '/')) != NULL)
{
if (cp != f->path) *cp = 0;
else f->path[1] = 0; // root
}
#ifdef __UNIX__
else
*f->fullname = 0;
#endif
if ((f->unixtime = file_isreg(f->fullname)) != 0L)
{
f->exists = 1;
f->pos = 0L;
f->size = filesize(f->fullname) - f->offset;
f->dostime = getdostime(f->unixtime);
#ifdef FEATURE_MDPW
if (f->requires & REQ_MD5HASH)
{
MD5 md5;
md5.readfile(f->fullname, f->offset);
md5.getdigest(f->md5hash);
md5.gethexdigest(f->md5str);
}
#endif
if (f->requires & REQ_CRCTHP)
{
crcthp crc_;
crc_.readfile(f->fullname, f->offset);
f->crc = crc_.result;
}
}
else
f->exists = 0;
f->eraseflag = 0;
}
/*---------------------------------------------------------------------------*/
void fileio::set_offset (off_t offset)
//*************************************************************************
//
// Specifies the position, where to start with the file transmission
// That's nothing to do with resume - the file will be sent as
// if the preciding part of the file did not exist!
//
// This method *MUST* be called before usefile()!
//
//*************************************************************************
{
f->offset = offset;
}
/*---------------------------------------------------------------------------*/
void fileio::doerase (void)
//****************************************************************************
//
//****************************************************************************
{
f->eraseflag = 1;
}
/*---------------------------------------------------------------------------*/
void fileio::tx (void)
//****************************************************************************
//
//****************************************************************************
{
if (! *f->fullname)
{
putf(ms(m_filemissing));
return;
}
if (! f->exists)
{
putf(ms(m_filenotfound), f->fullname);
return;
}
_tx();
}
/*---------------------------------------------------------------------------*/
void fileio::rx (void)
//****************************************************************************
//
//****************************************************************************
{
if (! *f->fullname)
{
putf(ms(m_filemissing));
return;
}
_rx();
}
/*---------------------------------------------------------------------------*/
fileio_text::fileio_text (void) : fileio()
//****************************************************************************
//
//****************************************************************************
{
tail = 0;
f->requires = 0;
}
/*---------------------------------------------------------------------------*/
void fileio_text::settail (signed short int si)
//****************************************************************************
//
//****************************************************************************
{
tail = si;
}
/*---------------------------------------------------------------------------*/
void fileio_text::_tx(void)
//*************************************************************************
//
// Sends a text file
//
//*************************************************************************
{
int a, last = 0;
if ((f->ptr = s_fopen(f->fullname, "lrt")) != NULL)
{
if (f->eraseflag) s_fsetopt(f->ptr, 2);
if (tail < 0 && (f->size) > (-tail))
{
fseek(f->ptr, tail, SEEK_END);
while ((a = fgetc(f->ptr)) != LF && a != EOF); // go to begin of next line
}
while ((a = fgetc(f->ptr)) != EOF)
{
last = a;
putv(a);
waitfor(e_ticsfull);
if (testabbruch()) break;
if (tail > 0 && (f->pos) > tail && a == LF) break;
f->pos++; //wg mancher compileroptimierungen ausserhalb der Abfrage
}
if (last != LF) putv(LF);
s_fclose(f->ptr);
}
else
putf(ms(m_filenotopen), f->fullname);
}
/*---------------------------------------------------------------------------*/
void fileio_text::_rx (void)
//*************************************************************************
//
// Receives a text file
//
//*************************************************************************
{
char *line = b->line;
char *eof;
char tmpname[20];
if (f->exists) putf(ms(m_fileexists));
putf(ms(m_filewaiting), "TEXT", f->fullname);
putflush();
sprintf(tmpname, TEMPPATH "/%s", time2filename(0));
if ((f->ptr = s_fopen(tmpname, "lwt")) != NULL)
{
s_fsetopt(f->ptr, 1);
do
{
getline(line, BUFLEN - 1, 1);
// subst(line, 0x01, CTRLZ); //also accept CTRLA as eof-marker
if (! strnicmp(line, "/EX", 3)) eof = line;
else eof = strchr(line, CTRLZ);
if (eof)
{
*eof = 0;
if (strlen(line))
{
fputs(line, f->ptr);
fputc(LF, f->ptr);
}
}
else
{
fputs(line, f->ptr);
fputc(LF, f->ptr);
}
}
while (! eof);
if (! ftell(f->ptr))
{
s_fclose(f->ptr);
putf(ms(m_filenotstored));
xunlink(tmpname);
}
else
{
s_fclose(f->ptr);
xunlink(f->fullname);
if (file_isreg(f->fullname))
putf(ms(m_filenotrm), tmpname);
else
{
if (xrename(tmpname, f->fullname))
putf(ms(m_filewriteerror), tmpname);
else
putf(ms(m_filestored), f->fullname);
}
}
}
else putf(ms(m_filenotopen), tmpname);
}
/*---------------------------------------------------------------------------*/
fileio_abin::fileio_abin (void) : fileio()
//****************************************************************************
//
//****************************************************************************
{
*autobin_header = 0;
f->requires = REQ_CRCTHP;
}
/*---------------------------------------------------------------------------*/
void fileio_abin::set_header (char *str)
//****************************************************************************
//
//****************************************************************************
{
strncpy(autobin_header, str, LINELEN);
autobin_header[LINELEN] = 0;
}
/*---------------------------------------------------------------------------*/
void fileio_abin::_tx (void)
//*************************************************************************
//
// Sends a file using the AutoBIN protocol
//
//*************************************************************************
{
int a;
unsigned long int bps;
time_t startt, txtime;
if (! f->size)
{
putf(ms(m_filenobytes), f->fullname);
return;
}
if ((f->ptr = s_fopen(f->fullname, "lrb")) != NULL)
{
if (f->eraseflag) s_fsetopt(f->ptr, 2);
fseek(f->ptr, f->offset, SEEK_SET);
if (*autobin_header)
putf("%s", autobin_header);
else
putf("#BIN#%ld#|%05u#$%08lX#%s\n", f->size, f->crc, f->dostime, f->name);
startt = ad_time();
while ((a = fgetc (f->ptr)) != EOF) bputv(a);
txtime = ad_time() - startt;
if (! txtime) txtime = 1L;
bps = f->size / txtime;
putf("\n");
putf(ms(m_rxok), "BIN", f->crc, f->crc, bps << 3);
s_fclose(f->ptr);
}
else putf(ms(m_filenotopen), f->fullname);
}
/*---------------------------------------------------------------------------*/
void fileio_abin::_rx (void)
//*************************************************************************
//
// Receives a file using the AutoBIN protocol
//
//*************************************************************************
{
char *line = b->line;
char tmpname[20];
int a;
off_t flen = 0L, len;
char rxed_crc = 0;
unsigned short int rx_crc = 0;
crcthp crc_;
unsigned long int bps;
time_t startt = ad_time(), rxtime;
unsigned long bintime;
sprintf(tmpname, TEMPPATH "/%s", time2filename(0));
if (f->exists) putf(ms(m_fileexists));
putf(ms(m_filewaiting), "AutoBIN", f->fullname);
putflush();
if ((f->ptr = s_fopen(tmpname, "lwb")) != NULL)
{
s_fsetopt(f->ptr, 1);
// Wait for the #BIN#-Header, ignore empty lines
do getline(line, BUFLEN - 1, 1);
while (! *line);
if (strstr(line, "#BIN#") == line)
{
putf("#OK#\n");
putflush();
// Get the file length
flen = len = atol(line+5);
// Get the CRC
if (strstr(line, "#|"))
{
rx_crc = (unsigned short int) atol(strstr(line, "#|") + 2);
rxed_crc = 1;
}
sscanf(strchr(line, '$'), "$%lX#", &bintime);
while (len--)
{
a = getv();
fputc(a, f->ptr);
crc_.update(a);
}
}
if (! ftell(f->ptr))
{
s_fclose(f->ptr);
putf(ms(m_filenotstored));
xunlink(tmpname);
return;
}
s_fclose(f->ptr);
if (rxed_crc && (rx_crc != crc_.result))
{
putf(ms(m_filecrcerror));
xunlink(tmpname);
return;
}
rxtime = ad_time() - startt;
if (! rxtime) rxtime = 1L;
bps = flen / rxtime;
putf("\n");
putf(ms(m_rxok), "BIN", crc_.result, crc_.result, bps << 3);
if (f->exists && xunlink(f->fullname))
putf(ms(m_filenotrm), tmpname); // DH3MB
else
{
if (xrename(tmpname, f->fullname))
putf(ms(m_filewriteerror), tmpname);
else
setfiletime(f->fullname, bintime);
}
}
else putf(ms(m_filenotopen), tmpname);
}
/*---------------------------------------------------------------------------*/