2 * FreeBSD install - a package for the installation and maintenance
3 * of non-core utilities.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
17 * Miscellaneous file access utilities.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
30 /* Quick check to see if a file exists */
32 fexists(const char *fname)
36 if ((fd = open(fname, O_RDONLY)) == -1)
43 /* Quick check to see if something is a directory or symlink to a directory */
45 isdir(const char *fname)
49 if (lstat(fname, &sb) != FAIL && S_ISDIR(sb.st_mode))
51 else if (lstat(strconcat(fname, "/."), &sb) != FAIL && S_ISDIR(sb.st_mode))
57 /* Check to see if file is a dir or symlink to a dir, and is empty */
59 isemptydir(const char *fname)
65 dirp = opendir(fname);
67 return FALSE; /* no perms, leave it alone */
68 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
69 if (strcmp(dp->d_name, ".") && strcmp(dp->d_name, "..")) {
81 * Returns TRUE if file is a regular file or symlink pointing to a regular
85 isfile(const char *fname)
88 if (stat(fname, &sb) != FAIL && S_ISREG(sb.st_mode))
94 * Check to see if file is a file or symlink pointing to a file and is empty.
95 * If nonexistent or not a file, say "it's empty", otherwise return TRUE if
99 isemptyfile(const char *fname)
102 if (stat(fname, &sb) != FAIL && S_ISREG(sb.st_mode)) {
109 /* Returns TRUE if file is a symbolic link. */
111 issymlink(const char *fname)
114 if (lstat(fname, &sb) != FAIL && S_ISLNK(sb.st_mode))
119 /* Returns TRUE if file is a URL specification */
121 isURL(const char *fname)
124 * I'm sure there are other types of URL specifications that I could
125 * also be looking for here, but for now I'll just be happy to get ftp
130 while (isspace(*fname))
132 if (!strncmp(fname, "ftp://", 6) || !strncmp(fname, "http://", 7) ||
133 !strncmp(fname, "https://", 8) || !strncmp(fname, "file://", 7))
139 fileFindByPath(const char *base, const char *fname)
141 static char tmp[FILENAME_MAX];
143 const char *suffixes[] = {".tbz", ".tgz", ".tar", ".txz", NULL};
146 if (fexists(fname) && isfile(fname)) {
153 cp = strrchr(tmp, '/');
155 *cp = '\0'; /* chop name */
156 cp = strrchr(tmp, '/');
159 for (i = 0; suffixes[i] != NULL; i++) {
163 strcat(cp, suffixes[i]);
169 cp = getenv("PKG_PATH");
171 char *cp2 = strsep(&cp, ":");
173 for (i = 0; suffixes[i] != NULL; i++) {
174 snprintf(tmp, FILENAME_MAX, "%s/%s%s", cp2 ? cp2 : cp, fname, suffixes[i]);
175 if (fexists(tmp) && isfile(tmp))
183 fileGetContents(const char *fname)
189 if (stat(fname, &sb) == FAIL) {
191 errx(2, "%s: can't stat '%s'", __func__, fname);
194 contents = (char *)malloc(sb.st_size + 1);
195 fd = open(fname, O_RDONLY, 0);
198 errx(2, "%s: unable to open '%s' for reading", __func__, fname);
200 if (read(fd, contents, sb.st_size) != sb.st_size) {
202 errx(2, "%s: short read on '%s' - did not get %lld bytes", __func__,
203 fname, (long long)sb.st_size);
206 contents[sb.st_size] = '\0';
211 * Takes a filename and package name, returning (in "try") the
212 * canonical "preserve" name for it.
215 make_preserve_name(char *try, int max, const char *name, const char *file)
219 if ((len = strlen(file)) == 0)
223 strncpy(try, file, max);
224 if (try[i] == '/') /* Catch trailing slash early and save checking in the loop */
229 strncpy(&try[i + 2], &file[i + 1], max - i - 2);
235 strncpy(try + 1, file, max - 1);
237 /* I should probably be called rude names for these inline assignments */
238 strncat(try, ".", max -= strlen(try));
239 strncat(try, name, max -= strlen(name));
240 strncat(try, ".", max--);
241 strncat(try, "backup", max -= 6);
245 /* Write the contents of "str" to a file */
247 write_file(const char *name, const char *str)
252 fp = fopen(name, "w");
255 errx(2, "%s: cannot fopen '%s' for writing", __func__, name);
258 if (fwrite(str, 1, len, fp) != len) {
260 errx(2, "%s: short fwrite on '%s', tried to write %ld bytes",
261 __func__, name, (long)len);
265 errx(2, "%s: failure to fclose '%s'", __func__, name);
270 copy_file(const char *dir, const char *fname, const char *to)
272 char cmd[FILENAME_MAX];
275 snprintf(cmd, FILENAME_MAX, "/bin/cp -r %s %s", fname, to);
277 snprintf(cmd, FILENAME_MAX, "/bin/cp -r %s/%s %s", dir, fname, to);
280 errx(2, "%s: could not perform '%s'", __func__, cmd);
285 move_file(const char *dir, const char *fname, const char *tdir)
287 char from[FILENAME_MAX];
288 char to[FILENAME_MAX];
291 strncpy(from, fname, FILENAME_MAX);
293 snprintf(from, FILENAME_MAX, "%s/%s", dir, fname);
295 snprintf(to, FILENAME_MAX, "%s/%s", tdir, fname);
297 if (rename(from, to) == -1) {
298 if (vsystem("/bin/mv %s %s", from, to)) {
300 errx(2, "%s: could not move '%s' to '%s'", __func__, from, to);
306 * Copy a hierarchy (possibly from dir) to the current directory, or
307 * if "to" is TRUE, from the current directory to a location someplace
310 * Though slower, using tar to copy preserves symlinks and everything
311 * without me having to write some big hairy routine to do it.
314 copy_hierarchy(const char *dir, const char *fname, Boolean to)
316 char cmd[FILENAME_MAX * 3];
319 /* If absolute path, use it */
322 snprintf(cmd, FILENAME_MAX * 3, "/usr/bin/tar cf - -C %s %s | /usr/bin/tar xpf -",
326 snprintf(cmd, FILENAME_MAX * 3, "/usr/bin/tar cf - %s | /usr/bin/tar xpf - -C %s",
329 printf("Using '%s' to copy trees.\n", cmd);
333 errx(2, "%s: could not perform '%s'", __func__, cmd);
337 /* Unpack a tar file */
339 unpack(const char *pkg, const char *flist)
341 const char *comp, *cp;
346 * Figure out by a crude heuristic whether this or not this is probably
347 * compressed and whichever compression utility was used (gzip or bzip2).
349 if (strcmp(pkg, "-")) {
350 cp = strrchr(pkg, '.');
352 strcpy(suff, cp + 1);
353 if (strchr(suff, 'z') || strchr(suff, 'Z')) {
354 if (strchr(suff, 'b'))
363 if (vsystem("/usr/bin/tar -xp %s -f '%s' %s", comp, pkg, flist ? flist : "")) {
364 warnx("tar extract of %s failed!", pkg);
371 * Using fmt, replace all instances of:
373 * %F With the parameter "name"
374 * %D With the parameter "dir"
375 * %B Return the directory part ("base") of %D/%F
376 * %f Return the filename part of %D/%F
378 * Does not check for overflow - caution!
382 format_cmd(char *buf, int max, const char *fmt, const char *dir, const char *name)
384 char *cp, scratch[FILENAME_MAX * 2];
387 while (*fmt && max > 0) {
391 strncpy(buf, name, max);
397 strncpy(buf, dir, max);
403 snprintf(scratch, FILENAME_MAX * 2, "%s/%s", dir, name);
404 cp = &scratch[strlen(scratch) - 1];
405 while (cp != scratch && *cp != '/')
408 strncpy(buf, scratch, max);
414 snprintf(scratch, FILENAME_MAX * 2, "%s/%s", dir, name);
415 cp = &scratch[strlen(scratch) - 1];
416 while (cp != scratch && *(cp - 1) != '/')
418 strncpy(buf, cp, max);