]> pd.if.org Git - zpackage/blob - zpm-add
ensure all paths are absolute in zpm-add
[zpackage] / zpm-add
1 #!/bin/sh
2
3 # zpm add -f pkgfile pkgid [ files ]
4
5 # no package file? take from ZPM_PACKAGE_FILE
6 # no pkgid ? take from ZPM_PACKAGE_ID
7 # no id impossible
8
9 die() {
10         echo $* 1>&2
11         exit 1
12 }
13
14 # basic cleanup on a path
15 cleanpath() {
16         clean="$1"
17         if [ -z "$clean" ]; then printf ''; fi
18
19         # multiple slashes
20         clean=$(printf "%s" "$clean" | sed -e 's|/+|/|g')
21         # curdir
22         clean=$(printf "%s" "$clean" | sed -e 's|/\./|/|g')
23         # leading curdir
24         clean=${clean#./}
25         # trailing curdir
26         clean=${clean%/.}
27         # trailing slash
28         clean=${clean%/}
29         printf "%s" "$clean"
30 }
31
32 verbose=0
33 tags=
34 isconfig=0
35 addcontent=1
36 complete=0
37 # option for "multipackage" just to let the system know that's what you meant
38 # option to take filenames from stdin
39 # parse package, version, release from file if not given
40 # TODO -l follow symlinks, -L follow symlinks, adding all, links and targets
41 while getopts :f:vr:l:P:S:cu:g:NC opt; do
42         case $opt in
43                 N) addcontent=0 ;;
44                 f) pkgfile="$OPTARG" ;;
45                 P) prefix="$OPTARG" ;;
46                 S) strip=$(cleanpath "$OPTARG"); ;;
47                 t) tags="$tags $OPTARG" ;;
48                 c) isconfig=1 ;;
49                 u) username="$OPTARG" ;;
50                 g) groupname="$OPTARG" ;;
51                 v) verbose=$((verbose + 1)) ;;
52                 C) complete=1 ;;
53                 *) echo 'unknown option' $OPTARG; exit 1 ;;
54         esac
55 done
56 shift $((OPTIND - 1))
57
58 if [ $isconfig -eq 1 ]; then
59         tags="$tags configuration"
60 fi
61
62 if [ $verbose -gt 2 ]; then
63         set -x
64 fi
65
66 pkgid="$1"
67 shift
68 eval $(zpm parse -E $pkgid)
69
70 if [ -z "$pkgfile" ]; then
71         pkgfile=$ZPM_PACKAGE_FILE
72 fi
73
74 if [ -z "$release" ]; then
75         if [ -z "$pkgfile" ]; then
76                 die "cannot determine package file"
77         else
78                 pkgstr=$(zpm findpkg -f $pkgfile $pkgid)
79                 if [ -z "$pkgstr" ]; then
80                         die "unable to find package id for $pkgid in $pkgfile"
81                 fi
82                 pkgid=$pkgstr
83                 # need to reparse the new package id
84                 eval $(zpm parse -E $pkgid)
85         fi
86 fi
87
88 # look for a .zpm file here
89 if [ -z "$pkgfile" ] && [ -f "$pkgid.zpm" ]; then
90         pkgfile="$pkgid.zpm"
91 fi
92
93 if [ -z "$pkgfile" ]; then
94         die "cannot determine package file"
95 fi
96
97 # check for package file
98 if [ ! -f "$pkgfile" ]; then
99         echo $pkgfile does not exist
100         exit 1
101 fi
102
103 set -e
104 zpm test -v $pkgfile
105 set +e
106
107 if [ $verbose -gt 0 ]; then
108         echo adding to $pkgfile $pkgid
109 fi
110
111 package=$(zpm quote "$name")
112 pkgver=$(zpm quote "$version")
113 pkgrel=$(zpm quote "$release")
114
115 #strip=$(cleanpath "$strip")
116 for path in $*; do
117         #echo adding $path
118         mtime=$(zpm stat -f '%y' $path)
119         uid=$(zpm stat -f '%u' $path)
120         gid=$(zpm stat -f '%g' $path)
121         mode=$(zpm stat -f '%a' $path)
122
123         # only stat the file for the user and group name if not set on the
124         # command line
125         : ${username:=$(zpm stat -f '%U' $path)}
126         : ${groupname:=$(zpm stat -f '%G' $path)}
127
128         rpath="$path"
129
130         rpath=$(cleanpath "$path")
131
132         # strip off leading slash
133         rpath=${rpath#/}
134
135         if [ -z "$rpath" ] || [ "$rpath" = '.' ]; then
136                 continue
137         fi
138
139         if [ ! -z "$strip" ]; then
140                 echo "stripping $strip"
141                 rpath=${rpath#$strip}
142                 rpath=${rpath#/}
143         fi
144
145         if [ -z "$rpath" ]; then
146                 die "$path resolves to nothing"
147         fi
148
149         prefix=$(cleanpath "$prefix")
150         if [ ! -z "$prefix" ]; then
151                 rpath="$prefix/$rpath"
152         fi
153
154         # ensure all paths are absolute
155         rpath=/${rpath#/}
156
157         filetype=$(zpm stat -l -f '%t' "$path")
158         hash='NULL'
159         target='NULL'
160         case "$filetype" in
161                 regular)
162                         filetype=r
163                         if [ $addcontent -eq 1 ]; then
164                                 hash=$(zpm addfile $pkgfile "$path")
165                                 if [ $? -ne 0 ]; then
166                                         die "zpm addfile failed ($?): $pkgfile $path" 
167                                 fi
168                         else
169                                 hash=$(zpm hash "$path")
170                         fi
171                         hash="'$hash'"
172                         ;;
173                 directory)
174                         filetype=d
175                         ;;
176                 symlink)
177                         filetype=l
178                         target=$(readlink $path)
179                         target="'$target'"
180                         ;;
181         esac
182
183         # TODO check that we have such a package,version,release
184         #cat <<EOS
185         zpm shell $pkgfile <<EOS
186 begin;
187 insert or replace into packagefiles (package,version,release,path,mode,mtime,username,groupname,filetype,hash,configuration,target)
188 values ('$package', '$pkgver', $pkgrel, '$rpath', '$mode',$mtime, '$username','$groupname','$filetype',$hash,$isconfig,$target);
189 commit;
190 EOS
191
192 #printf "%s %s%s\n" $path $rpath ${target:+" -> $target"}
193 if [ $verbose -gt 1 ]; then
194         printf "%s%s %s:%s %s\n" $filetype $mode $username $groupname $path
195 elif [ $verbose -gt 0 ]; then
196         printf "%s\n" $path
197 fi
198
199 done
200
201 if [ $complete -eq 1 ]; then
202         zpm pkg -f $pkgfile $pkgid build_time=$(date +'%s')
203         zpm packagehash -f $pkgfile -s -q $pkgid
204 else
205         zpm pkg -f $pkgfile $pkgid hash=
206 fi