forked from openpbs/openpbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage_func.c
2548 lines (2310 loc) · 67.6 KB
/
stage_func.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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 1994-2020 Altair Engineering, Inc.
* For more information, contact Altair at www.altair.com.
*
* This file is part of both the OpenPBS software ("OpenPBS")
* and the PBS Professional ("PBS Pro") software.
*
* Open Source License Information:
*
* OpenPBS is free software. You can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* OpenPBS is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Commercial License Information:
*
* PBS Pro is commercially licensed software that shares a common core with
* the OpenPBS software. For a copy of the commercial license terms and
* conditions, go to: (http://www.pbspro.com/agreement.html) or contact the
* Altair Legal Department.
*
* Altair's dual-license business model allows companies, individuals, and
* organizations to create proprietary derivative works of OpenPBS and
* distribute them - whether embedded or bundled with other software -
* under a commercial license agreement.
*
* Use of Altair's trademarks, including but not limited to "PBS™",
* "OpenPBS®", "PBS Professional®", and "PBS Pro™" and Altair's logos is
* subject to Altair's trademark licensing policies.
*/
#include <pbs_config.h> /* the master config generated by configure */
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef WIN32
#include <io.h>
#include <windows.h>
#include "win.h"
#else
#include <sys/wait.h>
#include <dirent.h>
#include "tpp.h"
#endif
#include "pbs_ifl.h"
#include "list_link.h"
#include "attribute.h"
#include "job.h"
#include "ticket.h"
#include "libpbs.h"
#include "batch_request.h"
#include "pbs_nodes.h"
#include "mom_func.h"
/**
* @file stage_func.c
*/
extern char *path_spool; /* path to spool directory */
extern char *path_undeliv; /* path to undelivered directory */
extern char *path_checkpoint; /* path to checkpoint directory */
extern char *msg_err_unlink; /* unlink error message (see pbs_messages.c) */
extern char rcperr[MAXPATHLEN]; /* path to rcp errfile for current copy */
extern char *pbs_jobdir; /* path to staging and execution dir of current job */
extern char *cred_buf; /* cred buffer */
extern size_t cred_len; /* length of cred buffer */
#ifndef WIN32
extern char *shell;
extern char *argv[BUF_SIZE]; /* lots of args */
extern int argc;
extern int cred_pipe;
extern char *pwd_buf;
#endif
extern char mom_host[PBS_MAXHOSTNAME+1]; /* MoM host name */
int stage_file(int, int, char *, struct rqfpair *, int, cpy_files *, char *);
static int sys_copy(int, int, char *, char *, struct rqfpair *, int, char *);
/**
* A path in windows is not case sensitive so do a define
* to do the right compare.
*/
#ifdef WIN32
#define PATHCMP strncasecmp
#else
#define PATHCMP strncmp
#endif
#ifdef WIN32
/**
* @brief
* check_err - Report the error in log file, if the length of actual data
* buffer and length of the total data written is different.
*
* @param[in] func_name - Name of the caller function
* @param[in] buffer - Actual data buffer
* @param[in] written_len - Total length of the written data
*
* @return void
*/
void
check_err(const char* func_name, char *buffer, int written_len)
{
if (written_len != strlen(buffer)) {
DWORD ecode = GetLastError();
snprintf(log_buffer, sizeof(log_buffer),
"Failed to write or written partial data to the pipe: data=[%s], total_len=%d, written_len=%d, ecode=%d",
buffer, strlen(buffer), written_len, ecode);
log_err(-1, func_name, log_buffer);
}
}
#endif
/**
* @brief
* add_bad_list
* add new bad file message to bad file messages list
*
* @param[in/out] pbl - list of bad file messages
* @param[in] newtext - new bad file message
* @param[in] nl - number of prefix new-lines
*
* @return void
*
* @note: if <pbl> is non-NULL then it will be reallocated based on length of
* <newtext> or if <pbl> is NULL then it will be allocated
* So, caller should free <pbl> after use
*
*/
void
add_bad_list(char **pbl, char *newtext, int nl)
{
int needed = 0;
char *pnew = NULL;
if (*pbl) {
needed += strlen(*pbl) + strlen(newtext) + nl + 1;
pnew = realloc(*pbl, needed);
} else {
needed += strlen(newtext) + nl + 1;
pnew = malloc(needed);
if (pnew)
*pnew = '\0';
}
if (pnew == NULL) {
log_err(errno, __func__, "Failed to allocate memory");
return;
}
*pbl = pnew;
while (nl--) /* prefix new-lines */
(void)strcat(*pbl, "\n");
(void)strcat(*pbl, newtext);
return;
}
/**
* @brief
* is_child_path
* check if provided <path> specifies location under specified directory <dir>
*
* @param[in] dir - directory path
* @param[in] path - path to check
*
* @return int
* @retval 1 - given path is child of dir
* @retval 0 - given not a child path
* @retval -1 - error encountered
*/
int
is_child_path(char *dir, char *path)
{
char fullpath[2*MAXPATHLEN+2] = {'\0'};
char *dir_real = NULL;
char *fullpath_real = NULL;
char *pos = NULL;
int return_value = 0;
/* if file path is relative, combine it with directory */
if (!is_full_path(path)) {
snprintf(fullpath, sizeof(fullpath), "%s/%s", dir, path);
} else {
snprintf(fullpath, sizeof(fullpath), "%s", path);
}
#ifdef WIN32
dir_real = malloc(sizeof(char) * (MAXPATHLEN + 1));
fullpath_real = malloc(sizeof(char) * (2 * MAXPATHLEN + 2));
#else
dir_real = realpath(dir, NULL);
fullpath_real = realpath(fullpath, NULL);
#endif
if (dir_real == NULL || fullpath_real == NULL) {
log_err(errno, __func__, "Failed to allocate memory");
return_value = -1;
goto error_exit;
}
/* even if the file path is relative to some directory, */
/* it may use /../../ notation and escape that directory, */
/* so always perform full check of parent-child directory relation */
#ifdef WIN32
forward2back_slash(fullpath);
strncpy(dir_real, lpath2short(dir), MAXPATHLEN);
strncpy(fullpath_real, lpath2short(fullpath), 2 * MAXPATHLEN + 1);
#endif
/* check that fullpath_real begins with dir_real */
if (strlen(dir_real) && strlen(fullpath_real)) {
pos = strstr(fullpath_real, dir_real);
if (pos == fullpath_real) {
return_value = 1;
}
}
error_exit:
free(dir_real);
free(fullpath_real);
return return_value;
}
/**
* @brief wchost_match
* Wild card host name match.
* Do a case insensitive compare since hostnames are not case sensitive.
*
* @param[in] can - stage request hostname
* @param[in] master - name from usecp list (may be wild carded at beginning)
*
* @return int
* @retval 1 - can"idate" matches master name
* @retval 0 - not a match
*
*/
int
wchost_match(const char *can, const char *master)
{
const char *pc;
const char *pm;
if ((can == NULL) || (master == NULL))
return 0;
pc = can + strlen(can) - 1;
pm = master + strlen(master) - 1;
while ((pc > can) && (pm > master)) {
if (tolower(*pc) != tolower(*pm))
return 0;
pc--;
pm--;
}
/* comparison of one or both reached the start of the string */
if (pm == master) {
if (*pm == '*')
return 1;
else if ((pc == can) && (tolower(*pc) == tolower(*pm)))
return 1;
}
return 0;
}
#ifdef NAS /* localmod 009 */
/**
* @brief
* Wild card suffix host name match. Do a case insensitive compare since
* hostnames are not case sensitive.
*
* @param can stage request hostname
* @param master name from usecp list (may be wild carded at end)
*
* @return int
* @retval 1 candidate matches master name
* @retval 0 not a match
*
*/
static int
wcsuffix_host_match(can, master)
const char *can;
const char *master;
{
int cmp_length;
int master_length = strlen(master);
if (master[master_length - 1] == '*') {
cmp_length = master_length - 1;
}
else {
cmp_length = master_length;
}
if (strncasecmp(can, master, cmp_length) == 0)
return 1;
return 0;
}
#endif /* localmod 009 */
/**
* @brief
* told_to_cp - Check a stage file request against the saved set of "usecp" paths.
*
* @param[in] host - stage request hostname
* @param[in] oldpath - stage request path
* @param[out] newpath - pointer to "usecp" path
*
* @return int
* @retval 1 - file matched, newpath is updated
* @retval 0 - no match, newpath is unchanged
*
*/
int
told_to_cp(char *host, char *oldpath, char **newpath)
{
int i = 0;
int nh = 0;
static char newp[MAXPATHLEN+1] = {'\0'};
#ifdef NAS /* localmod 009 */
extern struct cphosts *pcphosts;
int match_found = 0;
for (nh = 0; nh < cphosts_num; nh++) {
if (wchost_match(host, (pcphosts+nh)->cph_hosts) ||
wcsuffix_host_match(host, (pcphosts+nh)->cph_hosts)) {
i = strlen((pcphosts+nh)->cph_from);
if (PATHCMP((pcphosts+nh)->cph_from, oldpath, i) == 0) {
if ((pcphosts+nh)->cph_exclude)
return 0;
match_found = 1;
(void)strcpy(newp, (pcphosts+nh)->cph_to);
(void)strcat(newp, oldpath+i);
}
}
}
if (match_found) {
*newpath = newp;
}
return match_found;
#else
for (nh = 0; nh < cphosts_num; nh++) {
if (wchost_match(host, (pcphosts+nh)->cph_hosts)) {
i = strlen((pcphosts+nh)->cph_from);
if (PATHCMP((pcphosts+nh)->cph_from, oldpath, i) == 0) {
(void)strcpy(newp, (pcphosts+nh)->cph_to);
(void)strcat(newp, oldpath+i);
*newpath = newp;
return 1;
}
}
}
return 0;
#endif /* localmod 009 */
}
/**
* @brief
* local_or_remote - Decide if the specified path is to a local or remote file.
*
* @param[in] path - pointer to stage path string
*
* @return int
* @retval 1 - if given path is remote path
* @retval 0 - if given path is local path
*
* @note This function will updates the path pointer to just the path name if local.
*
*/
int
local_or_remote(char **path)
{
int len = 0;
char *pcolon = NULL;
pcolon = strchr(*path, (int)':');
if (pcolon == NULL)
return 0;
*pcolon = '\0';
len = strlen(*path);
#ifdef WIN32
if (IS_UNCPATH(pcolon+1)) {
/*
* UNC path found
* treat as local path and
* remove given hostname part from path
*/
*pcolon = ':';
*path = pcolon+1;
return 0;
} else if (told_to_cp(*path, pcolon+1, path))
#else
if (told_to_cp(*path, pcolon+1, path))
#endif
{
/* path updated in told_to_cp() */
*pcolon = ':';
return 0;
} else if ((strcasecmp("localhost", *path) ==0) ||
((strncasecmp(mom_host, *path, len) == 0) &&
((mom_host[len] == '\0') || (mom_host[len] == '.')))) {
/* we have a host match, file is local */
*pcolon = ':';
*path = pcolon+1;
return 0;
} else {
/* remote file */
*pcolon = ':';
return 1;
}
}
/**
* @brief
* Setup for direct write of spool file
* @par
* Determines if a spool file is to be directly written to its final destination, i.e:
* 1. Direct write of spool files has been requested by the job, and
* 2. Final destination of the file maps to a locally-mounted directory, either because it is
* explicitly mapped by $usecp, or the destination hostname is Mom's host.
*
* @param[in] pjob - pointer to job structure
* @param[in] which - identifies which file: StdOut, StdErr, or Chkpt.
* @param[out] path - pointer to array of size MAPATHXLEN+1 into which the final path of the file is to be stored.
* @param[out] direct_write_possible - Determines whether direct_write is possible.
* Useful to decide whether to write warning message to stderr file
* after multiple function invocation.
*
* @return int
*
* @retval 0 if file is not to be directly written,
* @retval 1 otherwise.
*
* @par
* If the file is not to be directly written (return zero), the contents of *path are unchanged.
* @par
* Direct write of checkpoint files is not currently supported.
*
* @par @par MT-safe: No
*
*/
int
is_direct_write(job *pjob, enum job_file which, char *path, int *direct_write_possible)
{
char working_path[MAXPATHLEN + PBS_MAXSVRJOBID + 3 + 1];
char *p = working_path;
if (which == Chkpt) return(0); /* direct write of checkpoint not supported */
/* Check if direct_write requested. */
if (!((pjob->ji_wattr[(int)JOB_ATR_keep].at_flags & ATR_VFLAG_SET) &&
(strchr(pjob->ji_wattr[(int)JOB_ATR_keep].at_val.at_str, 'd'))))
return(0);
/* Figure out what the final destination path is */
switch(which)
{
case StdOut:
if(!strchr(pjob->ji_wattr[(int)JOB_ATR_keep].at_val.at_str, 'o'))
return(0);
else
/* Make local working copy of path for call to local_or_remote */
snprintf(working_path, MAXPATHLEN + 1, "%s", pjob->ji_wattr[JOB_ATR_outpath].at_val.at_str);
if (
#ifdef WIN32
working_path[strlen(working_path) -1] == '\\'
#else
working_path[strlen(working_path) -1] == '/'
#endif
) {
strcat(working_path, pjob->ji_qs.ji_jobid);
strcat(working_path, JOB_STDOUT_SUFFIX);
}
break;
case StdErr:
if(!strchr(pjob->ji_wattr[(int)JOB_ATR_keep].at_val.at_str, 'e'))
return(0);
else
/* Make local working copy of path for call to local_or_remote */
snprintf(working_path, MAXPATHLEN + 1, "%s", pjob->ji_wattr[JOB_ATR_errpath].at_val.at_str);
if (
#ifdef WIN32
working_path[strlen(working_path) -1] == '\\'
#else
working_path[strlen(working_path) -1] == '/'
#endif
) {
strcat(working_path, pjob->ji_qs.ji_jobid);
strcat(working_path, JOB_STDERR_SUFFIX);
}
break;
default:
return(0);
}
if (local_or_remote(&p) == 1) {
*direct_write_possible = 0;
if (pjob->ji_hosts != NULL) {
log_eventf(PBSEVENT_DEBUG3,
PBS_EVENTCLASS_JOB, LOG_DEBUG, pjob->ji_qs.ji_jobid,
"Direct write is requested for job: %s, but the destination: %s is not usecp-able from %s",
pjob->ji_qs.ji_jobid, p,
pjob->ji_hosts[pjob->ji_nodeid].hn_host);
} else {
/* When a job is requeued and later run, the ji_hosts
* information is not available when this function is
* called as part of req_mvjobfile
*/
log_eventf(PBSEVENT_DEBUG3,
PBS_EVENTCLASS_JOB, LOG_DEBUG, pjob->ji_qs.ji_jobid,
"Direct write is requested for job: %s, but the destination: %s is not usecp-able",
pjob->ji_qs.ji_jobid, p);
}
return (0);
}
if (strlen(p) > MAXPATHLEN) {
*direct_write_possible = 0;
sprintf(log_buffer,
"Direct write is requested for job: %s, but the destination path is longer than %d",
pjob->ji_qs.ji_jobid, MAXPATHLEN);
log_event(PBSEVENT_DEBUG3,
PBS_EVENTCLASS_JOB, LOG_DEBUG, pjob->ji_qs.ji_jobid, log_buffer);
return (0);
}
/* Destination maps to local directory - final path is in working_path. */
snprintf(path, MAXPATHLEN + 1, "%s", p);
return(1);
}
#ifdef WIN32
/**
* @brief
* remtree - wrapper function to remove a tree (or single file) to support for UNC path
*
* @param[in] dirname - path of single file or dir to remove
*
* @return int
* @retval 0 - success
* @retval -1 - failure
*
*/
int
remtree(char *dirname)
{
int rtnv = 0;
char unipath[MAXPATHLEN+1] = {'\0'};
int unmap = 0;
char map_drive[MAXPATHLEN+1] = {'\0'};
if (dirname != NULL && *dirname != '\0') {
replace(dirname, "\\ ", " ", unipath);
forward2back_slash(unipath);
} else {
log_event(PBSEVENT_ERROR, PBS_EVENTCLASS_FILE, LOG_ERR, __func__, "directory or file path is NULL");
return -1;
}
unmap = get_localpath(unipath, map_drive);
rtnv = remdir(unipath);
if (unmap)
unmap_unc_path(map_drive);
return rtnv;
}
/**
* @brief
* remove a tree (or single file)
*
* @param[in] path - path of single file or dir to remove
*
* @return int
* @retval 0 on success
* @retval -1 on failure
*
*/
int
remdir(char *dirname)
#else /* WIN32 */
int
remtree(char *dirname)
#endif
{
#ifdef WIN32
static char id[] = "remdir";
#else
static char id[] = "remtree";
#endif
DIR *dir = NULL;
struct dirent *pdir = NULL;
char namebuf[MAXPATHLEN] = {'\0'};
char *filnam = NULL;
int i = 0;
int rtnv = 0;
struct stat sb = {0};
if (lstat(dirname, &sb) == -1) {
if (errno != ENOENT) {
sprintf(log_buffer, "lstat: %s", dirname);
log_err(errno, id, log_buffer);
}
return -1;
}
if (S_ISDIR(sb.st_mode)) {
if ((dir = opendir(dirname)) == NULL) {
if (errno != ENOENT) {
sprintf(log_buffer, "opendir: %s", dirname);
log_err(errno, id, log_buffer);
}
return -1;
}
(void)strcpy(namebuf, dirname);
#ifdef WIN32
(void)strcat(namebuf, "\\");
#else
(void)strcat(namebuf, "/");
#endif
i = strlen(namebuf);
filnam = &namebuf[i];
while (errno = 0, (pdir = readdir(dir)) != NULL) {
if (pdir->d_name[0] == '.') {
if (pdir->d_name[1] == '\0' ||
(pdir->d_name[1] == '.' &&
pdir->d_name[2] == '\0'))
continue;
}
(void)strcpy(filnam, pdir->d_name);
#ifdef WIN32
rtnv = remdir(namebuf);
#else
rtnv = remtree(namebuf);
#endif
if (rtnv == -1) {
errno = 0;
break;
}
}
if (errno != 0 && errno != ENOENT) {
sprintf(log_buffer, "readdir: %s", dirname);
log_err(errno, id, log_buffer);
(void)closedir(dir);
return -1;
}
(void)closedir(dir);
if (rmdir(dirname) < 0) {
if (errno != ENOENT) {
sprintf(log_buffer, "rmdir: %s", dirname);
log_err(errno, id, log_buffer);
rtnv = -1;
}
}
} else if (unlink(dirname) < 0) {
sprintf(log_buffer, "unlink: %s", dirname);
log_err(errno, id, log_buffer);
rtnv = -1;
}
return rtnv;
}
/**
* @brief
* pbs_glob - check whether given pattern matches in filename
* pattern matches:
* * matches zero or more characters
* ? matches any single character
* char matches itself except where char is '*' or '?'
*
* @param[in] filen - filename
* @param[in] pat - pattern to match
*
* @return Boolean
* @retval TRUE - if pattern matches
* @retval FALSE - if no match
*
*/
int
pbs_glob(char *filen, char *pat)
{
int c = 0;
while (*pat) {
if (!*filen && *pat != '*')
return FALSE;
c = *pat++;
switch (c) {
case '*':
while (*pat == '*')
pat++;
if (!*pat)
return TRUE;
if (*pat != '?') {
while (*filen && *pat != *filen)
filen++;
}
while (*filen) {
if (pbs_glob(filen, pat))
return TRUE;
filen++;
}
return FALSE;
case '?':
if (*filen)
break;
return FALSE;
default:
if (c != *filen)
return FALSE;
break;
}
filen++;
}
return !*filen;
}
/**
* @brief
* copy_file - Do a single staging file copy.
*
* @param[in] dir - direction of copy
* STAGE_DIR_IN - for stage in request
* STAGE_DIR_OUT - for stageout request
* @param[in] rmtflag - is remote file copy
* @param[in] owner - username for owner of copy request
* @param[in] src - path to source is stageout else local file name
* @param[in] pair - list of file pair
* @param[in] conn - socket on which request is received
* @param[in/out] stage_inout - pointer to cpy_files struct
* @param[in] prmt - path to destination if stageout else source path
*
* @return int
* @retval 0 - all OK
* @retval !0 - error
*
*/
int
copy_file(int dir, int rmtflag, char *owner, char *src, struct rqfpair *pair, int conn, cpy_files *stage_inout, char *prmt)
{
int rc = 0;
int ret = 0;
int len = 0;
struct stat buf = {0};
char dest[MAXPATHLEN+1] = {'\0'};
char src_file[MAXPATHLEN+1] = {'\0'};
/*
** The destination is calcluated for a stagein so it can
** be used later. It does not need to be passed to sys_copy.
*/
if (dir == STAGE_DIR_IN) {
/* if destination is a directory, append filename */
#ifdef WIN32
if (stat_uncpath(pair->fp_local, &buf) == 0 && S_ISDIR(buf.st_mode))
#else
if (stat(pair->fp_local, &buf) == 0 && S_ISDIR(buf.st_mode))
#endif
{
char *slash = strrchr(src, '/');
strcpy(dest, pair->fp_local);
strcat(dest, "/");
strcat(dest, (slash != NULL) ? slash + 1 : src);
}
else
strcpy(dest, pair->fp_local);
}
ret = sys_copy(dir, rmtflag, owner, src, pair, conn, prmt);
if (ret == 0) {
/*
** Copy worked. If old behavior is used, a stageout file
** is deleted now. New behavior of waiting to delete
** everything could be achived by adding the file to
** a list to delete later.
*/
if (dir == STAGE_DIR_OUT) {
/*
** have copied out, may need to remove local file
** if sandbox=private then the file is not removed here
** it will be removed when the sandbox directory is removed
*/
if (!(stage_inout->sandbox_private &&
is_child_path(pbs_jobdir, src) == 1)) {
/* Check if local file path has comma in it, if
* found escape character prefixed will be
* removed
*/
replace(src, "\\,", ",", src_file);
if (*src_file == '\0')
strcpy(src_file, src);
if (remtree(src_file) < 0) {
if (errno == ENOENT) {
log_event(PBSEVENT_ADMIN,
PBS_EVENTCLASS_FILE,
LOG_INFO, src,
"previously removed");
} else {
char temp[80 + MAXPATHLEN];
snprintf(temp, sizeof(temp),
msg_err_unlink,
"stage out", src);
log_err(errno, "req_cpyfile",
temp);
add_bad_list(&(stage_inout->bad_list),
temp, 2);
stage_inout->bad_files = 1;
}
}
}
}
else {
/*
** Add destination (local) filename to list so it can
** be deleted on later failure.
*/
char temp[80 + MAXPATHLEN];
char **stage_file_list_temp = NULL;
if (stage_inout->file_max == stage_inout->file_num) { /* need to extend list */
stage_inout->file_max += 10;
if((stage_file_list_temp = (char **)realloc(stage_inout->file_list, stage_inout->file_max * sizeof(char **))) == NULL) {
snprintf(temp, sizeof(temp), "Out of Memory!");
log_err(ENOMEM, "req_cpyfile", temp);
return -1;
} else {
stage_inout->file_list = stage_file_list_temp;
}
}
DBPRT(("%s: listadd %s\n", __func__, dest))
if ((stage_inout->file_list[stage_inout->file_num++] = strdup(dest)) == NULL) {
snprintf(temp, sizeof(temp), "Out of Memory!");
log_err(ENOMEM, "req_cpyfile", temp);
return -1;
}
}
}
else { /* failure */
FILE *fp = NULL;
DBPRT(("%s: sys_copy failed, error = %d\n", __func__, ret))
snprintf(log_buffer, sizeof(log_buffer), "sys_copy failed with status=%d", ret);
log_event(PBSEVENT_ERROR, PBS_EVENTCLASS_FILE, LOG_ERR, __func__, log_buffer);
stage_inout->bad_files = 1;
snprintf(log_buffer, sizeof(log_buffer), "Unable to copy file %s %s %s",
(dir == STAGE_DIR_IN) ? dest : src,
(dir == STAGE_DIR_IN) ? "from" : "to",
(dir == STAGE_DIR_IN) ? src : pair->fp_rmt);
add_bad_list(&(stage_inout->bad_list), log_buffer, 2);
log_event(PBSEVENT_ADMIN, PBS_EVENTCLASS_FILE, LOG_INFO,
pair->fp_local, log_buffer);
/* copy message from rcp as well */
if ((fp = fopen(rcperr, "r")) != NULL) {
add_bad_list(&(stage_inout->bad_list), ">>> error from copy", 1);
while (fgets(log_buffer, LOG_BUF_SIZE, fp) != NULL) {
len = strlen(log_buffer) - 1;
/* strip up to 2 line endings */
if (len >= 0) {
if (log_buffer[len] == '\n' ||
log_buffer[len] == '\r')
log_buffer[len] = '\0';
len--;
if (len >= 0) {
if (log_buffer[len] == '\n' ||
log_buffer[len] == '\r')
log_buffer[len] = '\0';
}
}
add_bad_list(&(stage_inout->bad_list), log_buffer, 1);
log_event(PBSEVENT_ADMIN, PBS_EVENTCLASS_FILE,
LOG_INFO, pair->fp_local, log_buffer);
}
fclose(fp);
add_bad_list(&(stage_inout->bad_list), ">>> end error output", 1);
} else {
log_errf(errno, __func__, "Failed to open %s file", rcperr);
}
if (dir == STAGE_DIR_IN)
rc = -1;
else {
#ifndef NO_SPOOL_OUTPUT
if (stage_inout->from_spool == 1) { /* copy out of spool */
char undelname[MAXPATHLEN+1];
len = strlen(path_spool);
strcpy(undelname, path_undeliv);
strcat(undelname, src+len); /* src path begins with spool */
if (rename(src, undelname) == 0) { /* move file to undelivered */
add_bad_list(&(stage_inout->bad_list), "Output retained on that host in: ", 1);
add_bad_list(&(stage_inout->bad_list), undelname, 0);
}
else {
char temp[80+2*MAXPATHLEN];
sprintf(temp, "Unable to rename %s to %s",
src, undelname);
log_err(errno, "req_cpyfile", temp);
}
}
#endif /* NO_SPOOL_OUTPUT */
if (is_child_path(pbs_jobdir, src) == 1)
stage_inout->stageout_failed = TRUE;
}
}
unlink(rcperr);
return rc;
}
/**
* @brief
* stage_file - Handle file stage pair. The source could have a wildcard
* which would result in multipile copies.
*
* @param[in] dir - direction of copy
* STAGE_DIR_IN - for stage in request
* STAGE_DIR_OUT - for stageout request
* @param[in] rmtflag - is remote file copy
* @param[in] owner - username for owner of copy request
* @param[in] pair - list of file pair
* @param[in] conn - socket on which request is received
* @param[in/out] stage_inout - pointer cpy_files struct
* @param[in] prmt - path to destination if stageout else source path
*
* @return int
* @retval 0 - all OK
* @retval !0 - error
*
*/
int
stage_file(int dir, int rmtflag, char *owner, struct rqfpair *pair, int conn, cpy_files *stage_inout, char *prmt)
{
char *ps = NULL;
int i = 0;
int rc = 0;
int len = 0;
char dname[MAXPATHLEN+1] = {'\0'};
char source[MAXPATHLEN+1] = {'\0'};
char matched[MAXPATHLEN+1] = {'\0'};
DIR *dirp = NULL;
struct dirent *pdirent = NULL;
struct stat statbuf;
DBPRT(("%s: entered local %s remote %s\n", __func__, pair->fp_local, prmt))
/*
* figure out the source path
*/
if (dir == STAGE_DIR_OUT) {
source[0] = '\0';
if (pair->fp_flag == STDJOBFILE) {
#ifndef NO_SPOOL_OUTPUT
/* stdout | stderr from MOM's spool area */