/
ndmjob_job.c
642 lines (518 loc) · 12.6 KB
/
ndmjob_job.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
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/*
* Copyright (c) 1998,1999,2000
* Traakan, Inc., Los Altos, CA
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice unmodified, this list of conditions, and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Project: NDMJOB
* Ident: $Id: $
*
* Description:
*
*/
#include "ndmjob.h"
#ifndef NDMOS_OPTION_NO_CONTROL_AGENT
int
build_job (void)
{
struct ndm_job_param * job = &the_job;
int i, rc, n_err;
char errbuf[100];
NDMOS_MACRO_ZEROFILL (job);
args_to_job ();
ndma_job_auto_adjust (job);
if (o_rules)
apply_rules (job, o_rules);
i = n_err = 0;
do {
rc = ndma_job_audit (job, errbuf, i);
if (rc > n_err || rc < 0) {
ndmjob_log (0, "error: %s", errbuf);
}
n_err = rc;
} while (i++ < n_err);
if (n_err) {
error_byebye ("can't proceed");
/* no return */
}
return 0;
}
int
args_to_job (void)
{
struct ndm_job_param * job = &the_job;
int i;
switch (the_mode) {
case NDM_JOB_OP_QUERY_AGENTS:
case NDM_JOB_OP_INIT_LABELS:
case NDM_JOB_OP_LIST_LABELS:
case NDM_JOB_OP_REMEDY_ROBOT:
case NDM_JOB_OP_TEST_TAPE:
case NDM_JOB_OP_TEST_MOVER:
case NDM_JOB_OP_TEST_DATA:
case NDM_JOB_OP_REWIND_TAPE:
case NDM_JOB_OP_EJECT_TAPE:
case NDM_JOB_OP_MOVE_TAPE:
case NDM_JOB_OP_IMPORT_TAPE:
case NDM_JOB_OP_EXPORT_TAPE:
case NDM_JOB_OP_LOAD_TAPE:
case NDM_JOB_OP_UNLOAD_TAPE:
case NDM_JOB_OP_INIT_ELEM_STATUS:
break;
case NDM_JOB_OP_BACKUP:
args_to_job_backup_env();
break;
case NDM_JOB_OP_TOC:
args_to_job_recover_env();
args_to_job_recover_nlist();
if (J_index_file) {
jndex_doit();
jndex_merge_environment();
}
break;
case NDM_JOB_OP_EXTRACT:
args_to_job_recover_env();
args_to_job_recover_nlist();
jndex_doit();
jndex_merge_environment();
break;
case 'D': /* -o daemon */
return 0;
default:
printf ("mode -%c not implemented yet\n", the_mode);
break;
}
job->operation = the_mode;
/* DATA agent */
job->data_agent = D_data_agent;
job->bu_type = B_bu_type;
job->env_tab = E_environment;
if (the_mode == NDM_JOB_OP_EXTRACT || the_mode == NDM_JOB_OP_TOC) {
for (i = 0; i < n_file_arg; i++) {
ndma_store_nlist (&job->nlist_tab, &nlist[i]);
}
job->nlist_tab.n_nlist = n_file_arg;
}
job->index_log.deliver = ndmjob_ixlog_deliver;
/* TAPE agent */
job->tape_agent = T_tape_agent;
job->tape_device = f_tape_device;
job->record_size = b_bsize * 512;
job->tape_timeout = o_tape_timeout;
job->use_eject = o_use_eject;
job->tape_target = o_tape_scsi;
job->tape_tcp = o_tape_tcp;
/* ROBOT agent */
job->robot_agent = R_robot_agent;
job->robot_target = r_robot_target;
job->robot_timeout = o_robot_timeout;
if (o_tape_addr >= 0) {
job->drive_addr = o_tape_addr;
job->drive_addr_given = 1;
}
if (o_from_addr >= 0) {
job->from_addr = o_from_addr;
job->from_addr_given = 1;
}
if (o_to_addr >= 0) {
job->to_addr = o_to_addr;
job->to_addr_given = 1;
}
if (ROBOT_GIVEN())
job->have_robot = 1;
/* media */
job->media_tab = m_media;
return 0;
}
int
args_to_job_backup_env (void)
{
ndmp9_pval pv;
int i;
if (C_chdir) {
pv.name = "FILESYSTEM";
pv.value = C_chdir;
ndma_store_env_list (&E_environment, &pv);
}
pv.name = "HIST";
pv.value = I_index_file ? "y" : "n";
ndma_store_env_list (&E_environment, &pv);
pv.name = "TYPE";
pv.value = B_bu_type;
ndma_store_env_list (&E_environment, &pv);
if (U_user) {
pv.name = "USER";
pv.value = U_user;
ndma_store_env_list (&E_environment, &pv);
}
for (i = 0; (i < n_e_exclude_pattern); i++) {
pv.name = "EXCLUDE";
pv.value = e_exclude_pattern[i];
ndma_store_env_list (&E_environment, &pv);
}
for (i = 0; (i < n_file_arg); i++) {
pv.name = "FILES";
pv.value = file_arg[i];
ndma_store_env_list (&E_environment, &pv);
}
if (o_rules) {
pv.name = "RULES";
pv.value = o_rules;
ndma_store_env_list (&E_environment, &pv);
}
return E_environment.n_env;
}
int
args_to_job_recover_env (void)
{
ndmp9_pval pv;
int i;
if (C_chdir) {
pv.name = "PREFIX";
pv.value = C_chdir;
ndma_store_env_list (&E_environment, &pv);
}
pv.name = "HIST";
pv.value = I_index_file ? "y" : "n";
ndma_store_env_list (&E_environment, &pv);
pv.name = "TYPE";
pv.value = B_bu_type;
ndma_store_env_list (&E_environment, &pv);
if (U_user) {
pv.name = "USER";
pv.value = U_user;
ndma_store_env_list (&E_environment, &pv);
}
for (i = 0; i < n_e_exclude_pattern; i++) {
pv.name = "EXCLUDE";
pv.value = e_exclude_pattern[i];
ndma_store_env_list (&E_environment, &pv);
}
if (o_rules) {
pv.name = "RULES";
pv.value = o_rules;
ndma_store_env_list (&E_environment, &pv);
}
/* file_arg[]s are done in nlist[] */
jndex_merge_environment ();
return E_environment.n_env;
}
void
normalize_name (char *name)
{
char * p = name;
while (*p) {
if (*p == '/' && p[1] == '/') {
strcpy (p, p+1);
continue;
}
if (p[0] == '/' && p[1] == '.' && (p[2] == '/' || p[2] == 0)) {
strcpy (p, p+2);
continue;
}
p++;
}
}
int
args_to_job_recover_nlist (void)
{
int not_found = 0;
int i, prefix_len, len;
char * dest;
if (C_chdir) {
prefix_len = strlen (C_chdir) + 2;
} else {
prefix_len = 0;
}
for (i = 0; (i < n_file_arg) && (i < NDM_MAX_NLIST); i++) {
if (file_arg_new[i]) {
len = strlen (file_arg_new[i]) + prefix_len + 1;
dest = NDMOS_API_MALLOC (len);
*dest = 0;
if (C_chdir) {
strcpy (dest, C_chdir);
}
if (file_arg_new[i][0] != '/') {
strcat (dest, "/");
}
strcat (dest, file_arg_new[i]);
normalize_name (file_arg_new[i]);
normalize_name (file_arg[i]);
normalize_name (dest);
nlist[i].original_path = file_arg[i];
nlist[i].destination_path = dest;
} else {
len = strlen (file_arg[i]) + prefix_len + 1;
dest = NDMOS_API_MALLOC (len);
*dest = 0;
if (C_chdir) {
strcpy (dest, C_chdir);
}
if (file_arg[i][0] != '/') {
strcat (dest, "/");
}
strcat (dest, file_arg[i]);
normalize_name (file_arg[i]);
normalize_name (dest);
nlist[i].original_path = file_arg[i];
nlist[i].destination_path = dest;
}
}
return not_found; /* should ALWAYS be 0 */
}
/*
* Index files are sequentially searched. They can be VERY big.
* There is a credible effort for efficiency here.
* Probably lots and lots and lots of room for improvement.
*/
FILE * jndex_open (void);
int
jndex_doit (void)
{
FILE * fp;
int rc;
fp = jndex_open();
if (!fp) {
/* error messages already given */
return -1;
}
ndmjob_log (1, "Processing input index (-J%s)", J_index_file);
if (n_file_arg > 0) {
rc = ndmfhdb_add_fh_info_to_nlist (fp, nlist, n_file_arg);
if (rc < 0) {
/* toast one way or another */
}
}
jndex_fetch_post_backup_data_env(fp);
jndex_fetch_post_backup_media(fp);
jndex_tattle();
if (jndex_audit_not_found ()) {
ndmjob_log (1,
"Warning: Missing index entries, valid file name(s)?");
}
jndex_merge_media ();
fclose(fp);
return 0;
}
FILE *
jndex_open (void)
{
char buf[256];
FILE * fp;
if (!J_index_file) {
/* Hmmm. */
ndmjob_log (1, "Warning: No -J input index?");
return 0;
}
ndmjob_log (1, "Reading input index (-I%s)", J_index_file);
fp = fopen(J_index_file, "r");
if (!fp) {
perror (J_index_file);
error_byebye ("Can not open -J%s input index", J_index_file);
/* no return */
}
if (fgets (buf, sizeof buf, fp) == NULL) {
fclose (fp);
error_byebye ("Failed read 1st line of -J%s", J_index_file);
/* no return */
}
if (strcmp (buf, "##ndmjob -I\n") != 0) {
fclose (fp);
error_byebye ("Bad 1st line in -J%s", J_index_file);
/* no return */
}
if (fgets (buf, sizeof buf, fp) == NULL) {
fclose (fp);
error_byebye ("Failed read 2nd line of -J%s", J_index_file);
/* no return */
}
if (strcmp (buf, "##ndmjob -J\n") != 0) {
fclose (fp);
error_byebye ("Bad 2nd line in -J%s", J_index_file);
/* no return */
}
ndmjob_log (2, "Opened index (-J%s)", J_index_file);
return fp;
}
int
jndex_tattle (void)
{
char buf[100];
struct ndmmedia * me;
struct ndm_env_entry * nev;
int i;
for (me = ji_media.head; me; me = me->next) {
ndmmedia_to_str (me, buf);
ndmjob_log (3, "ji me[%d] %s", i, buf);
}
for (nev = ji_environment.head; nev; nev = nev->next) {
ndmjob_log (3, "ji env[%d] %s=%s", i, nev->pval.name, nev->pval.value);
}
for (i = 0; (i < n_file_arg) && (i < NDM_MAX_NLIST); i++) {
if (nlist[i].fh_info.valid) {
ndmjob_log (3, "ji fil[%d] fi=%lld %s",
i, nlist[i].fh_info.value, file_arg[i]);
} else {
ndmjob_log (3, "ji fil[%d] not-found %s",
i, file_arg[i]);
}
}
return 0;
}
int
jndex_merge_media (void)
{
struct ndmmedia * me;
struct ndmmedia * jme;
int i;
for (jme = ji_media.head; jme; jme = jme->next) {
if (! jme->valid_label)
continue; /* can't match it up */
for (me = m_media.head; me; me = me->next) {
if (! me->valid_label)
continue; /* can't match it up */
if (strcmp (jme->label, me->label) != 0)
continue;
if (!jme->valid_slot && me->valid_slot) {
jme->slot_addr = me->slot_addr;
jme->valid_slot = 1;
}
}
}
ndmca_destroy_media_table (&m_media);
m_media = ji_media;
ndmjob_log (3, "After merging input -J index with -m entries");
i = 0;
for (me = m_media.head; me; me = me->next) {
char buf[40];
ndmmedia_to_str (me, buf);
ndmjob_log (3, "%d: -m %s", i + 1, buf);
i++;
}
return 0;
}
int
jndex_audit_not_found (void)
{
int i;
int not_found = 0;
for (i = 0; (i < n_file_arg) && (i < NDM_MAX_NLIST); i++) {
if (!nlist[i].fh_info.valid) {
ndmjob_log (0, "No index entry for %s", file_arg[i]);
not_found++;
}
}
return not_found;
}
int
jndex_merge_environment (void)
{
struct ndm_env_entry * entry;
for (entry = ji_environment.head; entry; entry = entry->next) {
if (strcmp(entry->pval.name, "FILESYSTEM") != 0 &&
strcmp(entry->pval.name, "PREFIX") != 0 &&
strcmp(entry->pval.name, "HIST") != 0 &&
strcmp(entry->pval.name, "TYPE") != 0) {
ndma_store_env_list (&E_environment, &entry->pval);
}
}
return 0;
}
int
jndex_fetch_post_backup_data_env (FILE *fp)
{
int rc;
char buf[512];
char * p;
char * q;
ndmp9_pval pv;
rc = ndmbstf_first (fp, "DE ", buf, sizeof buf);
if (rc <= 0) {
return rc; /* error or not found */
}
/* DE HIST=Yes */
while (buf[0] == 'D' && buf[1] == 'E' && buf[2] == ' ') {
if (ji_environment.n_env >= NDM_MAX_ENV) {
goto overflow;
}
p = &buf[2];
while (*p == ' ') p++;
if (!strchr (p, '=')) {
goto malformed;
}
q = strchr (p, '=');
if (!q) {
goto malformed;
}
*q++ = 0;
pv.name = p;
pv.value = q;
ndma_store_env_list (&ji_environment, &pv);
rc = ndmbstf_getline (fp, buf, sizeof buf);
if (rc <= 0) {
break;
}
continue;
malformed:
ndmjob_log (1, "Malformed in -J%s: %s", J_index_file, buf);
continue;
overflow:
ndmjob_log (1, "Overflow in -J%s: %s", J_index_file, buf);
}
return 0;
}
int
jndex_fetch_post_backup_media (FILE *fp)
{
int rc;
char buf[512];
struct ndmmedia me;
rc = ndmbstf_first (fp, "CM ", buf, sizeof buf);
if (rc <= 0) {
return rc; /* error or not found */
}
/* CM 01 T103/10850K */
while (buf[0] == 'C' && buf[1] == 'M' && buf[2] == ' ') {
if (ji_media.n_media >= NDM_MAX_MEDIA) {
goto overflow;
}
if (ndmmedia_from_str (&me, &buf[6])) {
goto malformed;
}
ndma_clone_media_entry (&ji_media, &me);
rc = ndmbstf_getline (fp, buf, sizeof buf);
if (rc <= 0) {
break;
}
continue;
malformed:
ndmjob_log (1, "Malformed in -J%s: %s", J_index_file, buf);
continue;
overflow:
ndmjob_log (1, "Overflow in -J%s: %s", J_index_file, buf);
}
return 0;
}
#endif /* !NDMOS_OPTION_NO_CONTROL_AGENT */