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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
|
/* $OpenBSD: util.c,v 1.58 2005/12/03 15:07:21 joris Exp $ */
/*
* Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
* 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, this list of conditions and the following disclaimer.
* 2. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``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 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.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <md5.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "cvs.h"
#include "log.h"
#if !defined(RCSPROG)
/* letter -> mode type map */
static const int cvs_modetypes[26] = {
-1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1,
-1, 2, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1,
};
/* letter -> mode map */
static const mode_t cvs_modes[3][26] = {
{
0, 0, 0, 0, 0, 0, 0, /* a - g */
0, 0, 0, 0, 0, 0, 0, /* h - m */
0, 0, 0, S_IRUSR, 0, 0, 0, /* n - u */
0, S_IWUSR, S_IXUSR, 0, 0 /* v - z */
},
{
0, 0, 0, 0, 0, 0, 0, /* a - g */
0, 0, 0, 0, 0, 0, 0, /* h - m */
0, 0, 0, S_IRGRP, 0, 0, 0, /* n - u */
0, S_IWGRP, S_IXGRP, 0, 0 /* v - z */
},
{
0, 0, 0, 0, 0, 0, 0, /* a - g */
0, 0, 0, 0, 0, 0, 0, /* h - m */
0, 0, 0, S_IROTH, 0, 0, 0, /* n - u */
0, S_IWOTH, S_IXOTH, 0, 0 /* v - z */
}
};
/* octal -> string */
static const char *cvs_modestr[8] = {
"", "x", "w", "wx", "r", "rx", "rw", "rwx"
};
/*
* cvs_readrepo()
*
* Read the path stored in the `Repository' CVS file for a given directory
* <dir>, and store that path into the buffer pointed to by <dst>, whose size
* is <len>.
*/
int
cvs_readrepo(const char *dir, char *dst, size_t len)
{
size_t dlen, l;
FILE *fp;
char repo_path[MAXPATHLEN];
l = cvs_path_cat(dir, "CVS/Repository", repo_path, sizeof(repo_path));
if (l >= sizeof(repo_path))
return (NULL);
fp = fopen(repo_path, "r");
if (fp == NULL)
return (-1);
if (fgets(dst, (int)len, fp) == NULL) {
if (ferror(fp)) {
cvs_log(LP_ERRNO, "failed to read from `%s'",
repo_path);
}
(void)fclose(fp);
return (-1);
}
dlen = strlen(dst);
if ((dlen > 0) && (dst[dlen - 1] == '\n'))
dst[--dlen] = '\0';
(void)fclose(fp);
return (0);
}
/*
* cvs_strtomode()
*
* Read the contents of the string <str> and generate a permission mode from
* the contents of <str>, which is assumed to have the mode format of CVS.
* The CVS protocol specification states that any modes or mode types that are
* not recognized should be silently ignored. This function does not return
* an error in such cases, but will issue warnings.
* Returns 0 on success, or -1 on failure.
*/
int
cvs_strtomode(const char *str, mode_t *mode)
{
char type;
mode_t m;
char buf[32], ms[4], *sp, *ep;
m = 0;
if (strlcpy(buf, str, sizeof(buf)) >= sizeof(buf)) {
return (-1);
}
sp = buf;
ep = sp;
for (sp = buf; ep != NULL; sp = ep + 1) {
ep = strchr(sp, ',');
if (ep != NULL)
*ep = '\0';
memset(ms, 0, sizeof ms);
if (sscanf(sp, "%c=%3s", &type, ms) != 2 &&
sscanf(sp, "%c=", &type) != 1) {
cvs_log(LP_WARN, "failed to scan mode string `%s'", sp);
continue;
}
if ((type <= 'a') || (type >= 'z') ||
(cvs_modetypes[type - 'a'] == -1)) {
cvs_log(LP_WARN,
"invalid mode type `%c'"
" (`u', `g' or `o' expected), ignoring", type);
continue;
}
/* make type contain the actual mode index */
type = cvs_modetypes[type - 'a'];
for (sp = ms; *sp != '\0'; sp++) {
if ((*sp <= 'a') || (*sp >= 'z') ||
(cvs_modes[(int)type][*sp - 'a'] == 0)) {
cvs_log(LP_WARN,
"invalid permission bit `%c'", *sp);
} else
m |= cvs_modes[(int)type][*sp - 'a'];
}
}
*mode = m;
return (0);
}
/*
* cvs_modetostr()
*
* Generate a CVS-format string to represent the permissions mask on a file
* from the mode <mode> and store the result in <buf>, which can accept up to
* <len> bytes (including the terminating NUL byte). The result is guaranteed
* to be NUL-terminated.
* Returns 0 on success, or -1 on failure.
*/
int
cvs_modetostr(mode_t mode, char *buf, size_t len)
{
size_t l;
char tmp[16], *bp;
mode_t um, gm, om;
um = (mode & S_IRWXU) >> 6;
gm = (mode & S_IRWXG) >> 3;
om = mode & S_IRWXO;
bp = buf;
*bp = '\0';
l = 0;
if (um) {
snprintf(tmp, sizeof(tmp), "u=%s", cvs_modestr[um]);
l = strlcat(buf, tmp, len);
}
if (gm) {
if (um)
strlcat(buf, ",", len);
snprintf(tmp, sizeof(tmp), "g=%s", cvs_modestr[gm]);
strlcat(buf, tmp, len);
}
if (om) {
if (um || gm)
strlcat(buf, ",", len);
snprintf(tmp, sizeof(tmp), "o=%s", cvs_modestr[gm]);
strlcat(buf, tmp, len);
}
return (0);
}
/*
* cvs_cksum()
*
* Calculate the MD5 checksum of the file whose path is <file> and generate
* a CVS-format 32 hex-digit string, which is stored in <dst>, whose size is
* given in <len> and must be at least 33.
* Returns 0 on success, or -1 on failure.
*/
int
cvs_cksum(const char *file, char *dst, size_t len)
{
if (len < CVS_CKSUM_LEN) {
cvs_log(LP_WARN, "buffer too small for checksum");
return (-1);
}
if (MD5File(file, dst) == NULL) {
cvs_log(LP_ERRNO, "failed to generate checksum for %s", file);
return (-1);
}
return (0);
}
/*
* cvs_splitpath()
*
* Split a path <path> into the base portion and the filename portion.
* The path is copied in <base> and the last delimiter is replaced by a NUL
* byte. The <file> pointer is set to point to the first character after
* that delimiter.
* Returns 0 on success, or -1 on failure.
*/
int
cvs_splitpath(const char *path, char *base, size_t blen, char **file)
{
size_t rlen;
char *sp;
if ((rlen = strlcpy(base, path, blen)) >= blen)
return (-1);
while ((rlen > 0) && (base[rlen - 1] == '/'))
base[--rlen] = '\0';
sp = strrchr(base, '/');
if (sp == NULL) {
strlcpy(base, "./", blen);
strlcat(base, path, blen);
sp = base + 1;
}
*sp = '\0';
if (file != NULL)
*file = sp + 1;
return (0);
}
/*
* cvs_getargv()
*
* Parse a line contained in <line> and generate an argument vector by
* splitting the line on spaces and tabs. The resulting vector is stored in
* <argv>, which can accept up to <argvlen> entries.
* Returns the number of arguments in the vector, or -1 if an error occurred.
*/
int
cvs_getargv(const char *line, char **argv, int argvlen)
{
u_int i;
int argc, err;
char linebuf[256], qbuf[128], *lp, *cp, *arg;
strlcpy(linebuf, line, sizeof(linebuf));
memset(argv, 0, argvlen * sizeof(char *));
argc = 0;
/* build the argument vector */
err = 0;
for (lp = linebuf; lp != NULL;) {
if (*lp == '"') {
/* double-quoted string */
lp++;
i = 0;
memset(qbuf, 0, sizeof(qbuf));
while (*lp != '"') {
if (*lp == '\\')
lp++;
if (*lp == '\0') {
cvs_log(LP_ERR, "no terminating quote");
err++;
break;
}
qbuf[i++] = *lp++;
if (i == sizeof(qbuf)) {
err++;
break;
}
}
arg = qbuf;
} else {
cp = strsep(&lp, " \t");
if (cp == NULL)
break;
else if (*cp == '\0')
continue;
arg = cp;
}
if (argc == argvlen) {
err++;
break;
}
argv[argc] = strdup(arg);
if (argv[argc] == NULL) {
cvs_log(LP_ERRNO, "failed to copy argument");
err++;
break;
}
argc++;
}
if (err != 0) {
/* ditch the argument vector */
for (i = 0; i < (u_int)argc; i++)
free(argv[i]);
argc = -1;
}
return (argc);
}
/*
* cvs_makeargv()
*
* Allocate an argument vector large enough to accommodate for all the
* arguments found in <line> and return it.
*/
char **
cvs_makeargv(const char *line, int *argc)
{
int i, ret;
char *argv[1024], **copy;
size_t size;
ret = cvs_getargv(line, argv, 1024);
if (ret == -1)
return (NULL);
size = (ret + 1) * sizeof(char *);
copy = (char **)malloc(size);
if (copy == NULL) {
cvs_log(LP_ERRNO, "failed to allocate argument vector");
cvs_freeargv(argv, ret);
return (NULL);
}
memset(copy, 0, size);
for (i = 0; i < ret; i++)
copy[i] = argv[i];
copy[ret] = NULL;
*argc = ret;
return (copy);
}
/*
* cvs_freeargv()
*
* Free an argument vector previously generated by cvs_getargv().
*/
void
cvs_freeargv(char **argv, int argc)
{
int i;
for (i = 0; i < argc; i++)
if (argv[i] != NULL)
free(argv[i]);
}
/*
* cvs_mkadmin()
*
* Create the CVS administrative files within the directory <cdir>. If the
* files already exist, they are kept as is.
* Returns 0 on success, or -1 on failure.
*/
int
cvs_mkadmin(const char *dpath, const char *rootpath, const char *repopath,
char *tag, char *date, int nb)
{
size_t l;
char path[MAXPATHLEN];
FILE *fp;
CVSENTRIES *ef;
struct stat st;
cvs_log(LP_TRACE, "cvs_mkadmin(%s, %s, %s, %s, %s, %d)",
dpath, rootpath, repopath, tag ? tag : "", date ? date : "", nb);
l = cvs_path_cat(dpath, CVS_PATH_CVSDIR, path, sizeof(path));
if (l >= sizeof(path))
return (-1);
if ((mkdir(path, 0755) == -1) && (errno != EEXIST)) {
cvs_log(LP_ERRNO, "failed to create directory %s", path);
return (-1);
}
/* just create an empty Entries file */
ef = cvs_ent_open(dpath, O_WRONLY);
if (ef != NULL)
cvs_ent_close(ef);
l = cvs_path_cat(dpath, CVS_PATH_ROOTSPEC, path, sizeof(path));
if (l >= sizeof(path))
return (-1);
if ((stat(path, &st) == -1) && (errno == ENOENT)) {
fp = fopen(path, "w");
if (fp == NULL) {
cvs_log(LP_ERRNO, "failed to open %s", path);
return (-1);
}
if (rootpath != NULL)
fprintf(fp, "%s\n", rootpath);
(void)fclose(fp);
}
l = cvs_path_cat(dpath, CVS_PATH_REPOSITORY, path, sizeof(path));
if (l >= sizeof(path))
return (-1);
if ((stat(path, &st) == -1) && (errno == ENOENT)) {
fp = fopen(path, "w");
if (fp == NULL) {
cvs_log(LP_ERRNO, "failed to open %s", path);
return (-1);
}
if (repopath != NULL)
fprintf(fp, "%s\n", repopath);
(void)fclose(fp);
}
/* create CVS/Tag file (if needed) */
/* XXX correct? */
if (tag != NULL || date != NULL)
(void)cvs_write_tagfile(tag, date, nb);
return (0);
}
/*
* cvs_exec()
*/
int
cvs_exec(int argc, char **argv, int fds[3])
{
int ret;
pid_t pid;
if ((pid = fork()) == -1) {
cvs_log(LP_ERRNO, "failed to fork");
return (-1);
} else if (pid == 0) {
execvp(argv[0], argv);
cvs_log(LP_ERRNO, "failed to exec %s", argv[0]);
exit(1);
}
if (waitpid(pid, &ret, 0) == -1)
cvs_log(LP_ERRNO, "failed to waitpid");
return (ret);
}
/*
* cvs_chdir()
*
* Change to directory.
* chdir() wrapper with an error message.
* Returns 0 on success, or -1 on failure.
*/
int
cvs_chdir(const char *path)
{
if (chdir(path) == -1) {
cvs_log(LP_ERRNO, "cannot change to dir `%s'", path);
return (-1);
}
return (0);
}
/*
* cvs_rename()
* Change the name of a file.
* rename() wrapper with an error message.
* Returns 0 on success, or -1 on failure.
*/
int
cvs_rename(const char *from, const char *to)
{
cvs_log(LP_TRACE, "cvs_rename(%s,%s)", from, to);
if (cvs_noexec == 1)
return (0);
if (rename(from, to) == -1) {
cvs_log(LP_ERRNO, "cannot rename file `%s' to `%s'", from, to);
return (-1);
}
return (0);
}
/*
* cvs_unlink()
*
* Removes the link named by <path>.
* unlink() wrapper with an error message.
* Returns 0 on success, or -1 on failure.
*/
int
cvs_unlink(const char *path)
{
cvs_log(LP_TRACE, "cvs_unlink(%s)", path);
if (cvs_noexec == 1)
return (0);
if ((unlink(path) == -1) && (errno != ENOENT)) {
cvs_log(LP_ERRNO, "cannot remove `%s'", path);
return (-1);
}
return (0);
}
/*
* cvs_rmdir()
*
* Remove a directory tree from disk.
* Returns 0 on success, or -1 on failure.
*/
int
cvs_rmdir(const char *path)
{
int ret = -1;
size_t len;
DIR *dirp;
struct dirent *ent;
char fpath[MAXPATHLEN];
cvs_log(LP_TRACE, "cvs_rmdir(%s)", path);
if (cvs_noexec == 1)
return (0);
if ((dirp = opendir(path)) == NULL) {
cvs_log(LP_ERRNO, "failed to open '%s'", path);
return (-1);
}
while ((ent = readdir(dirp)) != NULL) {
if (!strcmp(ent->d_name, ".") ||
!strcmp(ent->d_name, ".."))
continue;
len = cvs_path_cat(path, ent->d_name, fpath, sizeof(fpath));
if (len >= sizeof(fpath))
goto done;
if (ent->d_type == DT_DIR) {
if (cvs_rmdir(fpath) == -1)
goto done;
} else if ((cvs_unlink(fpath) == -1) && (errno != ENOENT))
goto done;
}
if ((rmdir(path) == -1) && (errno != ENOENT)) {
cvs_log(LP_ERRNO, "failed to remove '%s'", path);
goto done;
}
ret = 0;
done:
closedir(dirp);
return (ret);
}
/*
* Create a directory, and the parent directories if needed.
* based upon mkpath() from mkdir.c
*/
int
cvs_create_dir(const char *path, int create_adm, char *root, char *repo)
{
size_t l;
int len, ret;
char *d, *s;
struct stat sb;
char rpath[MAXPATHLEN], entry[MAXPATHLEN];
CVSENTRIES *entf;
struct cvs_ent *ent;
if (create_adm == 1 && (root == NULL)) {
cvs_log(LP_ERR, "missing stuff in cvs_create_dir");
return (-1);
}
if ((s = strdup(path)) == NULL)
return (-1);
rpath[0] = '\0';
if (repo != NULL) {
if (strlcpy(rpath, repo, sizeof(rpath)) >= sizeof(rpath)) {
errno = ENAMETOOLONG;
cvs_log(LP_ERRNO, "%s", rpath);
free(s);
return (-1);
}
if (strlcat(rpath, "/", sizeof(rpath)) >= sizeof(rpath)) {
errno = ENAMETOOLONG;
cvs_log(LP_ERRNO, "%s", rpath);
free(s);
return (-1);
}
}
ret = -1;
entf = NULL;
d = strtok(s, "/");
while (d != NULL) {
if (stat(d, &sb)) {
/* try to create the directory */
if ((errno != ENOENT) || (mkdir(d, 0755) &&
errno != EEXIST)) {
cvs_log(LP_ERRNO, "failed to create `%s'", d);
goto done;
}
} else if (!S_ISDIR(sb.st_mode)) {
cvs_log(LP_ERR, "`%s' not a directory", d);
goto done;
}
/*
* Create administrative files if requested.
*/
if (create_adm == 1) {
l = strlcat(rpath, d, sizeof(rpath));
if (l >= sizeof(rpath))
goto done;
l = strlcat(rpath, "/", sizeof(rpath));
if (l >= sizeof(rpath))
goto done;
if (cvs_mkadmin(d, root, rpath, NULL, NULL, 0) < 0) {
cvs_log(LP_ERR, "failed to create adm files");
goto done;
}
}
/*
* Add it to the parent directory entry file.
* (if any).
*/
entf = cvs_ent_open(".", O_RDWR);
if (entf != NULL && strcmp(d, ".")) {
len = snprintf(entry, sizeof(entry), "D/%s////", d);
if (len == -1 || len >= (int)sizeof(entry)) {
errno = ENAMETOOLONG;
cvs_log(LP_ERRNO, "%s", entry);
goto done;
}
if ((ent = cvs_ent_parse(entry)) == NULL) {
cvs_log(LP_ERR, "failed to parse entry");
goto done;
}
cvs_ent_remove(entf, d, 0);
if (cvs_ent_add(entf, ent) < 0) {
cvs_log(LP_ERR, "failed to add entry");
goto done;
}
}
if (entf != NULL) {
cvs_ent_close(entf);
entf = NULL;
}
/*
* All went ok, switch to the newly created directory.
*/
if (cvs_chdir(d) == -1)
goto done;
d = strtok(NULL, "/");
}
ret = 0;
done:
if (entf != NULL)
cvs_ent_close(entf);
free(s);
return (ret);
}
/*
* cvs_path_cat()
*
* Concatenate the two paths <base> and <end> and store the generated path
* into the buffer <dst>, which can accept up to <dlen> bytes, including the
* NUL byte. The result is guaranteed to be NUL-terminated.
* Returns the number of bytes necessary to store the full resulting path,
* not including the NUL byte (a value equal to or larger than <dlen>
* indicates truncation).
*/
size_t
cvs_path_cat(const char *base, const char *end, char *dst, size_t dlen)
{
size_t len;
len = strlcpy(dst, base, dlen);
if (len >= dlen - 1) {
errno = ENAMETOOLONG;
cvs_log(LP_ERRNO, "%s", dst);
} else {
dst[len] = '/';
dst[len + 1] = '\0';
len = strlcat(dst, end, dlen);
if (len >= dlen) {
errno = ENAMETOOLONG;
cvs_log(LP_ERRNO, "%s", dst);
}
}
return (len);
}
/*
* cvs_rcs_getpath()
*
* Get the RCS path of the file <file> and store it in <buf>, which is
* of size <len>. For portability, it is recommended that <buf> always be
* at least MAXPATHLEN bytes long.
* Returns a pointer to the start of the path on success, or NULL on failure.
*/
char *
cvs_rcs_getpath(CVSFILE *file, char *buf, size_t len)
{
int l;
char *repo;
struct cvsroot *root;
root = CVS_DIR_ROOT(file);
repo = CVS_DIR_REPO(file);
l = snprintf(buf, len, "%s/%s/%s%s",
root->cr_dir, repo, file->cf_name, RCS_FILE_EXT);
if (l == -1 || l >= (int)len) {
errno = ENAMETOOLONG;
cvs_log(LP_ERRNO, "%s", buf);
return (NULL);
}
return (buf);
}
/*
* cvs_write_tagfile()
*
* Write the CVS/Tag file for current directory.
*/
void
cvs_write_tagfile(char *tag, char *date, int nb)
{
FILE *fp;
char tagpath[MAXPATHLEN];
if (cvs_noexec == 1)
return;
if (strlcpy(tagpath, CVS_PATH_TAG, sizeof(tagpath)) >= sizeof(tagpath))
return;
if ((tag != NULL) || (date != NULL)) {
fp = fopen(tagpath, "w+");
if (fp == NULL) {
if (errno != ENOENT)
cvs_log(LP_NOTICE,
"failed to open `%s' : %s", tagpath,
strerror(errno));
return;
}
if (tag != NULL) {
if (nb != 0)
fprintf(fp, "N%s\n", tag);
else
fprintf(fp, "T%s\n", tag);
} else {
fprintf(fp, "D%s\n", date);
}
(void)fclose(fp);
} else {
cvs_unlink(tagpath);
return;
}
}
/*
* cvs_parse_tagfile()
*
* Parse the CVS/Tag file for current directory.
*
* If it contains a branch tag, sets <tagp>.
* If it contains a date, sets <datep>.
* If it contains a non-branch tag, sets <nbp>.
*
* Returns nothing but an error message, and sets <tagp>, <datep> to NULL
* and <nbp> to 0.
*/
void
cvs_parse_tagfile(char **tagp, char **datep, int *nbp)
{
FILE *fp;
int linenum;
size_t len;
char linebuf[128], tagpath[MAXPATHLEN];
if (tagp != NULL)
*tagp = (char *)NULL;
if (datep != NULL)
*datep = (char *)NULL;
if (nbp != NULL)
*nbp = 0;
if (strlcpy(tagpath, CVS_PATH_TAG, sizeof(tagpath)) >= sizeof(tagpath))
return;
fp = fopen(tagpath, "r");
if (fp == NULL) {
if (errno != ENOENT)
cvs_log(LP_NOTICE, "failed to open `%s' : %s", tagpath,
strerror(errno));
return;
}
linenum = 0;
while (fgets(linebuf, (int)sizeof(linebuf), fp) != NULL) {
linenum++;
if ((len = strlen(linebuf)) == 0)
continue;
if (linebuf[len -1] != '\n') {
cvs_log(LP_WARN, "line too long in `%s:%d'", tagpath,
linenum);
break;
}
linebuf[--len] = '\0';
switch (*linebuf) {
case 'T':
if (tagp != NULL)
*tagp = strdup(linebuf);
break;
case 'D':
if (datep != NULL)
*datep = strdup(linebuf);
break;
case 'N':
if (tagp != NULL)
*tagp = strdup(linebuf);
if (nbp != NULL)
*nbp = 1;
break;
default:
break;
}
}
if (ferror(fp))
cvs_log(LP_NOTICE, "failed to read line from `%s'", tagpath);
(void)fclose(fp);
}
#endif /* !RCSPROG */
/*
* Split the contents of a file into a list of lines.
*/
struct cvs_lines *
cvs_splitlines(const char *fcont)
{
char *dcp;
struct cvs_lines *lines;
struct cvs_line *lp;
lines = (struct cvs_lines *)malloc(sizeof(*lines));
if (lines == NULL)
return (NULL);
TAILQ_INIT(&(lines->l_lines));
lines->l_nblines = 0;
lines->l_data = strdup(fcont);
if (lines->l_data == NULL) {
free(lines);
return (NULL);
}
lp = (struct cvs_line *)malloc(sizeof(*lp));
if (lp == NULL) {
cvs_freelines(lines);
return (NULL);
}
lp->l_line = NULL;
lp->l_lineno = 0;
TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
for (dcp = lines->l_data; *dcp != '\0';) {
lp = (struct cvs_line *)malloc(sizeof(*lp));
if (lp == NULL) {
cvs_freelines(lines);
return (NULL);
}
lp->l_line = dcp;
lp->l_lineno = ++(lines->l_nblines);
TAILQ_INSERT_TAIL(&(lines->l_lines), lp, l_list);
dcp = strchr(dcp, '\n');
if (dcp == NULL)
break;
*(dcp++) = '\0';
}
return (lines);
}
void
cvs_freelines(struct cvs_lines *lines)
{
struct cvs_line *lp;
while ((lp = TAILQ_FIRST(&(lines->l_lines))) != NULL) {
TAILQ_REMOVE(&(lines->l_lines), lp, l_list);
free(lp);
}
free(lines->l_data);
free(lines);
}
BUF *
cvs_patchfile(const char *data, const char *patch,
int (*p)(struct cvs_lines *, struct cvs_lines *))
{
struct cvs_lines *dlines, *plines;
struct cvs_line *lp;
size_t len;
int lineno;
BUF *res;
len = strlen(data);
if ((dlines = cvs_splitlines(data)) == NULL)
return (NULL);
if ((plines = cvs_splitlines(patch)) == NULL)
return (NULL);
if (p(dlines, plines) < 0) {
cvs_freelines(dlines);
cvs_freelines(plines);
return (NULL);
}
if ((res = cvs_buf_alloc(len, BUF_AUTOEXT)) == NULL) {
cvs_freelines(dlines);
cvs_freelines(plines);
return (NULL);
}
lineno = 0;
TAILQ_FOREACH(lp, &dlines->l_lines, l_list) {
if (lineno != 0)
cvs_buf_fappend(res, "%s\n", lp->l_line);
lineno++;
}
cvs_freelines(dlines);
cvs_freelines(plines);
return (res);
}
/*
* a hack to mimic and thus match gnu cvs behaviour.
*/
time_t
cvs_hack_time(time_t oldtime, int togmt)
{
int l;
struct tm *t;
char tbuf[32];
if (togmt == 1) {
t = gmtime(&oldtime);
if (t == NULL)
return (0);
return (mktime(t));
}
t = localtime(&oldtime);
l = snprintf(tbuf, sizeof(tbuf), "%d/%d/%d GMT %d:%d:%d",
t->tm_mon + 1, t->tm_mday, t->tm_year + 1900, t->tm_hour,
t->tm_min, t->tm_sec);
if (l == -1 || l >= (int)sizeof(tbuf))
return (0);
return (cvs_date_parse(tbuf));
}
|