]> pd.if.org Git - zpackage/blob - zpm-add
quote paths 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 255
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 noclear=0
38 # option for "multipackage" just to let the system know that's what you meant
39 # option to take filenames from stdin
40 # parse package, version, release from file if not given
41 # TODO -l follow symlinks, -L follow symlinks, adding all, links and targets
42 # -T set link target
43 # -M set mtime
44 # -m set mode
45 # -H set hash, will be overridden for regular files unless -N is set
46 # -F set filetype, regular, symlink, directory
47 while getopts :f:vr:l:P:S:cu:g:NCm:M:T:H:F:z opt; do
48         case $opt in
49                 N) addcontent=0 ;;
50                 f) pkgfile="$OPTARG" ;;
51                 P) prefix="$OPTARG" ;;
52                 S) strip=$(cleanpath "$OPTARG"); ;;
53                 t) tags="$tags $OPTARG" ;;
54                 c) isconfig=1 ;;
55                 u) cluser="$OPTARG" ;;
56                 g) clgroup="$OPTARG" ;;
57                 m) clmode="$OPTARG" ;;
58                 M) clmtime="$OPTARG" ;;
59                 T) cltarget="$OPTARG" ;;
60                 F) cltype="$OPTARG" ;;
61                 H) clhash="$OPTARG" ;;
62                 v) verbose=$((verbose + 1)) ;;
63                 C) complete=1 ;;
64                 z) noclear=1 ;;
65                 *) echo 'unknown option' $OPTARG; exit 255 ;;
66         esac
67 done
68 shift $((OPTIND - 1))
69
70 if [ $isconfig -eq 1 ]; then
71         tags="$tags configuration"
72 fi
73
74 if [ $verbose -gt 2 ]; then
75         set -x
76 fi
77
78 pkgid="$1"
79 shift
80 eval $(zpm parse -E $pkgid)
81
82 if [ -z "$pkgfile" ]; then
83         pkgfile=$ZPM_PACKAGE_FILE
84 fi
85
86 if [ -z "$release" ]; then
87         if [ -z "$pkgfile" ]; then
88                 die "cannot determine package file"
89         else
90                 pkgstr=$(zpm findpkg -f $pkgfile $pkgid)
91                 if [ -z "$pkgstr" ]; then
92                         die "unable to find package id for $pkgid in $pkgfile"
93                 fi
94                 pkgid=$pkgstr
95                 # need to reparse the new package id
96                 eval $(zpm parse -E $pkgid)
97         fi
98 fi
99
100 # look for a .zpm file here
101 if [ -z "$pkgfile" ] && [ -f "$pkgid.zpm" ]; then
102         pkgfile="$pkgid.zpm"
103 fi
104
105 if [ -z "$pkgfile" ]; then
106         die "cannot determine package file"
107 fi
108
109 # check for package file
110 if [ ! -f "$pkgfile" ]; then
111         die $pkgfile does not exist
112 fi
113
114 set -e
115 zpm test -v $pkgfile
116 set +e
117
118 if [ $verbose -gt 0 ]; then
119         echo adding to $pkgfile $pkgid
120 fi
121
122 package=$(zpm quote "$name")
123 pkgver=$(zpm quote "$version")
124 pkgrel=$(zpm quote "$release")
125
126 #strip=$(cleanpath "$strip")
127 for path in "$@"; do
128         #echo adding $path
129 if [ $verbose -gt 1 ]; then
130         printf "adding %s\n" "$path"
131 fi
132
133         # only stat the file for items not set on the command line
134         mode=${clmode:-$(zpm stat -l -f '%a' "$path")}
135         username=${cluser:-$(zpm stat -l -f '%U' "$path")}
136         groupname=${clgroup:-$(zpm stat -l -f '%G' "$path")}
137         mtime=${clmtime:-$(zpm stat -l -f '%y' "$path")}
138         filetype=${cltype:-$(zpm stat -l -f '%t' "$path")}
139
140         rpath="$path"
141
142         rpath=$(cleanpath "$path")
143
144         # strip off leading slash
145         rpath=${rpath#/}
146
147         if [ -z "$rpath" ] || [ "$rpath" = '.' ]; then
148                 continue
149         fi
150
151         if [ ! -z "$strip" ]; then
152                 rpath=${rpath#$strip}
153                 rpath=${rpath#/}
154         fi
155
156         if [ -z "$rpath" ]; then
157                 die "$path resolves to nothing"
158         fi
159
160         prefix=$(cleanpath "$prefix")
161         if [ ! -z "$prefix" ]; then
162                 rpath="$prefix/$rpath"
163         fi
164
165         # ensure all paths are absolute
166         rpath=/${rpath#/}
167
168         hash='NULL'
169         target='NULL'
170         case "$filetype" in
171                 regular)
172                         filetype=r
173                         if [ $addcontent -eq 1 ]; then
174                                 hash=$(zpm addfile $pkgfile "$path")
175                                 if [ $? -ne 0 ]; then
176                                         die "zpm addfile failed ($?): $pkgfile $path" 
177                                 fi
178                         else
179                                 if [ -z "$clhash" ]; then
180                                         hash=$(zpm hash "$path")
181                                 else
182                                         hash=$clhash
183                                 fi
184                         fi
185                         hash="'$hash'"
186                         ;;
187                 directory)
188                         filetype=d
189                         ;;
190                 symlink)
191                         filetype=l
192                         if [ -n "$cltarget" ]; then
193                                 target=$cltarget
194                         else 
195                                 target=$(readlink $path)
196                                 target=$(zpm quote -q "$target")
197                         fi
198                         ;;
199                 *)
200                         die "filetype $filetype not supported"
201                         ;;
202         esac
203
204         # TODO check that we have such a package,version,release
205         #cat <<EOS
206         if [ $verbose -gt 2 ]; then
207                 shellecho=".echo on"
208         fi
209         zpm shell $pkgfile <<EOS
210 begin;
211 $shellecho
212 insert or replace into packagefiles (package,version,release,path,mode,mtime,username,groupname,filetype,hash,configuration,target)
213 values ('$package', '$pkgver', $pkgrel, '$rpath', '$mode',$mtime, '$username','$groupname','$filetype',$hash,$isconfig,$target);
214 commit;
215 EOS
216
217 if [ $? -ne 0 ]; then
218         die zpm-add shell failed
219 fi
220
221 #printf "%s %s%s\n" $path $rpath ${target:+" -> $target"}
222 if [ $verbose -gt 1 ]; then
223         printf "%s%s %s:%s %s\n" $filetype $mode $username $groupname "$path"
224 elif [ $verbose -gt 0 ]; then
225         printf "%s\n" "$path"
226 fi
227
228 done
229
230 if [ $complete -eq 1 ]; then
231         zpm pkg -f $pkgfile $pkgid build_time=$(date +'%s')
232         zpm packagehash -f $pkgfile -s -q $pkgid
233 elif [ $noclear -eq 0 ]; then
234         zpm pkg -f $pkgfile $pkgid hash=
235         rv=$?
236         if [ $rv -ne 0 ]; then
237                 die clearing packagehash failed
238         fi
239 fi
240 exit 0