Debian Codegraph @

src/burp/split/spit.cpp

raw
1 /*************************************************************************
2 **
3 ** PROGRAM: JRD file split utility program
4 ** MODULE: spit.cpp
5 ** DESCRIPTION: Command line interpreter for backup file split/join
6 ** utility program
7 **
8 **
9 **************************************************************************
10 * The contents of this file are subject to the Interbase Public
11 * License Version 1.0 (the "License"); you may not use this file
12 * except in compliance with the License. You may obtain a copy
13 * of the License at http://www.Inprise.com/IPL.html
14 *
15 * Software distributed under the License is distributed on an
16 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
17 * or implied. See the License for the specific language governing
18 * rights and limitations under the License.
19 *
20 * The Original Code was created by Inprise Corporation
21 * and its predecessors. Portions created by Inprise Corporation are
22 * Copyright (C) Inprise Corporation.
23 *
24 * All Rights Reserved.
25 * Contributor(s): ______________________________________.
26 */
27
28 #include "firebird.h"
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <errno.h>
33 #include "../common/gdsassert.h"
34 #include <stdarg.h>
35
36 #include <fcntl.h>
37 #include <ctype.h>
38 #ifdef TIME_WITH_SYS_TIME
39 # include <sys/time.h>
40 # include <time.h>
41 #else
42 # ifdef HAVE_SYS_TIME_H
43 # include <sys/time.h>
44 # else
45 # include <time.h>
46 # endif
47 #endif
48 #include "../burp/split/spit.h"
49 #include "../common/classes/Switches.h"
50 #include "../burp/std_desc.h"
51 #include "../burp/burpswi.h"
52 #include "../common/os/os_utils.h"
53
54 #ifdef HAVE_UNISTD_H
55 #include <unistd.h>
56 #endif
57
58 #ifdef HAVE_IO_H
59 #include <io.h>
60 #endif
61
62 #ifdef HAVE_LOCALE_H
63 #include <locale.h>
64 #endif
65
66 static const int mode_read = O_RDONLY;
67 static const int mode_write = O_WRONLY | O_CREAT;
68 static const int mask = 0666;
69
70 static DESC open_platf(const char* name, int writeFlag)
71 {
72 #ifdef WIN_NT
73 return CreateFile(name, (writeFlag ? GENERIC_WRITE : GENERIC_READ), 0, NULL,
74 (writeFlag ? CREATE_ALWAYS : OPEN_EXISTING), FILE_ATTRIBUTE_NORMAL, 0);
75 #else
76 return os_utils::open(name, (writeFlag ? mode_write : mode_read), mask);
77 #endif
78 }
79
80 static int read_platf(DESC file, void* buf, int count)
81 {
82 #ifdef WIN_NT
83 DWORD act;
84 if (!ReadFile(file, buf, count, &act, NULL))
85 {
86 return -1;
87 }
88 return act;
89 #else
90 return read(file, buf, count);
91 #endif
92 }
93
94 static int write_platf(DESC file, const void* buf, int count)
95 {
96 #ifdef WIN_NT
97 DWORD act;
98 if (!WriteFile(file, buf, count, &act, NULL))
99 {
100 return -1;
101 }
102 return act;
103 #else
104 return write(file, buf, count);
105 #endif
106 }
107
108 static void close_platf(DESC file)
109 {
110 #ifdef WIN_NT
111 CloseHandle(file);
112 #else
113 close(file);
114 #endif
115 }
116
117 // Definitions for GSPLIT
118 enum gsplit_option
119 {
120 IN_SW_SPIT_0 = 0, // the unknowable switch
121 IN_SW_SPIT_SP = 30, // splits back up files
122 IN_SW_SPIT_JT = 31 // joins back up files
123 };
124
125
126 const static Switches::in_sw_tab_t spit_in_sw_table[] =
127 {
128 {IN_SW_SPIT_SP, 0, "SPLIT_BK_FILE", 0, 0, 0, false, 0, 0, 0},
129 {IN_SW_SPIT_JT, 0, "JOIN_BK_FILE", 0, 0, 0, false, 0, 0, 0},
130 {IN_SW_SPIT_0, 0, NULL, 0, 0, 0, false, 0, 0, 0}
131 };
132
133
134 // backup files header record
135
136 struct header_rec
137 {
138 TEXT name[18];
139 TEXT date_time[30];
140 TEXT text1[11];
141 TEXT num[4];
142 TEXT text2[4];
143 TEXT total[4];
144 TEXT text3[2];
145 TEXT fl_name[MAX_FILE_NM_LEN];
146 };
147
148 const size_t header_rec_len = sizeof(header_rec);
149 static const char* const header_rec_name = "InterBase/gsplit, ";
150
151
152 // backup files structure
153
154 struct b_fil
155 {
156 b_fil *b_fil_next;
157 TEXT *b_fil_name;
158 SLONG b_fil_number;
159 SINT64 b_fil_size;
160 };
161
162 const size_t b_fil_len = sizeof(b_fil);
163
164
165 // local function declarations
166
167 static int conv_ntoc(SLONG, TEXT*);
168 static int free_file_list(b_fil*);
169 static int final_flush_io_buff(const UCHAR*, SLONG, FILE_DESC);
170 static int final_read_and_write(FILE_DESC, FILE_DESC, const TEXT*, SLONG, UCHAR**, bool*);
171 static int flush_io_buff(const UCHAR*, SLONG, FILE_DESC, SINT64, SLONG*, bool*);
172 static int get_file_name(const SCHAR*, SINT64, b_fil**);
173 static int get_file_size(const SCHAR*, const SCHAR*, SINT64*);
174 static int get_function_option(const SCHAR*, gsplit_option*, const SCHAR*, const Switches&);
175 static int gen_multy_bakup_files(b_fil*, FILE_DESC, SLONG);
176 static int set_hdr_str(TEXT*, const TEXT*, SLONG, SLONG);
177 static int join_multy_bakup_files(b_fil*);
178 static void print_clo(const TEXT*);
179 static int read_and_write(FILE_DESC, FILE_DESC, /*const TEXT*,*/ SLONG,
180 SINT64, UCHAR**, bool*, SINT64*, SLONG*);
181 static int read_and_write_for_join(FILE_DESC, const TEXT*, UCHAR*, SLONG, SLONG*);
182 static int write_header(const b_fil*, header_rec, FILE_DESC, TEXT*);
183
184
185
186 /*****************************************************
187 **
188 ** M A I N P R O G R A M
189 **
190 ******************************************************
191 */
192
193 int main( int argc, char* argv[])
194 {
195 #ifdef HAVE_LOCALE_H
196 // Pick up the system locale to allow SYSTEM<->UTF8 conversions
197 setlocale(LC_CTYPE, "");
198 #endif
199
200 const SCHAR* const prog_name = argv[0];
201
202 if (argc < 2)
203 {
204 fprintf(stderr, "%s: No Command Line Option Specified\n", argv[0]);
205 print_clo(prog_name);
206 return FB_FAILURE;
207 }
208
209 // Fields initialization
210
211 b_fil* file_ptr = NULL;
212 b_fil* file_list = NULL;
213 b_fil* prev_file = NULL;
214 SINT64 file_size = -1;
215 gsplit_option sw_replace = IN_SW_SPIT_0;
216
217
218 // Initialize in_sw_table table.
219
220 const Switches switches(spit_in_sw_table, FB_NELEM(spit_in_sw_table), false, false);
221
222 // validating command line options
223
224 SLONG ret_cd, file_num = 0;
225 bool file_nm_sw = false;
226 const SCHAR* string = NULL;
227
228 const SCHAR* const* const end = argv + argc;
229 ++argv;
230 while (argv < end)
231 {
232 string = *argv;
233 if (*string == '-')
234 {
235 argv++;
236 ret_cd = get_function_option(prog_name, &sw_replace, string, switches);
237 if (ret_cd == FB_FAILURE)
238 {
239 free_file_list(file_list);
240 return FB_FAILURE;
241 }
242 } // end of processing (*string == '-')
243 else
244 {
245 // processing function specific command line options
246
247 switch (sw_replace)
248 {
249 case IN_SW_SPIT_SP:
250 if (!file_nm_sw)
251 {
252 // process file name
253 file_size = 0;
254 file_num = file_num + 1;
255
256 if (file_num > MAX_NUM_OF_FILES)
257 {
258 fprintf(stderr, "%s: maximum of files is %d\n", prog_name, MAX_NUM_OF_FILES);
259 print_clo(prog_name);
260 free_file_list(file_list);
261 return FB_FAILURE;
262 }
263
264 if (strlen(string) > MAX_FILE_NM_LEN)
265 {
266 fprintf(stderr, "%s: file name %s is too long\n", prog_name, string);
267 fprintf(stderr, "%s: maximum length of file name is %" SIZEFORMAT" bytes\n",
268 prog_name, MAX_FILE_NM_LEN);
269 print_clo(prog_name);
270 free_file_list(file_list);
271 return FB_FAILURE;
272 }
273
274 ret_cd = get_file_name(string, file_size, &file_ptr);
275
276 if (ret_cd == FB_FAILURE)
277 {
278 free_file_list(file_list);
279 return FB_FAILURE;
280 }
281
282 file_nm_sw = true;
283 file_ptr->b_fil_number = file_num;
284
285 if (!file_list)
286 file_list = prev_file = file_ptr;
287 else
288 {
289 prev_file->b_fil_next = file_ptr;
290 prev_file = file_ptr;
291 }
292
293 } // processing file name
294 else
295 {
296 // processing file size
297 file_nm_sw = false;
298 ret_cd = get_file_size(prog_name, string, &file_size);
299 if (ret_cd == FB_FAILURE)
300 {
301 free_file_list(file_list);
302 return FB_FAILURE;
303 }
304 } // end of processing file size specification
305 file_ptr->b_fil_size = file_size;
306 break;
307
308 case IN_SW_SPIT_JT:
309 ret_cd = get_file_name(string, file_size, &file_ptr);
310
311 if (ret_cd == FB_FAILURE)
312 {
313 free_file_list(file_list);
314 return FB_FAILURE;
315 }
316
317 file_num = file_num + 1;
318 file_ptr->b_fil_number = file_num;
319
320 if (!file_list)
321 file_list = prev_file = file_ptr;
322 else
323 {
324 prev_file->b_fil_next = file_ptr;
325 prev_file = file_ptr;
326 } // end of processing file size specification
327 break;
328
329 default:
330 fprintf(stderr, "%s: invalid option '%s'\n", prog_name, string);
331 print_clo(prog_name);
332 free_file_list(file_list);
333 return FB_FAILURE;
334 } // end of switch (sw_replace)
335
336 argv++;
337 } // processing function specific command line options
338 } // while (argv < end)
339
340 if (!file_list && sw_replace != IN_SW_SPIT_0)
341 {
342 fprintf(stderr, "%s: invalid option '%s', rest of parameters is missing\n", prog_name, string);
343 print_clo(prog_name);
344 free_file_list(file_list);
345 return FB_FAILURE;
346 }
347
348 FILE_DESC input_file_desc;
349 switch (sw_replace)
350 {
351 case IN_SW_SPIT_SP:
352 input_file_desc = GBAK_STDIN_DESC();
353 ret_cd = gen_multy_bakup_files(file_list, input_file_desc, file_num);
354 if (ret_cd == FB_FAILURE)
355 {
356 fprintf(stderr, "%s: progam fails to generate multi-volumn back-up files\n", prog_name);
357 free_file_list(file_list);
358 return FB_FAILURE;
359 }
360 break;
361
362 case IN_SW_SPIT_JT:
363 ret_cd = join_multy_bakup_files(file_list);
364 if (ret_cd == FB_FAILURE)
365 {
366 fprintf(stderr, "%s: progam fails to join multi-volumn back-up files\n", prog_name);
367 free_file_list(file_list);
368 return FB_FAILURE;
369 }
370 break;
371
372 default:
373 break;
374 }
375
376 // free all the storage allocated for backup files
377
378 free_file_list(file_list);
379 return FB_SUCCESS;
380
381 } // end of main()
382
383
384 static int get_function_option(const SCHAR* prog_name,
385 gsplit_option* sw_replace,
386 const SCHAR* string,
387 const Switches& switches)
388 {
389 /********************************************************************
390 **
391 ** g e t _ f u n c t i o n _ o p t i o n
392 **
393 *********************************************************************
394 **
395 ** Functional description:
396 **
397 ** processing command line option and set proper function switch
398 **
399 *********************************************************************
400 */
401
402 if (strlen(string) == 1)
403 {
404 fprintf(stderr, "%s: invalid option '%s'\n", prog_name, string);
405 print_clo(prog_name);
406 return FB_FAILURE;
407 }
408
409 //gsplit_option op_specified = *sw_replace; Commented, where is it used here???
410 const Switches::in_sw_tab_t* op = switches.findSwitch(string);
411 if (!op)
412 {
413 fprintf(stderr, "%s: invalid option '%s'\n", prog_name, string);
414 print_clo(prog_name);
415 return FB_FAILURE;
416 }
417
418 if (*sw_replace == IN_SW_SPIT_0)
419 {
420 *sw_replace = (gsplit_option) op->in_sw;
421 return FB_SUCCESS;
422 }
423
424 if (*sw_replace != op->in_sw)
425 {
426 fprintf(stderr, "%s: invalid option '%s', incompatible option\n", prog_name, string);
427 print_clo(prog_name);
428 return FB_FAILURE;
429 }
430
431 return FB_SUCCESS;
432 }
433
434
435 static int get_file_name( const SCHAR* string, SINT64 file_size, b_fil** file_ptr)
436 {
437 /********************************************************************
438 **
439 ** g e t _ f i l e _ n a m e
440 **
441 *********************************************************************
442 **
443 ** Functional description:
444 **
445 ** processing file name specification and returns file structure
446 ** pointer.
447 **
448 *********************************************************************
449 */
450
451 b_fil* const temp_ptr = (b_fil*) malloc(b_fil_len);
452
453 *file_ptr = temp_ptr;
454 temp_ptr->b_fil_name = (TEXT*) malloc(strlen(string) + 1);
455
456 temp_ptr->b_fil_next = NULL;
457 strcpy(temp_ptr->b_fil_name, string);
458 temp_ptr->b_fil_number = 0;
459 temp_ptr->b_fil_size = file_size;
460
461 return FB_SUCCESS;
462 }
463
464
465 static int get_file_size(const SCHAR* prog_name, const SCHAR* string, SINT64* file_size)
466 {
467 /********************************************************************
468 **
469 ** g e t _ f i l e _ s i z e
470 **
471 *********************************************************************
472 **
473 ** Functional description:
474 **
475 ** processing file size specification and returns calculated
476 ** file size.
477 **
478 *********************************************************************
479 */
480
481 UCHAR c;
482
483 for (const SCHAR* p = string; (c = *p++);)
484 {
485 if (c == '\0')
486 break;
487
488 if (isdigit(c))
489 *file_size = *file_size * 10 + (c - '0');
490 else
491 {
492 if ((isalpha(c)) && (UPPER(c) == 'G' || UPPER(c) == 'K' || UPPER(c) == 'M'))
493 {
494 SLONG size_indicator = 0;
495 switch (UPPER(c))
496 {
497 case 'K':
498 size_indicator = K_BYTES;
499 break;
500
501 case 'M':
502 size_indicator = M_BYTES;
503 break;
504
505 case 'G':
506 size_indicator = G_BYTES;
507 break;
508
509 default:
510 break;
511 } // end of switch( UPPER (c) )
512
513 *file_size = *file_size * size_indicator;
514 }
515 else
516 {
517 // invalid size indicator
518
519 fprintf(stderr, "%s: invalid size indicator '%s'\n", prog_name, string);
520 print_clo(prog_name);
521 return FB_FAILURE;
522 }
523 }
524 }
525 if (*file_size < MIN_FILE_SIZE)
526 {
527 // handling user specifies file size 0
528 fprintf(stderr, "%s: invalid option '%s', minimum file size is 1 megabyte\n",
529 prog_name, string);
530 print_clo(prog_name);
531 return FB_FAILURE;
532 }
533
534 return FB_SUCCESS;
535 }
536
537
538 static int gen_multy_bakup_files(b_fil* file_list, FILE_DESC input_file_desc, SLONG file_num)
539 {
540 /********************************************************************
541 **
542 ** g e n _ m u l t y _ b a c k u p _ f i l e s
543 **
544 *********************************************************************
545 **
546 ** Functional description:
547 **
548 ** processing input data from stdin and splits the data into
549 ** multiple back-up files.
550 **
551 ** allocates an 16K bytes I/O buffer
552 ** intilializes header record common fields
553 ** do forever
554 ** walk through the backup file chain
555 ** intilializes header record unique fields
556 ** open backup file
557 ** writes out header record to backup file
558 ** points to the next backup file in the chain
559 ** calculates the actual file size ( minus header record length )
560 ** if the actual file size less than 16K bytes
561 ** set I/O size to actual file size
562 ** otherwise
563 ** set I/O size to 16K byte long
564 ** when it is the last backup file
565 ** reads data from standard input as much as indicated by I/O size
566 ** and writes it out to the last backup file until no EOF.
567 ** issues error message when disk space full condition is detected
568 ** otherwise reads and writes to backup files util EOF
569 ** if disk full cobdition is detected
570 ** flush the remaining data in the I/O buffer to subsequence
571 ** backup files
572 ** go back to normal read and write process util EOF
573 **
574 *********************************************************************
575 */
576
577 SLONG byte_write, ret_cd;
578 TEXT header_str[header_rec_len], num_arr[5];
579 header_rec hdr_rec;
580
581 // CVC: there's a can of worms here. First, this function assumes it can free
582 // the io_buffer's allocated memory without keeping a second copy of that pointer.
583 // However, io_buffer can't be declared UCHAR* const because its address is
584 // passed to final_read_and_write() and read_and_write() and both functions
585 // thus suggest, by taking a UCHAR** that they can change the pointer's address;
586 // but in practice they never affect it, so fixing those functions to take simply
587 // UCHAR* would allow the correct declaration for io_buffer to succeed.
588 //UCHAR* const io_buffer = (UCHAR *) malloc(IO_BUFFER_SIZE);
589 UCHAR* io_buffer = (UCHAR *) malloc(IO_BUFFER_SIZE);
590
591 if (!io_buffer)
592 {
593 fprintf(stderr, "I/O buffer allocation failed\n");
594 return FB_FAILURE;
595 }
596
597 FB_SIZE_T pos;
598 for (pos = 0; pos < header_rec_len; pos++)
599 header_str[pos] = BLANK;
600
601 pos = 0;
602 ret_cd = set_hdr_str(header_str, header_rec_name, pos, sizeof(hdr_rec.name));
603 size_t indx;
604 for (indx = 0; indx < sizeof(hdr_rec.name); indx++)
605 hdr_rec.name[indx] = BLANK;
606
607 pos = pos + sizeof(hdr_rec.name);
608 time_t clock = time(0); // was SLONG
609 ret_cd = set_hdr_str(header_str, ctime(&clock), pos, sizeof(hdr_rec.date_time));
610 for (indx = 0; indx < sizeof(hdr_rec.date_time); indx++)
611 hdr_rec.date_time[indx] = BLANK;
612
613 pos = pos + sizeof(hdr_rec.date_time);
614 ret_cd = set_hdr_str(header_str, ", file No. ", pos, sizeof(hdr_rec.text1));
615 for (indx = 0; indx < sizeof(hdr_rec.text1); indx++)
616 hdr_rec.text1[indx] = BLANK;
617
618 for (indx = 0; indx < sizeof(hdr_rec.num); indx++)
619 hdr_rec.num[indx] = BLANK;
620
621 pos = pos + sizeof(hdr_rec.text1) + sizeof(hdr_rec.num);
622 ret_cd = set_hdr_str(header_str, " of ", pos, sizeof(hdr_rec.text2));
623 for (indx = 0; indx < sizeof(hdr_rec.text2); indx++)
624 hdr_rec.text2[indx] = BLANK;
625
626 ret_cd = conv_ntoc(file_num, num_arr);
627 if (ret_cd == FB_FAILURE)
628 {
629 free(io_buffer);
630 fprintf(stderr, "gsplit could not convert numeric data to character data\n");
631 return FB_FAILURE;
632 }
633
634 num_arr[sizeof(num_arr) - 1] = TERMINAL;
635 pos = pos + sizeof(hdr_rec.text2);
636 ret_cd = set_hdr_str(header_str, num_arr, pos, sizeof(hdr_rec.total));
637 for (indx = 0; indx < sizeof(hdr_rec.total); indx++)
638 hdr_rec.total[indx] = BLANK;
639
640 pos = pos + sizeof(hdr_rec.total);
641 ret_cd = set_hdr_str(header_str, ", ", pos, sizeof(hdr_rec.text3));
642 for (indx = 0; indx < sizeof(hdr_rec.text3); indx++)
643 hdr_rec.text3[indx] = BLANK;
644
645 for (indx = 0; indx < sizeof(hdr_rec.fl_name); indx++)
646 hdr_rec.fl_name[indx] = BLANK;
647
648 FILE_DESC output_fl_desc = 0;
649 bool end_of_input = false, flush_done = false;
650 const TEXT* file_name = NULL;
651 SLONG io_size = 0;
652 b_fil* fl_ptr = file_list;
653
654 SINT64 byte_read = 0;
655 SINT64 file_size = 0;
656
657 while (true)
658 {
659 if (fl_ptr != NULL)
660 {
661 byte_read = 0;
662 byte_write = 0;
663 if (!fl_ptr->b_fil_next && (fl_ptr->b_fil_size == 0))
664 {
665 fl_ptr->b_fil_size = MIN_FILE_SIZE;
666 }
667
668 file_size = fl_ptr->b_fil_size - header_rec_len;
669 file_name = fl_ptr->b_fil_name;
670
671 output_fl_desc = open_platf(file_name, 1);
672 if (output_fl_desc == INVALID_HANDLE_VALUE)
673 {
674 free(io_buffer);
675 fprintf(stderr, "can not open back up file %s\n", file_name);
676 return FB_FAILURE;
677 }
678
679 ret_cd = write_header(fl_ptr, hdr_rec, output_fl_desc, header_str);
680 if (ret_cd == FB_FAILURE)
681 {
682 free(io_buffer);
683 fprintf(stderr, "could not write header record to file %s\n", file_name);
684 return FB_FAILURE;
685 }
686
687 fl_ptr = fl_ptr->b_fil_next;
688 }
689
690 if (file_size < IO_BUFFER_SIZE)
691 io_size = (SLONG) file_size;
692 else
693 io_size = IO_BUFFER_SIZE;
694
695 if (!fl_ptr)
696 {
697 while (!end_of_input)
698 {
699 ret_cd = final_read_and_write(input_file_desc, output_fl_desc,
700 file_name, io_size, &io_buffer, &end_of_input);
701 if (ret_cd == FB_FAILURE)
702 {
703 free(io_buffer);
704 return FB_FAILURE;
705 }
706
707 if (end_of_input)
708 {
709 free(io_buffer);
710 return FB_SUCCESS;
711 }
712 }
713 }
714 else
715 {
716 while ((file_size > byte_read) && (fl_ptr != NULL))
717 {
718 ret_cd = read_and_write(input_file_desc, output_fl_desc,
719 /*file_name,*/ io_size, file_size,
720 &io_buffer, &end_of_input,
721 &byte_read, &byte_write);
722 switch (ret_cd)
723 {
724 case FB_FAILURE:
725 free(io_buffer);
726 return FB_FAILURE;
727
728 case FILE_IS_FULL:
729 {
730 byte_read = 0; // reset byte read count, prepare for next read
731
732 const UCHAR* remaining_io = io_buffer + byte_write;
733 SLONG remaining_io_len = IO_BUFFER_SIZE - byte_write;
734 while (!flush_done && (fl_ptr != NULL))
735 {
736 if (!fl_ptr->b_fil_next && fl_ptr->b_fil_size == 0)
737 fl_ptr->b_fil_size = MIN_FILE_SIZE;
738
739 file_size = fl_ptr->b_fil_size - header_rec_len;
740 file_name = fl_ptr->b_fil_name;
741
742 output_fl_desc = open_platf(file_name, 1);
743 if (output_fl_desc == INVALID_HANDLE_VALUE)
744 {
745 free(io_buffer);
746 fprintf(stderr, "can not open back up file %s\n", file_name);
747 return FB_FAILURE;
748 }
749 ret_cd = write_header(fl_ptr, hdr_rec, output_fl_desc, header_str);
750 if (ret_cd == FB_FAILURE)
751 {
752 free(io_buffer);
753 fprintf(stderr, "fail to write header rec to file %s\n", file_name);
754 return FB_FAILURE;
755 }
756
757 fl_ptr = fl_ptr->b_fil_next;
758 if (!fl_ptr)
759 {
760 ret_cd = final_flush_io_buff(remaining_io,
761 remaining_io_len,
762 output_fl_desc);
763 if (ret_cd == FB_FAILURE)
764 {
765 fprintf(stderr, "gsplit could not do backup due");
766 fprintf(stderr, " to lack of space or I/O problem\n");
767 free(io_buffer);
768 return FB_FAILURE;
769 }
770 }
771 else
772 {
773 // got a lot of backup files
774
775 ret_cd = flush_io_buff(remaining_io,
776 remaining_io_len,
777 output_fl_desc,
778 file_size,
779 &byte_write,
780 &flush_done);
781 if (ret_cd == FB_FAILURE)
782 {
783 fprintf(stderr, "gsplit could not do backup due");
784 fprintf(stderr, " I/O problem\n");
785 free(io_buffer);
786 return FB_FAILURE;
787 }
788 if (flush_done)
789 {
790 file_size = file_size - byte_write;
791 byte_write = 0;
792 }
793 else
794 {
795 remaining_io = remaining_io + byte_write;
796 remaining_io_len = remaining_io_len - byte_write;
797 }
798 }
799 } // end of while loop
800 break;
801 } // case FILE_IS_FULL
802
803 default:
804 break;
805 }
806
807 if (end_of_input)
808 {
809 free(io_buffer);
810 return FB_SUCCESS;
811 }
812 }
813 }
814 } // end of while ( true )
815 }
816
817
818 static int read_and_write(FILE_DESC input_file_desc,
819 FILE_DESC output_fl_desc,
820 //const TEXT* file_name,
821 SLONG io_size,
822 SINT64 file_size,
823 UCHAR** io_buffer,
824 bool* end_of_input,
825 SINT64* byte_read,
826 SLONG* byte_write)
827 {
828
829 /********************************************************************
830 **
831 ** r e a d _ a n d _ w r i t e
832 **
833 *********************************************************************
834 **
835 ** Functional description:
836 **
837 ** Read data from input file and write the data out to output
838 ** file.
839 **
840 *********************************************************************
841 */
842
843 SLONG read_cnt, last_read_size;
844
845 // when number of byte read + number of byte goint to
846 // be read is greater then file size, then calculate
847 // the size for the last read and do the last read for
848 // the current backup file. Otherwise read as mush data
849 // as will fit in the current backup file.
850
851 if (*byte_read + io_size > file_size)
852 {
853 last_read_size = (SLONG) (file_size - *byte_read);
854 read_cnt = read_platf(input_file_desc, *io_buffer, last_read_size);
855 }
856 else
857 read_cnt = read_platf(input_file_desc, *io_buffer, io_size);
858
859 switch (read_cnt)
860 {
861 case 0: // no more data to be read
862 close_platf(output_fl_desc);
863 *end_of_input = true;
864 *byte_read = *byte_read + read_cnt;
865 return FB_SUCCESS;
866
867 case -1: // read failed
868 close_platf(output_fl_desc);
869 fprintf(stderr, "fail to read input from stdin, errno = %d\n", errno);
870 return FB_FAILURE;
871
872 default: // read ok
873 *byte_read = *byte_read + read_cnt;
874 break;
875 }
876
877 const SLONG write_cnt = write_platf(output_fl_desc, *io_buffer, read_cnt);
878
879 switch (write_cnt)
880 {
881 case -1: // write failed
882 close_platf(output_fl_desc);
883 return FB_FAILURE;
884
885 default:
886 if (write_cnt == read_cnt) // write ok
887 return FB_SUCCESS;
888
889 // write less data than it reads in
890 close_platf(output_fl_desc);
891 *byte_write = write_cnt;
892 return FILE_IS_FULL;
893 }
894 }
895
896
897 static int final_read_and_write(FILE_DESC input_file_desc,
898 FILE_DESC output_fl_desc,
899 const TEXT* file_name,
900 SLONG io_size,
901 UCHAR** io_buffer,
902 bool* end_of_input)
903 {
904
905 /********************************************************************
906 **
907 ** f i n a l _ r e a d _ a n d _ w r i t e
908 **
909 *********************************************************************
910 **
911 ** Functional description:
912 **
913 ** Read all data from input file and write the data out to output
914 ** file.
915 **
916 *********************************************************************
917 */
918
919 const SLONG read_cnt = read_platf(input_file_desc, *io_buffer, io_size);
920
921 switch (read_cnt)
922 {
923 case 0: // no more data to be read
924 close_platf(output_fl_desc);
925 *end_of_input = true;
926 return FB_SUCCESS;
927
928 case -1: // read failed
929 close_platf(output_fl_desc);
930 fprintf(stderr, "problem when reading input file, errno = %d\n", errno);
931 return FB_FAILURE;
932
933 default: // read ok
934 break;
935 }
936
937 const SLONG write_cnt = write_platf(output_fl_desc, *io_buffer, read_cnt);
938
939 switch (write_cnt)
940 {
941 case -1: // write failed
942 close_platf(output_fl_desc);
943 return FB_FAILURE;
944
945 default:
946 if (write_cnt == read_cnt) // write ok
947 return FB_SUCCESS;
948
949 fprintf(stderr, "There is no enough space to write to back up file %s\n", file_name);
950 close_platf(output_fl_desc);
951 return FB_FAILURE;
952 }
953 }
954
955
956 static int join_multy_bakup_files( b_fil* file_list)
957 {
958 /********************************************************************
959 **
960 ** j o i n _ m u l t y _ b a c k u p _ f i l e s
961 **
962 *********************************************************************
963 **
964 ** Functional description:
965 **
966 ** allocates I/O buffer and walks through backup files` chain.
967 ** calls read_and_write_for_join routine to read data from
968 ** backup file and to write data to standard output file
969 ** which later to be processed by gbak. Finally frees up
970 ** I/O buffer
971 **
972 *********************************************************************
973 */
974
975 FILE_DESC output_fl_desc = GBAK_STDOUT_DESC();
976
977 // See comment near the beginning of gen_multy_bakup_files() as it
978 // also applies to read_and_write_for_join().
979 UCHAR* const io_buffer = (UCHAR*) malloc(IO_BUFFER_SIZE);
980 //UCHAR* io_buffer = (UCHAR*) malloc(IO_BUFFER_SIZE);
981
982 if (io_buffer == 0)
983 {
984 fprintf(stderr, "I/O buffer allocation failed\n");
985 return FB_FAILURE;
986 }
987
988 SLONG cnt = 0, total_int = 0;
989 // Why two variables to achieve this simple loop?
990 const b_fil* next_fl = NULL;
991 for (const b_fil* fl_ptr = file_list; fl_ptr; fl_ptr = next_fl)
992 {
993 cnt++;
994 next_fl = fl_ptr->b_fil_next;
995 const TEXT* file_name = fl_ptr->b_fil_name;
996
997 SLONG ret_cd = read_and_write_for_join(output_fl_desc, file_name, io_buffer, cnt, &total_int);
998
999 if (ret_cd == FB_FAILURE)
1000 {
1001 free(io_buffer);
1002 return FB_FAILURE;
1003 }
1004
1005 } // end of for loop
1006
1007 free(io_buffer);
1008 return FB_SUCCESS;
1009 }
1010
1011
1012 static int read_and_write_for_join(FILE_DESC output_fl_desc,
1013 const TEXT* file_name,
1014 UCHAR* io_buffer,
1015 SLONG cnt,
1016 SLONG* total_int)
1017 {
1018 /********************************************************************
1019 **
1020 ** r e a d _ a n d _ w r i t e _ f o r _ j o i n
1021 **
1022 *********************************************************************
1023 **
1024 ** Functional description:
1025 **
1026 ** Reads data from backup files and writes to standard
1027 ** output file.
1028 **
1029 *********************************************************************
1030 */
1031
1032 TEXT num_arr[5], total_arr[5];
1033 header_rec hdr_rec;
1034 FB_UNUSED_VAR(hdr_rec); // Silence compiler warning
1035
1036 FILE_DESC input_fl_desc = open_platf(file_name, mode_read);
1037
1038 if (input_fl_desc == INVALID_HANDLE_VALUE)
1039 {
1040 fprintf(stderr, "can not open input file %s\n", file_name);
1041 return FB_FAILURE;
1042 }
1043
1044 int read_cnt = read_platf(input_fl_desc, io_buffer, header_rec_len);
1045 if (read_cnt != static_cast<int>(header_rec_len))
1046 {
1047 close_platf(input_fl_desc);
1048 fprintf(stderr, "progam fails to read gsplit header record in back-up file%s\n", file_name);
1049 return FB_FAILURE;
1050 }
1051
1052 const TEXT* char_ptr1 = reinterpret_cast<char*>(io_buffer);
1053 SLONG ret_cd = strncmp(char_ptr1, header_rec_name, sizeof(hdr_rec.name) - 1);
1054 if (ret_cd != 0)
1055 {
1056 close_platf(input_fl_desc);
1057 fprintf(stderr, "gsplit: expected GSPLIT description record\n");
1058 fprintf(stderr, "gsplit: Exiting before completion due to errors\n");
1059 return FB_FAILURE;
1060 }
1061
1062 const SLONG skip_to_num = sizeof(hdr_rec.name) + sizeof(hdr_rec.date_time) + sizeof(hdr_rec.text1);
1063 const SLONG skip_to_total = skip_to_num + sizeof(hdr_rec.num) + sizeof(hdr_rec.text2);
1064
1065 char_ptr1 = reinterpret_cast<char*>(io_buffer + skip_to_num);
1066 const TEXT* char_ptr2 = reinterpret_cast<char*>(io_buffer + skip_to_total);
1067 size_t indx;
1068 for (indx = 0; indx < sizeof(hdr_rec.num); indx++)
1069 {
1070 num_arr[indx] = *char_ptr1;
1071 char_ptr1++;
1072 if (cnt == 1)
1073 {
1074 total_arr[indx] = *char_ptr2;
1075 char_ptr2++;
1076 }
1077 }
1078 num_arr[indx] = '\0';
1079 const SLONG num_int = atoi(num_arr);
1080 if (cnt == 1)
1081 {
1082 total_arr[indx] = '\0';
1083 *total_int = atoi(total_arr);
1084 }
1085
1086 if ((num_int != cnt) || (num_int > *total_int))
1087 {
1088 close_platf(input_fl_desc);
1089 fprintf(stderr, "gsplit: join backup file is out of sequence\n");
1090 fprintf(stderr, "gsplit: Exiting before completion due to errors\n");
1091 return FB_FAILURE;
1092 }
1093
1094 read_cnt = read_platf(input_fl_desc, io_buffer, IO_BUFFER_SIZE);
1095
1096
1097 while (true)
1098 {
1099 switch (read_cnt)
1100 {
1101 case 0: // no more data to be read
1102 close_platf(input_fl_desc);
1103 return FB_SUCCESS;
1104
1105 case -1: // read failed
1106 close_platf(input_fl_desc);
1107 return FB_FAILURE;
1108
1109 default: // this is the last read
1110 break;
1111 }
1112
1113 SLONG write_cnt = write_platf(output_fl_desc, io_buffer, read_cnt);
1114
1115 switch (write_cnt)
1116 {
1117 case -1: // write failed
1118 close_platf(input_fl_desc);
1119 return FB_FAILURE;
1120
1121 default:
1122 fb_assert(write_cnt == read_cnt);
1123 break;
1124 }
1125
1126 read_cnt = read_platf(input_fl_desc, io_buffer, IO_BUFFER_SIZE);
1127
1128 } // end of while (true) loop
1129 }
1130
1131
1132 static int conv_ntoc( SLONG numeric_in, TEXT char_out[])
1133 {
1134 /********************************************************************
1135 **
1136 ** c o n v _ n t o c
1137 **
1138 *********************************************************************
1139 **
1140 ** Functional description:
1141 **
1142 ** Convert 4-digit numeric data to a 4-byte character array.
1143 **
1144 *********************************************************************
1145 */
1146
1147 if (numeric_in <= 0)
1148 return FB_FAILURE;
1149
1150 int i = numeric_in;
1151 int indx = 3;
1152
1153 while (indx >= 0 && i > 0)
1154 {
1155 const int mod = i % 10;
1156 char_out[indx] = '0' + mod;
1157 --indx;
1158 i = i / 10;
1159 }
1160
1161 if (i > 0) // number was too big
1162 return FB_FAILURE;
1163
1164 for (; indx >= 0; indx--) {
1165 char_out[indx] = ' ';
1166 }
1167 return FB_SUCCESS;
1168 }
1169
1170
1171 static int write_header(const b_fil* fl_ptr,
1172 header_rec hdr_rec,
1173 FILE_DESC output_fl_desc,
1174 TEXT header_str[])
1175 {
1176 /********************************************************************
1177 **
1178 ** w r i t e _ h e a d e r
1179 **
1180 *********************************************************************
1181 **
1182 ** Functional description:
1183 **
1184 ** writes out gsplit header record
1185 **
1186 ** assigns backup file name to header record file name array
1187 ** calls conv_ntoc routine to convert numeric data to char data
1188 ** and assigns to header record file number field
1189 ** writes gsplit header record out to backup file
1190 **
1191 *********************************************************************
1192 */
1193 TEXT num_arr[5];
1194
1195 SLONG ret_cd = conv_ntoc(fl_ptr->b_fil_number, num_arr);
1196 if (ret_cd == FB_FAILURE)
1197 {
1198 printf("gsplit could not convert numeric data to character data\n");
1199 return FB_FAILURE;
1200 }
1201 num_arr[sizeof(num_arr) - 1] = TERMINAL;
1202 SLONG pos = sizeof(hdr_rec.name) + sizeof(hdr_rec.date_time) + sizeof(hdr_rec.text1);
1203 ret_cd = set_hdr_str(header_str, num_arr, pos, sizeof(hdr_rec.num));
1204
1205 const TEXT* file_name = fl_ptr->b_fil_name;
1206 pos = sizeof(hdr_rec.name) + sizeof(hdr_rec.date_time) +
1207 sizeof(hdr_rec.text1) + sizeof(hdr_rec.num) +
1208 sizeof(hdr_rec.text2) + sizeof(hdr_rec.total) + sizeof(hdr_rec.text3);
1209 ret_cd = set_hdr_str(header_str, file_name, pos, static_cast<SLONG>(strlen(file_name)));
1210
1211 SLONG end, indx;
1212 SLONG write_cnt = write_platf(output_fl_desc, header_str, header_rec_len);
1213 switch (write_cnt)
1214 {
1215 case -1: // write failed
1216 close_platf(output_fl_desc);
1217 return FB_FAILURE;
1218
1219 default:
1220 end = pos + fb_strlen(file_name);
1221 for (indx = pos; indx < end; indx++)
1222 header_str[indx] = BLANK;
1223 return FB_SUCCESS;
1224 }
1225 }
1226
1227
1228 static int flush_io_buff(const UCHAR* remaining_io,
1229 SLONG remaining_io_len,
1230 FILE_DESC output_fl_desc,
1231 SINT64 file_size,
1232 SLONG* byte_write,
1233 bool* flush_done)
1234 {
1235 /********************************************************************
1236 **
1237 ** f l u s h _ i o _ b u f f
1238 **
1239 *********************************************************************
1240 **
1241 ** Functional description:
1242 **
1243 ** flush out the remaining data in the I/O buffer
1244 **
1245 ** when the file_size is truly has space to write out the remaining data
1246 ** then
1247 ** set and returns flush_done true
1248 ** otherwise
1249 ** closes the output file, set and returns flush_done false.
1250 ** otherwise
1251 ** we can only writes out as much data as file_size indicated
1252 ** if it was able to write out the remaining data
1253 ** then
1254 ** set and returns flush_done true
1255 ** otherwise
1256 ** closes the output file, set and returns flush_done false.
1257 **
1258 *********************************************************************
1259 */
1260
1261 SLONG write_cnt;
1262
1263 if (file_size > remaining_io_len)
1264 write_cnt = write_platf(output_fl_desc, remaining_io, remaining_io_len);
1265 else // file_size <= remaining_io_len
1266 write_cnt = write_platf(output_fl_desc, remaining_io, (unsigned int) file_size);
1267
1268 switch (write_cnt)
1269 {
1270 case -1: // write failed
1271 close_platf(output_fl_desc);
1272 *flush_done = false;
1273 return FB_FAILURE;
1274
1275 default:
1276 if (write_cnt == remaining_io_len) // write ok
1277 *flush_done = true;
1278 else
1279 {
1280 // could not write out all remaining data
1281 close_platf(output_fl_desc);
1282 *flush_done = false;
1283 }
1284 *byte_write = write_cnt;
1285 return FB_SUCCESS;
1286 }
1287 }
1288
1289
1290 static int final_flush_io_buff(const UCHAR* remaining_io,
1291 SLONG remaining_io_len,
1292 FILE_DESC output_fl_desc)
1293 {
1294 /********************************************************************
1295 **
1296 ** f i n a l _ f l u s h _ i o _ b u f f
1297 **
1298 *********************************************************************
1299 **
1300 ** Functional description:
1301 **
1302 ** flush out the remaining data in the I/O buffer
1303 **
1304 *********************************************************************
1305 */
1306
1307 SLONG write_cnt = write_platf(output_fl_desc, remaining_io, remaining_io_len);
1308 switch (write_cnt)
1309 {
1310 case -1: // write failed
1311 close_platf(output_fl_desc);
1312 return FB_FAILURE;
1313
1314 default:
1315 if (write_cnt == remaining_io_len) // write ok
1316 return FB_SUCCESS;
1317
1318 close_platf(output_fl_desc);
1319 return FB_FAILURE;
1320 }
1321 }
1322
1323
1324 static void print_clo(const TEXT* prog_name)
1325 {
1326 /********************************************************************
1327 **
1328 ** p r i n t _ c l o
1329 **
1330 *********************************************************************
1331 **
1332 ** Functional description:
1333 **
1334 ** print out gsplit utility command line options
1335 **
1336 *********************************************************************
1337 */
1338
1339 fprintf(stderr, "%s: Command Line Options Are:\n", prog_name);
1340 fprintf(stderr,
1341 " gsplit -S[PLIT_BK_FILE] <file> <size>{k|m|g} [... <file> [<size>{k|m|g}]] or\n");
1342 fprintf(stderr, " gsplit -J[OINT_BK_FILE] <file> [... <file>]\n");
1343 fprintf(stderr, "%s: option can be abbreviated to the unparenthesized characters\n", prog_name);
1344 fprintf(stderr, "%s: Exiting before completion due to errors\n", prog_name);
1345
1346 //return FB_SUCCESS;
1347
1348 }
1349
1350
1351 static int set_hdr_str(TEXT header_str[], const TEXT* in_str, SLONG pos, SLONG len)
1352 {
1353 /********************************************************************
1354 **
1355 ** s e t _ h d r _ s t r
1356 **
1357 *********************************************************************
1358 **
1359 ** Functional description:
1360 **
1361 ** initialyze header string
1362 **
1363 *********************************************************************
1364 */
1365 const TEXT* t_str = in_str;
1366 const SLONG end = pos + len;
1367
1368 for (SLONG indx = pos; indx < end; indx++)
1369 {
1370 switch (*t_str)
1371 {
1372 case NEW_LINE:
1373 case TERMINAL:
1374 header_str[indx] = ' ';
1375 break;
1376 default:
1377 header_str[indx] = *t_str;
1378 break;
1379 }
1380 ++t_str;
1381 }
1382 return FB_SUCCESS;
1383 }
1384
1385
1386 static int free_file_list( b_fil* file_list)
1387 {
1388 /********************************************************************
1389 **
1390 ** f r e e _ f i l e _ l i s t
1391 **
1392 *********************************************************************
1393 **
1394 ** Functional description:
1395 **
1396 ** free all the storage allocated for backup files structure
1397 **
1398 *********************************************************************
1399 */
1400
1401 b_fil* next_file = NULL;
1402
1403 for (b_fil* file_ptr = file_list; file_ptr != NULL; file_ptr = next_file)
1404 {
1405 next_file = file_ptr->b_fil_next;
1406 free(file_ptr->b_fil_name);
1407 free(file_ptr);
1408 }
1409
1410 return FB_SUCCESS;
1411 }