1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
|
# Copyright (c) 2017 The NetBSD Foundation, Inc.
# All rights reserved.
#
# This code is derived from software contributed to The NetBSD Foundation
# by Johnny C. Lam.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# NAME
# refcount_file.subr -- directory-based reference-count implementation
#
# SYNOPSIS
# task_refcount_file exists database resource
# task_refcount_file add [-t token] database resource
# task_refcount_file remove [-t token] database resource
# task_refcount_file delete database resource
#
# task_refcount_file prop_exists database resource property
# task_refcount_file prop_put database resource property [value ...]
# task_refcount_file prop_match database resource property [value ...]
# task_refcount_file prop_delete database resource property
#
# DESCRIPTION
# The functions implement the refcount API functions with the
# corresponding names.
#
# IMPLEMENTATION
# This is a file-based implementation of a reference-counting API
# for packages. The resource being reference-counted is assigned a
# directory pathname that "shadows" the resource. It is populated
# with the following files:
#
# ${PKGNAME}
# This text file represents a reference on the resource by
# the package whose full package name is ${PKGNAME}. Each
# line of the file lists the location of the metadata files
# for the package, known as the "package metadata directory"
# (default "${PKG_DBDIR}/${PKGNAME}").
#
# +OWNER
# This text file consists of one line that lists the package
# metadata directory of the package that owns the resource.
#
# +PREEXISTING
# This text file consists of one line that lists the package
# metadata directory of the package that discovered the
# resource was pre-existing.
#
# +PERMISSIONS
# This text file consists of one line that lists the mode
# and permissions of a file resource. The line is of the
# form: "mode owner group", where "mode" is the file mode in
# octal form, "owner" is the user ID of the file, and
# "group" is the group ID of the file.
#
# There are four directories located under ${PKG_REFCOUNT_DBDIR}
# that are databases for resources that are reference-counted by the
# packages:
#
# dirs/
# files/ The relative paths under these directories shadow the
# resource at the same path relative to the root directory.
#
# groups/
# The entries of this directory are the names of groups.
#
# users/ The entries of this directory are the names of users.
#
# ENVIRONMENT
# The following variables are used if they are set:
#
# MV The name or path to the mv(1) utility.
#
# PKGNAME
# The name of the package manipulating the reference counts.
#
# PKG_DBDIR
# The location of the package metadata directory database.
# The default is "/var/db/pkg".
#
# PKG_DESTDIR
# A "destdir" prefix that is prepended to all filesystem
# paths. The default value is the empty string.
#
# PKG_METADATA_DIR
# The absolute path to the location of the meta-data files
# of ${PKGNAME}. The default is "${PKG_DBDIR}/${PKGNAME}".
#
# PKG_REFCOUNT_DBDIR
# The location of the directory tree that shadows the
# resources that are reference-counted. The default is
# "${PKG_DBDIR}.refcount".
#
# RM The name or path to the rm(1) utility.
#
# RMDIR The name or path to the rmdir(1) utility.
#
__task_refcount_file__="yes"
__task_refcount_file_init__="_task_refcount_file_init"
task_load cleanup
task_load makedir
task_load maketemp
task_load match
task_load quote
task_refcount_file()
{
[ $# -gt 0 ] || return 127
local command="$1"; shift
local fn
case $command in
exists|add|remove|delete|prop_exists|prop_put|prop_match|prop_delete)
eval fn="_task_refcount_file_$command" ;;
*) return 127 ;;
esac
$fn "$@"
}
_task_refcount_file_pkg_token()
{
: ${PKGNAME:=${0##*/}}
: ${PKG_DBDIR:=/var/db/pkg}
: ${PKG_METADATA_DIR:=${PKG_DBDIR}/${PKGNAME}}
pkg_token=${PKG_METADATA_DIR}
# POST-CONDITION:
# pkg_token is set to the package token unique to the system.
}
_task_refcount_file_shadowdir()
{
: ${PKG_DBDIR:=/var/db/pkg}
: ${PKG_REFCOUNT_DBDIR:=${PKG_DBDIR}.refcount}
local db="$1"; shift
local resource="$1"; shift
case $resource in
/*) shadowdir="${PKG_REFCOUNT_DBDIR}/$db$resource" ;;
*) shadowdir="${PKG_REFCOUNT_DBDIR}/$db/$resource" ;;
esac
shadowdir="${PKG_DESTDIR}/$shadowdir"
# POST-CONDITION:
# shadowdir is set to the location of the shadow directory.
}
_task_refcount_file_exists()
{
local db="$1"; shift
local resource="$1"; shift
local shadowdir
_task_refcount_file_shadowdir "$db" "$resource"
# shadowdir is now set to the location of the shadow directory.
set -- "$shadowdir"/*
local entry
for entry; do
case $entry in
"$shadowdir/*")
: "no references found" ;;
"$shadowdir"/+*)
: "property file" ;;
*) # package reference found
return 0 ;;
esac
done
return 1
}
_task_refcount_file_delete()
{
: ${RM:=rm}
: ${RMDIR:=rmdir}
local db="$1"; shift
local resource="$1"; shift
local shadowdir
_task_refcount_file_shadowdir "$db" "$resource"
# shadowdir is now set to the location of the shadow directory.
# Remove all references and properties.
${RM} -f "$shadowdir"/*
# Remove the shadow directory.
${RMDIR} -p "$shadowdir" 2>/dev/null
[ ! -d "$shadowdir" ] || return 1
}
_task_refcount_file_add()
{
: ${CHMOD:=chmod}
: ${CP:=cp}
: ${MV:=mv}
local token=
local arg
local OPTIND=1
while getopts ":t:" arg "$@"; do
case $arg in
t) token=${OPTARG} ;;
*) return 127 ;;
esac
done
shift $(( ${OPTIND} - 1 ))
local db="$1"; shift
local resource="$1"; shift
if [ -z "$token" ]; then
local pkg_token
_task_refcount_file_pkg_token
# pkg_token is now set to the package token unique to the system.
token=$pkg_token
fi
local shadowdir
_task_refcount_file_shadowdir "$db" "$resource"
# shadowdir is now set to the location of the shadow directory.
local countfile="$shadowdir/${PKGNAME}"
task_makedir "$shadowdir" 2>/dev/null
[ -d "$shadowdir" ] || return 1
local result=0
local temp temp_quoted quoted
temp=$( task_maketemp "$countfile.pkgtasks.XXXXXXXXXX" )
if [ -n "$temp" ]; then
task_quote "$temp"
temp_quoted=$quoted
__task_refcount_file_temps__="$__task_refcount_file_temps__ $temp_quoted"
if [ -f "$countfile" ]; then
# Preserve existing ownership and permissions onto tempfile.
${CP} -fp "$countfile" "$temp"
# Overwrite contents of tempfile.
{ task_match -sv "$token" < $countfile; echo "$token"; } > $temp
else
# Overwrite contents of tempfile.
echo "$token" > $temp
# Relax permissions of new file.
${CHMOD} go+r "$temp"
fi
# rename(2) is atomic
if ${MV} -f "$temp" "$countfile"; then
__task_refcount_file_temps__=${__task_refcount_file_temps__% $temp_quoted}
else
result=1
fi
else
# cannot create temporary file
result=1
fi
_task_refcount_file_cleanup
return $result
}
_task_refcount_file_remove()
{
: ${CP:=cp}
: ${MV:=mv}
: ${RM:=rm}
: ${RMDIR:=rmdir}
local token=
local arg
local OPTIND=1
while getopts ":t:" arg "$@"; do
case $arg in
t) token=${OPTARG} ;;
*) return 127 ;;
esac
done
shift $(( ${OPTIND} - 1 ))
local db="$1"; shift
local resource="$1"; shift
if [ -z "$token" ]; then
local pkg_token
_task_refcount_file_pkg_token
# pkg_token is now set to the package token unique to the system.
token=$pkg_token
fi
local shadowdir
_task_refcount_file_shadowdir "$db" "$resource"
# shadowdir is now set to the location of the shadow directory.
local countfile="$shadowdir/${PKGNAME}"
local result=0
if [ -f "$countfile" ]; then
local temp temp_quoted quoted
temp=$( task_maketemp "$countfile.pkgtasks.XXXXXXXXXX" )
if [ -n "$temp" ]; then
task_quote "$temp"
temp_quoted=$quoted
__task_refcount_file_temps__="$__task_refcount_file_temps__ $temp_quoted"
# Preserve existing ownership and permissions onto tempfile.
${CP} -fp "$countfile" "$temp"
# Overwrite contents of tempfile.
task_match -sv "$token" < $countfile > $temp
local remaining=
if [ -f "$temp" ]; then
local line
while read line; do
# reference file is non-empty
remaining="yes"
break
done < $temp
# rename(2) is atomic
if ${MV} -f "$temp" "$countfile"; then
__task_refcount_file_temps__=${__task_refcount_file_temps__% $temp_quoted}
else
result=1
fi
else
# cannot create temporary file
result=1
fi
if [ -z "$remaining" ]; then
${RM} -f "$countfile" || result=1
fi
fi
fi
# Remove shadow directory if empty.
${RMDIR} -p "$shadowdir" 2>/dev/null
_task_refcount_file_cleanup
return $result
}
_task_refcount_file_propfile()
{
local db="$1"; shift
local resource="$1"; shift
local property="$1"; shift
local shadowdir
_task_refcount_file_shadowdir "$db" "$resource"
# shadowdir is now set to the location of the shadow directory.
propfile=
case $property in
owner) propfile="$shadowdir/+OWNER" ;;
preexist)
propfile="$shadowdir/+PREEXISTING" ;;
permissions)
propfile="$shadowdir/+PERMISSIONS" ;;
+*) propfile="$shadowdir/$property" ;;
*) propfile="$shadowdir/+$property" ;;
esac
# POST-CONDITION:
# propfile is set to the location of the property file.
}
_task_refcount_file_prop_exists()
{
local db="$1"; shift
local resource="$1"; shift
local property="$1"; shift
local propfile
_task_refcount_file_propfile "$db" "$resource" "$property"
# propfile is now set to the location of the property file.
[ -f "$propfile" ] || return 1
return 0
}
_task_refcount_file_prop_put()
{
: ${CHMOD:=chmod}
: ${CP:=cp}
: ${MV:=mv}
local db="$1"; shift
local resource="$1"; shift
local property="$1"; shift
local propfile
_task_refcount_file_propfile "$db" "$resource" "$property"
# propfile is now set to the location of the property file.
local pkg_token
_task_refcount_file_pkg_token
# pkg_token is now set to the package token unique to the system.
local shadowdir="${propfile%/*}"
task_makedir "$shadowdir" 2>/dev/null
[ -d "$shadowdir" ] || return 1
local result=0
local temp temp_quoted quoted
temp=$( task_maketemp "$propfile.pkgtasks.XXXXXXXXXX" )
if [ -n "$temp" ]; then
task_quote "$temp"
temp_quoted=$quoted
__task_refcount_file_temps__="$__task_refcount_file_temps__ $temp_quoted"
if [ -f "$propfile" ]; then
# Preserve existing ownership and permissions onto tempfile.
${CP} -fp "$propfile" "$temp"
else
# Relax permissions of new file.
${CHMOD} go+r "$temp"
fi
# Overwrite contents of tempfile.
local value
if [ $# -gt 0 ]; then
value="$@"
else
value=$pkg_token
fi
echo "$value" > $temp
# rename(2) is atomic
if ${MV} -f "$temp" "$propfile"; then
__task_refcount_file_temps__=${__task_refcount_file_temps__% $temp_quoted}
else
result=1
fi
else
# cannot create temporary file
result=1
fi
_task_refcount_file_cleanup
return $result
}
_task_refcount_file_prop_match()
{
local db="$1"; shift
local resource="$1"; shift
local property="$1"; shift
local propfile
_task_refcount_file_propfile "$db" "$resource" "$property"
# propfile is now set to the location of the property file.
local pkg_token
_task_refcount_file_pkg_token
# pkg_token is now set to the package token unique to the system.
if [ -f "$propfile" ]; then
local pattern
if [ $# -gt 0 ]; then
pattern="$@"
else
pattern=$pkg_token
fi
if task_match -q "$pattern" < $propfile; then
return 0
fi
fi
return 1
}
_task_refcount_file_prop_delete()
{
: ${RM:=rm}
: ${RMDIR:=rmdir}
local db="$1"; shift
local resource="$1"; shift
local property="$1"; shift
local propfile
_task_refcount_file_propfile "$db" "$resource" "$property"
# propfile is now set to the location of the property file.
local result=0
${RM} -f "$propfile"
[ ! -f "$propfile" ] || result=1
# Try to clean up the shadow directory in case it is empty.
local shadowdir="${propfile%/*}"
${RMDIR} -p "$shadowdir" 2>/dev/null
return $result
}
_task_refcount_file_cleanup()
{
: ${RM:=rm}
eval set -- $__task_refcount_file_temps__
local file
for file; do
${RM} -f "$file"
done
__task_refcount_file_temps__=
}
_task_refcount_file_init()
{
task_cleanup_add_hook _task_refcount_file_cleanup
}
# Static variable for temporary files that should be removed if an error occurs.
__task_refcount_file_temps__=
|