4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 #include <sys/param.h>
39 #include "exception_list.h"
44 resolve_relative(const char *source
, const char *link
)
49 static char curpath
[MAXPATHLEN
];
51 /* if absolute path - no relocation required */
53 return (strcpy(curpath
, link
));
55 (void) strcpy(curpath
, source
);
56 p
= rindex(curpath
, '/');
60 l_next
= index(l_pos
, '/');
61 if (strncmp(l_pos
, "../", 3) == 0) {
62 if ((p
= rindex(curpath
, '/')) != NULL
)
66 } else if (strncmp(l_pos
, "./", 2)) {
67 /* if not . then we process */
69 (void) strcat(curpath
, "/");
71 (void) strncat(curpath
, l_pos
,
74 (void) strcat(curpath
, l_pos
);
84 parse_proto_line(const char *basedir
, char *line
, elem_list
*list
, short arch
,
87 char *type
, *class, *file
, *src
, *maj
, *min
, *perm
, *owner
, *group
;
90 static elem
*e
= NULL
;
92 (void) strcpy(p_line
, line
);
94 e
= (elem
*)calloc(1, sizeof (elem
));
98 if (!(type
= strtok(p_line
, FS
))) {
99 (void) fprintf(stderr
, "error: bad line(type) : %s\n", line
);
103 e
->file_type
= type
[0];
105 if ((class = strtok(NULL
, FS
)) == NULL
) {
106 (void) fprintf(stderr
, "error: bad line(class) : %s\n", line
);
111 * Just ignore 'legacy' entries. These are not present in the proto
112 * area at all. They're phantoms.
114 if (strcmp(class, "legacy") == 0)
117 if (!(file
= strtok(NULL
, FS
))) {
118 (void) fprintf(stderr
, "error: bad line(file_name) : %s\n",
124 if ((src
= index(file
, '=')) != NULL
) {
126 * The '=' operator is subtly different for link and non-link
127 * entries. For the hard or soft link case, the left hand side
128 * exists in the proto area and is created by the package.
130 * When the file is an editable file, it's very likely that the
131 * right hand side is only a fragment of that file, which is
132 * delivered by multiple packages in the consolidation. Thus it
133 * can't exist in the proto area, and because we can't really
134 * know where the file's root directory is, we should skip the
137 * For all other filetypes, assume the right hand side is in the
140 if (e
->file_type
== SYM_LINK_T
|| e
->file_type
== LINK_T
) {
142 e
->symsrc
= strdup(src
);
143 } else if (e
->file_type
== EDIT_T
) {
151 * if a basedir has a value, prepend it to the filename
154 (void) strcat(strcat(strcpy(e
->name
, basedir
), "/"), file
);
156 (void) strcpy(e
->name
, file
);
158 if (e
->file_type
!= SYM_LINK_T
) {
159 if ((e
->file_type
== CHAR_DEV_T
) ||
160 (e
->file_type
== BLOCK_DEV_T
)) {
161 if (!(maj
= strtok(NULL
, FS
))) {
162 (void) fprintf(stderr
,
163 "error: bad line(major number) : %s\n",
167 e
->major
= atoi(maj
);
169 if (!(min
= strtok(NULL
, FS
))) {
170 (void) fprintf(stderr
,
171 "error: bad line(minor number) : %s\n",
175 e
->minor
= atoi(min
);
181 if (!(perm
= strtok(NULL
, FS
))) {
182 (void) fprintf(stderr
,
183 "error: bad line(permission) : %s\n", line
);
186 e
->perm
= strtol(perm
, NULL
, 8);
188 if (!(owner
= strtok(NULL
, FS
))) {
189 (void) fprintf(stderr
,
190 "error: bad line(owner) : %s\n", line
);
193 (void) strcpy(e
->owner
, owner
);
195 if (!(group
= strtok(NULL
, FS
))) {
196 (void) fprintf(stderr
,
197 "error: bad line(group) : %s\n", line
);
200 (void) strcpy(e
->group
, group
);
206 e
->link_parent
= NULL
;
208 if (!(dup
= find_elem(list
, e
, FOLLOW_LINK
))) {
209 e
->pkgs
= add_pkg(NULL
, pkgname
); /* init pkgs list */
213 } else if (dup
->file_type
== DIR_T
) {
214 if (!(dup
->pkgs
= add_pkg(dup
->pkgs
, pkgname
))) {
215 /* add entry to pkgs */
216 (void) fprintf(stderr
,
217 "warning: %s: Duplicate entry for %s\n",
221 if (e
->perm
!= dup
->perm
) {
222 (void) fprintf(stderr
,
223 "warning: %s: permissions %#o of %s do not match "
224 "previous permissions %#o\n",
225 pkgname
, e
->perm
, dup
->name
, dup
->perm
);
227 if (strcmp(e
->owner
, dup
->owner
) != 0) {
228 (void) fprintf(stderr
,
229 "warning: %s: owner \"%s\" of %s does not match "
230 "previous owner \"%s\"\n",
231 pkgname
, e
->owner
, dup
->name
, dup
->owner
);
233 if (strcmp(e
->group
, dup
->group
) != 0) {
234 (void) fprintf(stderr
,
235 "warning: %s: group \"%s\" of %s does not match "
236 "previous group \"%s\"\n",
237 pkgname
, e
->group
, dup
->name
, dup
->group
);
241 * Signal an error only if this is something that's not on the
244 (void) strcpy(e
->name
, file
);
245 if (find_elem(&exception_list
, e
, FOLLOW_LINK
) == NULL
) {
246 (void) fprintf(stderr
,
247 "warning: %s: duplicate entry for %s - ignored\n",
257 parse_proto_link(const char *basedir
, char *line
, elem_list
*list
, short arch
,
260 char *type
, *file
, *src
;
264 static elem
*e
= NULL
;
267 (void) strcpy(p_line
, line
);
269 e
= (elem
*)calloc(1, sizeof (elem
));
272 type
= strtok(p_line
, FS
);
275 e
->file_type
= type
[0];
276 (void) strtok(NULL
, FS
); /* burn class */
278 file
= strtok(NULL
, FS
);
279 if ((src
= index(file
, '=')) != NULL
) {
281 e
->symsrc
= strdup(src
);
283 (void) fprintf(stderr
,
284 "error: %s: hard link does not have destination (%s)\n",
290 * if a basedir has a value, prepend it to the filename
293 (void) strcat(strcat(strcpy(e
->name
, basedir
), "/"), file
);
295 (void) strcpy(e
->name
, file
);
298 * now we need to find the file that we link to - to do this
302 src
= resolve_relative(e
->name
, e
->symsrc
);
303 (void) strcpy(key
.name
, src
);
305 if ((p
= find_elem(list
, &key
, NO_FOLLOW_LINK
)) == NULL
) {
306 (void) fprintf(stderr
,
307 "error: %s: hardlink to non-existent file: %s=%s\n",
308 pkgname
, e
->name
, e
->symsrc
);
311 if ((p
->file_type
== SYM_LINK_T
) || (p
->file_type
== LINK_T
)) {
312 (void) fprintf(stderr
,
313 "error: %s: hardlink must link to a file or directory "
314 "(not other links): %s=%s\n", pkgname
, e
->name
, p
->name
);
318 e
->link_sib
= p
->link_sib
;
323 e
->ref_cnt
= p
->ref_cnt
;
326 (void) strcpy(e
->owner
, p
->owner
);
327 (void) strcpy(e
->group
, p
->owner
);
329 if (!(dup
= find_elem(list
, e
, NO_FOLLOW_LINK
))) {
330 e
->pkgs
= add_pkg(NULL
, pkgname
); /* init pkgs list */
337 * Signal an error only if this is something that's not on the
340 (void) strcpy(e
->name
, file
);
341 if (find_elem(&exception_list
, e
, FOLLOW_LINK
) == NULL
) {
342 (void) fprintf(stderr
,
343 "warning: %s: duplicate entry for %s - ignored\n",
354 * open up the pkginfo file and find the ARCH= and the BASEDIR= macros.
355 * I will set the arch and basedir variables based on these fields.
358 read_pkginfo(const char *protodir
, short *arch
, char *basedir
)
360 char pkginfofile
[MAXPATHLEN
];
361 char architecture
[MAXPATHLEN
];
369 architecture
[0] = '\0';
374 * determine whether the pkginfo file is a pkginfo.tmpl or
377 (void) strcat(strcat(strcpy(pkginfofile
, protodir
), "/"),
380 if ((pkginfo_fp
= fopen(pkginfofile
, "r")) == NULL
) {
381 (void) strcat(strcat(strcpy(pkginfofile
, protodir
), "/"),
383 if ((pkginfo_fp
= fopen(pkginfofile
, "r")) == NULL
) {
390 while (fgets(buf
, BUFSIZ
, pkginfo_fp
) && (hits
!= 3)) {
391 if (strncmp(buf
, "ARCH=", 5) == 0) {
394 * remove any '"' in the ARCH field.
396 for (i
= 5; buf
[i
]; i
++) {
398 architecture
[index
++] = buf
[i
];
400 /* -1 because above copy included '\n' */
401 architecture
[index
-1] = '\0';
403 } else if (strncmp(buf
, "BASEDIR=", 8) == 0) {
406 * remove any '"' in the BASEDIR field, and
407 * strip off a leading '/' if present.
409 for (i
= 8; buf
[i
]; i
++) {
411 (buf
[i
] != '/' || index
!= 0)) {
412 buf
[index
++] = buf
[i
];
415 /* -1 because above copy included '\n' */
417 (void) strcpy(basedir
, &buf
[0]);
421 (void) fclose(pkginfo_fp
);
424 if ((*arch
= assign_arch(architecture
)) == 0) {
425 (void) fprintf(stderr
,
426 "warning: Unknown architecture %s found in %s\n",
427 architecture
, pkginfofile
);
432 * The first pass through the prototype file goes through and reads
433 * in all the entries except 'hard links'. Those must be processed
436 * If any !includes are found in the prototype file this routine
437 * will be called recursively.
440 * protofile - full pathname to prototype file to be processed.
441 * protodir - directory in which prototype file resides.
442 * list - list to which elements will be added
443 * arch - architecture of current prototype
444 * basedir - basedir for package
445 * pkgname - name of package
448 * returns number of items added to list.
452 first_pass_prototype(const char *protofile
, const char *protodir
,
453 elem_list
*list
, short arch
, const char *basedir
, const char *pkgname
)
457 char include_file
[MAXPATHLEN
];
460 if ((proto_fp
= fopen(protofile
, "r")) == NULL
) {
466 * first pass through file - process everything but
469 while (fgets(buf
, BUFSIZ
, proto_fp
)) {
480 if ((rc
= parse_proto_line(basedir
, buf
, list
, arch
,
484 (void) fprintf(stderr
,
485 "error: Errors found in %s\n", protofile
);
494 /* Is this an include statement - if so process */
495 if (strncmp(buf
, "!include", 8) == 0) {
496 char *inc_file
= (char *)(buf
+ 9);
498 /* burn white space */
499 while ((*inc_file
== ' ') ||
503 /* remove trailing \n */
504 inc_file
[strlen(inc_file
) - 1] = '\0';
505 (void) strcat(strcat(strcpy(
506 include_file
, protodir
), "/"),
509 first_pass_prototype(include_file
,
510 protodir
, list
, arch
, basedir
,
513 (void) fprintf(stderr
,
514 "warning: bad !include statement "
515 "in prototype %s : %s\n",
519 (void) fprintf(stderr
,
520 "warning: unexpected ! notation in "
521 "prototype %s : %s\n", protofile
, buf
);
526 (void) fprintf(stderr
,
527 "warning: unexpected line in prototype %s : %s\n",
533 (void) fclose(proto_fp
);
539 * The second pass through the prototype file goes through and reads
540 * and processes only the 'hard links' in the prototype file. These
541 * are resolved and added accordingly to the elements list(list).
543 * If any !includes are found in the prototype file this routine
544 * will be called recursively.
547 * protofile - full pathname to prototype file to be processed.
548 * protodir - directory in which prototype file resides.
549 * list - list to which elements will be added
550 * arch - architecture of current prototype
551 * basedir - basedir for package
552 * pkgname - package name
555 * returns number of items added to list.
559 second_pass_prototype(const char *protofile
, const char *protodir
,
560 elem_list
*list
, short arch
, const char *basedir
, const char *pkgname
)
564 char include_file
[MAXPATHLEN
];
567 if ((proto_fp
= fopen(protofile
, "r")) == NULL
) {
573 * second pass through prototype file - process the hard links
576 while (fgets(buf
, BUFSIZ
, proto_fp
)) {
577 if (buf
[0] == LINK_T
) {
580 if ((rc
= parse_proto_link(basedir
, buf
, list
, arch
,
584 (void) fprintf(stderr
,
585 "error: Errors found in %s\n", protofile
);
587 } else if (strncmp(buf
, "!include", 8) == 0) {
589 * This is a file to include
591 char *inc_file
= (char *)(buf
+ 9);
593 /* burn white space */
594 while ((*inc_file
== ' ') || (*inc_file
== '\t'))
598 /* remove trailing \n */
599 inc_file
[strlen(inc_file
) - 1] = '\0';
600 /* build up include file name to be opened. */
601 (void) strcat(strcat(strcpy(include_file
,
602 protodir
), "/"), inc_file
);
604 * recursively call this routine to process the
608 second_pass_prototype(include_file
,
609 protodir
, list
, arch
, basedir
, pkgname
);
611 (void) fprintf(stderr
,
612 "warning: Bad !include statement in "
613 "prototype %s : %s\n", protofile
, buf
);
617 (void) fclose(proto_fp
);
624 * pkgname - name of package being processed
625 * protodir - pathname to package defs directory
626 * list - List of elements read in, elements are added to this
627 * as they are read in.
628 * verbose - verbose output
631 * number of elements added to list
634 process_package_dir(const char *pkgname
, const char *protodir
,
635 elem_list
*list
, int verbose
)
638 char protofile
[MAXPATHLEN
];
639 char basedir
[MAXPATHLEN
];
645 * skip any packages we've already handled (because of
648 if (processed_package(pkgname
)) {
653 * find the prototype file. Legal forms of the name are:
655 * prototype_<mach> (where mach == (sparc || i386 || ppc)
657 (void) strcat(strcat(strcpy(protofile
, protodir
), "/"), "prototype");
658 if (stat(protofile
, &st_buf
) < 0) {
659 if (errno
== ENOENT
) {
660 (void) strcat(strcat(strcat(strcpy(protofile
,
661 protodir
), "/"), "prototype"), PROTO_EXT
);
662 if (stat(protofile
, &st_buf
) < 0) {
663 if (errno
== ENOENT
) {
665 (void) fprintf(stderr
,
666 "warning: no prototype "
681 mark_processed(pkgname
);
683 read_pkginfo(protodir
, &arch
, basedir
);
685 count
+= first_pass_prototype(protofile
, protodir
, list
, arch
,
687 count
+= second_pass_prototype(protofile
, protodir
, list
, arch
,
690 /* print_list(list); */
695 read_in_protodir(const char *dir_name
, elem_list
*list
, int verbose
)
699 char protodir
[MAXPATHLEN
];
703 if ((p_dir
= opendir(dir_name
)) == NULL
) {
708 list
->type
= PROTODIR_LIST
;
710 while ((dp
= readdir(p_dir
)) != NULL
) {
712 * let's not check "." and ".." - I don't really like this
713 * but I wasn't really sure you could be sure that they
714 * are always the first two entries in the directory
715 * structure - so I put it in the loop.
717 * Also - we skip all directories that are names .del-*.
718 * and any SCCS directories too.
720 if ((strcmp(dp
->d_name
, ".") == 0) ||
721 (strcmp(dp
->d_name
, "..") == 0) ||
722 (strncmp(dp
->d_name
, ".del-", 5) == 0) ||
723 (strcmp(dp
->d_name
, "SCCS") == 0))
726 (void) strcat(strcat(strcpy(protodir
, dir_name
), "/"),
728 if (stat(protodir
, &st_buf
) < 0) {
732 if (!S_ISDIR(st_buf
.st_mode
)) {
734 (void) fprintf(stderr
,
735 "warning: %s not a directory\n", protodir
);
740 count
+= process_dependencies(dp
->d_name
, dir_name
, list
,
743 count
+= process_package_dir(dp
->d_name
, protodir
, list
,
748 (void) printf("read in %d lines\n", count
);
750 (void) closedir(p_dir
);