OSDN Git Service

[IMPROVED] Adapt to the specification change of pkg-create(8) that the default extens...
[portsreinstall/current.git] / lib / libdatabase_maintain.sh
1 #!/bin/sh -e
2 # ==============================================================================
3 # portsreinstall library script
4 # - Operations for maintaining the temporary database -
5 # Copyright (C) 2018-2021 Mamoru Sakaue, MwGhennndo, All Rights Reserved.
6 # This software is distributed under the 2-Clause BSD License.
7 # ==============================================================================
8
9
10 # ============= Variables =============
11 DATABASE_VERSION=
12 DATABASE_IS_OBSOLETE=no
13
14
15 # ============= Cleaning of the temporary database =============
16 database_maintain_clean_all ()
17 {
18         rm -rf "${DBDIR}"
19 }
20
21 # ============= Reset the execution result flags =============
22 database_maintain_reset_execflag ()
23 {
24         rm -rf "${DBDIR}/execflag"
25 }
26
27 # ============= Load a temporary database from an archive =============
28 database_maintain_load ()
29 {
30         local filepath dbdir_parent
31         filepath=$1
32         dbdir_parent=`dirname "${DBDIR}"`
33         mkdir -p "$dbdir_parent"
34         tar xzf "$filepath" -C "$dbdir_parent" --exclude "*/.lock"
35 }
36
37 # ============= Save the temporary database to an archive =============
38 database_maintain_save ()
39 {
40         local filepath dbdir_parent dbdir_node
41         filepath=$1
42         dbdir_parent=`dirname "${DBDIR}"`
43         dbdir_node=`basename "${DBDIR}"`
44         tar czf "$filepath" -C "$dbdir_parent" "$dbdir_node"
45 }
46
47 # ============= Creation of the temporary database =============
48 database_maintain_create ()
49 {
50         local subdir
51         [ `id -u` -eq 0 ] && mkdir -p "${DBDIR}"
52         misc_lock_duplicated_executions "${DBDIR}/.lock"
53         if [ -e "${DBDIR}/MYVERSION" ]
54         then
55                 if ! grep -q -E "$COMPATIBLE_VERSIONS" "${DBDIR}/MYVERSION" 2> /dev/null
56                 then
57                         message_echo "ERROR: The current temporary database is incompatible. You must delete it by" >&2
58                         message_echo "        ${APPNAME} clean force" >&2
59                         message_echo "       in order to enable the current version." >&2
60                         exit 1
61                 fi
62         elif misc_is_superuser_privilege
63         then
64                 echo "$MYVERSION" > ${DBDIR}/MYVERSION
65         fi
66         DATABASE_VERSION=`cat "${DBDIR}"/MYVERSION 2> /dev/null || :`
67         misc_is_superuser_privilege || return 0
68         for subdir in initial requires replace targets obsolete backup_packages \
69                 stage.loop_list stage.complete stage.reinit_loop stage.depends
70         do
71                 mkdir -p "${DBDIR}/$subdir"
72         done
73 }
74
75 # ============= Mark the use of the temporary database =============
76 database_maintain_mark_use ()
77 {
78          touch "${DBDIR}/in_use"
79 }
80
81 # ============= Check the use of the temporary database =============
82 database_maintain_chk_use ()
83 {
84          [ -e "${DBDIR}/in_use" ]
85 }
86
87 # ============= Refresh the temporary database =============
88 database_maintain_refresh ()
89 {
90         misc_is_superuser_privilege || return
91         [ $opt_suppress_obsolete_db_clean = no ] || return
92         message_echo "INFO: The temporary database is cleaned up."
93         message_echo
94         [ -d "${DBDIR}" -a ! -d "${DBDIR}.tmp" ] && mv "${DBDIR}" "${DBDIR}.tmp"
95         database_maintain_create
96         mv "${DBDIR}.tmp/saved_options.sh" "${DBDIR}" 2> /dev/null || :
97         mv "${DBDIR}.tmp/backup_packages" "${DBDIR}" 2> /dev/null || :
98         mv "${DBDIR}.tmp/backup_pkgarcs.lst" "${DBDIR}" 2> /dev/null || :
99         rm -rf "${DBDIR}.tmp"
100 }
101
102 # ============= Clean up the temporary database for upgrade of this utility =============
103 database_maintain_clean_for_self_upgrade ()
104 {
105         misc_is_superuser_privilege || return
106         [ $opt_suppress_obsolete_db_clean = no ] || return
107         rm -rf "${DBDIR}"
108         database_maintain_create
109         [ -e "${DBDIR}/MYVERSION" ] && mv "${DBDIR}/MYVERSION" "${DBDIR}/MYVERSION.prev"
110         :
111 }
112
113 # ============= Check whether the temporary database is newer than the ports tree and refresh if so =============
114 database_maintain_refresh_if_obsolete ()
115 {
116         if [ "${PORTS_INDEX_DB}" -nt "${DBDIR}"/MYVERSION ] && misc_is_superuser_privilege
117         then
118                 if [ $opt_suppress_obsolete_db_clean = no -a "z${command}" = zclean ]
119                 then
120                         DATABASE_IS_OBSOLETE=no
121                         message_echo "WARNING: The temporary database is older than the ports tree." >&2
122                         database_maintain_refresh || DATABASE_IS_OBSOLETE=yes
123                 else
124                         DATABASE_IS_OBSOLETE=yes
125                 fi
126         else
127                 DATABASE_IS_OBSOLETE=no
128         fi
129 }
130
131 # ============= Resetting of the temporary database =============
132 database_maintain_reset ()
133 {
134         local mode
135         mode=$1
136         if [ $mode = keepstatus ]
137         then
138                 if [ ! -d "${DBDIR}/prevset" ]
139                 then
140                         mkdir -p "${DBDIR}/prevset"
141                         {
142                                 echo requires
143                                 echo notes
144                                 echo to_be_reconf
145                                 echo failed.list
146                                 echo damaged_package
147                                 echo manually_done.list
148                                 for tag in all run build none
149                                 do
150                                         for level in full direct
151                                         do
152                                                 echo "success.$tag.$level.list"
153                                         done
154                                 done
155                         } | while read content
156                         do
157                                 [ -e "${DBDIR}/$content" ] && mv "${DBDIR}/$content" "${DBDIR}/prevset"
158                         done
159                 fi
160         else
161                 rm -rf "${DBDIR}"/requires.prev "${DBDIR}"/notes.prev
162         fi
163         find "${DBDIR}" -depth 1 -maxdepth 1 \
164                 -not \( -name saved_options.sh -or -name initial \
165                         -or -name fossil_pkgs -or -name moved_from \
166                         -or -name MYVERSION -or -name .lock \
167                         -or -name journal -or -name prevset \
168                         -or -name backup_failure -or -name installed_ports \
169                         -or -name installed_ports:pkg_vs_origin.tbl \) \
170                 -exec rm -rf {} \; 2> /dev/null || :
171         touch "${DBDIR}/MYVERSION"
172         case $mode in
173         all )
174                 command_exec_without_pkgtools__notify_reset_options
175                 rm -f "${DBDIR}/saved_options.sh" "${DBDIR}/prevset" "${DBDIR}/journal"*
176                 ;;
177         keepopts | keepstatus )
178                 (
179                         opt_batch_mode=yes
180                         while read srcline
181                         do
182                                 command_flexconf_update_taboo $srcline
183                         done < ${DBDIR}/journal/taboo || :
184                         while read srcline
185                         do
186                                 command_flexconf_update_freeze $srcline
187                         done < ${DBDIR}/journal/freeze || :
188                         while read srcline
189                         do
190                                 command_flexconf_update_need $srcline
191                         done < ${DBDIR}/journal/need || :
192                         while read srcline
193                         do
194                                 command_flexconf_update_noneed $srcline
195                         done < ${DBDIR}/journal/noneed || :
196                 ) 2> /dev/null
197                 ;;
198         esac
199 }
200
201 # ============= Clear the data of previous progress status =============
202 database_maintain_clear_prevset ()
203 {
204         rm -rf "${DBDIR}/prevset"
205 }