It is discouraged but possible to run pkg_add(1) with -n or -s as a user 
other than root.  However, if pkg_add(1) does not have write permission to 
$PKG_CACHE, then unclear error messages are produced.  For example:

$ ls -ld $PKG_CACHE
drwxr-xr-x  2 root  wheel  3072 Jul  2 12:13 /var/pkg_cache
$ pkg_add -vn gcal
pkg_add should be run as root
Update candidates: quirks-2.54 -> quirks-2.54
quirks-2.54 signed on 2015-03-08T12:33:05Z
Fatal error: Ustar 
[ftp://ftp.openbsd.org/pub/OpenBSD/5.7/packages/amd64/gcal-3.6.3p0.tgz][?]: 
Error while reading header
 at /usr/libdata/perl5/OpenBSD/Ustar.pm line 89.

One solution is for pkg_add(1) to silently omit the attempt to copy the 
package to an unwritable $PKG_CACHE.  Below is a diff that achieves this, 
and modifies pkg_add(1)'s man page accordingly.

Index: pkg_add.1
===================================================================
RCS file: /cvs/src/usr.sbin/pkg_add/pkg_add.1,v
retrieving revision 1.132
diff -u -p -r1.132 pkg_add.1
--- pkg_add.1   16 Apr 2015 20:01:39 -0000      1.132
+++ pkg_add.1   7 Jul 2015 00:51:22 -0000
@@ -371,7 +371,7 @@ Don't actually install a package, just r
 would be taken if it was.
 Will still copy packages to
 .Ev PKG_CACHE
-if applicable.
+if applicable, and if pkg_add has write permission to that directory.
 .It Fl P Ar type
 Check permissions for distribution, where
 .Ar type
Index: OpenBSD/PackageRepository.pm
===================================================================
RCS file: /cvs/src/usr.sbin/pkg_add/OpenBSD/PackageRepository.pm,v
retrieving revision 1.113
diff -u -p -r1.113 PackageRepository.pm
--- OpenBSD/PackageRepository.pm        4 Mar 2015 13:55:32 -0000       1.113
+++ OpenBSD/PackageRepository.pm        7 Jul 2015 00:51:22 -0000
@@ -534,7 +534,7 @@ sub open_pipe
                $object->{pid2} = $pid2;
        } else {
                open STDERR, '>', $object->{errors};
-               if (defined $object->{cache_dir}) {
+               if (defined $object->{cache_dir} and -w $object->{cache_dir}) {
                        my $pid3 = open(my $in, "-|");
                        $self->did_it_fork($pid3);
                        if ($pid3) {

Reply via email to