OSDN Git Service

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