[f8d632ac] | 1 | <?xml version="1.0" encoding="ISO-8859-1"?>
|
---|
[6732c094] | 2 | <!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
|
---|
| 3 | "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
|
---|
[f8d632ac] | 4 | <!ENTITY % general-entities SYSTEM "../../general.ent">
|
---|
| 5 | %general-entities;
|
---|
| 6 | ]>
|
---|
| 7 |
|
---|
[0290a023] | 8 | <sect1 id="compressdoc" xreflabel="Compressing man and info pages">
|
---|
[53819cbb] | 9 | <?dbhtml filename="compressdoc.html"?>
|
---|
| 10 |
|
---|
| 11 |
|
---|
| 12 | <title>Compressing Man and Info Pages</title>
|
---|
| 13 |
|
---|
| 14 | <indexterm zone="compressdoc">
|
---|
| 15 | <primary sortas="b-compressdoc">compressdoc</primary>
|
---|
| 16 | </indexterm>
|
---|
| 17 |
|
---|
[853ae568] | 18 | <para>Man and info reader programs can transparently process files compressed
|
---|
| 19 | with <command>gzip</command> or <command>bzip2</command>, a feature you can
|
---|
[9545a987] | 20 | use to free some disk space while keeping your documentation
|
---|
| 21 | available. However, things are not that simple; man directories tend to
|
---|
| 22 | contain links—hard and symbolic—which defeat simple ideas like
|
---|
| 23 | recursively calling <command>gzip</command> on them. A better way to go is
|
---|
| 24 | to use the script below. If you would prefer to download the file instead of
|
---|
[f0dfc28] | 25 | creating it by typing or copy-and-pasting, you can find it at
|
---|
[9545a987] | 26 | <ulink url="&files-anduin;/compressdoc"/> (the file should be installed in
|
---|
| 27 | the <filename class="directory">/usr/sbin</filename> directory).</para>
|
---|
[53819cbb] | 28 |
|
---|
[8e37798] | 29 | <screen role="root"><?dbfo keep-together="auto"?><userinput>cat > /usr/sbin/compressdoc << "EOF"
|
---|
[53819cbb] | 30 | <literal>#!/bin/bash
|
---|
[47aad0c8] | 31 | # VERSION: 20080421.1623
|
---|
[13f51bbc] | 32 | #
|
---|
| 33 | # Compress (with bzip2 or gzip) all man pages in a hierarchy and
|
---|
[2fa79e3b] | 34 | # update symlinks - By Marc Heerdink <marc @ koelkast.net>
|
---|
[4ee1c44] | 35 | #
|
---|
[13f51bbc] | 36 | # Modified to be able to gzip or bzip2 files as an option and to deal
|
---|
[4e88613] | 37 | # with all symlinks properly by Mark Hymers <markh @ &lfs-domainname;>
|
---|
[13f51bbc] | 38 | #
|
---|
[2fa79e3b] | 39 | # Modified 20030930 by Yann E. Morin <yann.morin.1998 @ anciens.enib.fr>
|
---|
[13f51bbc] | 40 | # to accept compression/decompression, to correctly handle hard-links,
|
---|
| 41 | # to allow for changing hard-links into soft- ones, to specify the
|
---|
[b714def5] | 42 | # compression level, to parse the man.conf for all occurrences of MANPATH,
|
---|
[13f51bbc] | 43 | # to allow for a backup, to allow to keep the newest version of a page.
|
---|
[4ee1c44] | 44 | #
|
---|
[e0d33ef] | 45 | # Modified 20040330 by Tushar Teredesai to replace $0 by the name of the
|
---|
[4ee1c44] | 46 | # script.
|
---|
[4067e17b] | 47 | # (Note: It is assumed that the script is in the user's PATH)
|
---|
[13f51bbc] | 48 | #
|
---|
[4ee1c44] | 49 | # Modified 20050112 by Randy McMurchy to shorten line lengths and
|
---|
| 50 | # correct grammar errors.
|
---|
| 51 | #
|
---|
[9545a987] | 52 | # Modified 20060128 by Alexander E. Patrakov for compatibility with Man-DB.
|
---|
| 53 | #
|
---|
| 54 | # Modified 20060311 by Archaic to use Man-DB manpath utility which is a
|
---|
| 55 | # replacement for man --path from Man.
|
---|
| 56 | #
|
---|
[6deeee2] | 57 | # Modified 20080421 by Dan Nicholson to properly execute the correct
|
---|
| 58 | # compressdoc when working recursively. This means the same compressdoc
|
---|
| 59 | # will be used whether a full path was given or it was resolved from PATH.
|
---|
| 60 | #
|
---|
[48e6b2a] | 61 | # Modified 20080421 by Dan Nicholson to be more robust with directories
|
---|
| 62 | # that don't exist or don't have sufficient permissions.
|
---|
| 63 | #
|
---|
[62b2fb0] | 64 | # Modified 20080421 by Lars Bamberger to (sort of) automatically choose
|
---|
| 65 | # a compression method based on the size of the manpage. A couple bug
|
---|
| 66 | # fixes were added by Dan Nicholson.
|
---|
| 67 | #
|
---|
[47aad0c8] | 68 | # Modified 20080421 by Dan Nicholson to suppress warnings from manpath
|
---|
| 69 | # since these are emitted when $MANPATH is set. Removed the TODO for
|
---|
| 70 | # using the $MANPATH variable since manpath(1) handles this already.
|
---|
| 71 | #
|
---|
[13f51bbc] | 72 | # TODO:
|
---|
[4ee1c44] | 73 | # - choose a default compress method to be based on the available
|
---|
| 74 | # tool : gzip or bzip2;
|
---|
| 75 | # - offer an option to restore a previous backup;
|
---|
| 76 | # - add other compression engines (compress, zip, etc?). Needed?
|
---|
[70919be] | 77 |
|
---|
[13f51bbc] | 78 | # Funny enough, this function prints some help.
|
---|
| 79 | function help ()
|
---|
[70919be] | 80 | {
|
---|
[13f51bbc] | 81 | if [ -n "$1" ]; then
|
---|
| 82 | echo "Unknown option : $1"
|
---|
| 83 | fi
|
---|
[4067e17b] | 84 | ( echo "Usage: $MY_NAME <comp_method> [options] [dirs]" && \
|
---|
[13f51bbc] | 85 | cat << EOT
|
---|
[e6da9e5] | 86 | Where comp_method is one of :
|
---|
[13f51bbc] | 87 | --gzip, --gz, -g
|
---|
| 88 | --bzip2, --bz2, -b
|
---|
| 89 | Compress using gzip or bzip2.
|
---|
[62b2fb0] | 90 | --automatic
|
---|
| 91 | Compress using either gzip or bzip2, depending on the
|
---|
| 92 | size of the file to be compressed. Files larger than 5
|
---|
| 93 | kB are bzipped, files larger than 1 kB are gzipped and
|
---|
| 94 | files smaller than 1 kB are not compressed.
|
---|
[13f51bbc] | 95 |
|
---|
| 96 | --decompress, -d
|
---|
| 97 | Decompress the man pages.
|
---|
| 98 |
|
---|
[4ee1c44] | 99 | --backup Specify a .tar backup shall be done for all directories.
|
---|
[e0d33ef] | 100 | In case a backup already exists, it is saved as .tar.old
|
---|
| 101 | prior to making the new backup. If a .tar.old backup
|
---|
[4ee1c44] | 102 | exists, it is removed prior to saving the backup.
|
---|
[13f51bbc] | 103 | In backup mode, no other action is performed.
|
---|
| 104 |
|
---|
[e6da9e5] | 105 | And where options are :
|
---|
[13f51bbc] | 106 | -1 to -9, --fast, --best
|
---|
[e0d33ef] | 107 | The compression level, as accepted by gzip and bzip2.
|
---|
| 108 | When not specified, uses the default compression level
|
---|
| 109 | for the given method (-6 for gzip, and -9 for bzip2).
|
---|
[4ee1c44] | 110 | Not used when in backup or decompress modes.
|
---|
[13f51bbc] | 111 |
|
---|
[e0d33ef] | 112 | --force, -F Force (re-)compression, even if the previous one was
|
---|
| 113 | the same method. Useful when changing the compression
|
---|
| 114 | ratio. By default, a page will not be re-compressed if
|
---|
| 115 | it ends with the same suffix as the method adds
|
---|
[4ee1c44] | 116 | (.bz2 for bzip2, .gz for gzip).
|
---|
[e6da9e5] | 117 |
|
---|
[e0d33ef] | 118 | --soft, -S Change hard-links into soft-links. Use with _caution_
|
---|
| 119 | as the first encountered file will be used as a
|
---|
[4ee1c44] | 120 | reference. Not used when in backup mode.
|
---|
[13f51bbc] | 121 |
|
---|
[e0d33ef] | 122 | --hard, -H Change soft-links into hard-links. Not used when in
|
---|
[4ee1c44] | 123 | backup mode.
|
---|
[2fa79e3b] | 124 |
|
---|
[13f51bbc] | 125 | --conf=dir, --conf dir
|
---|
[9545a987] | 126 | Specify the location of man_db.conf. Defaults to /etc.
|
---|
[13f51bbc] | 127 |
|
---|
[e0d33ef] | 128 | --verbose, -v Verbose mode, print the name of the directory being
|
---|
| 129 | processed. Double the flag to turn it even more verbose,
|
---|
[4ee1c44] | 130 | and to print the name of the file being processed.
|
---|
[13f51bbc] | 131 |
|
---|
[9545a987] | 132 | --fake, -f Fakes it. Print the actual parameters compressdoc will use.
|
---|
[13f51bbc] | 133 |
|
---|
[e0d33ef] | 134 | dirs A list of space-separated _absolute_ pathnames to the
|
---|
[9545a987] | 135 | man directories. When empty, and only then, use manpath
|
---|
| 136 | to parse ${MAN_CONF}/man_db.conf for all valid occurrences
|
---|
| 137 | of MANDATORY_MANPATH.
|
---|
[13f51bbc] | 138 |
|
---|
[4ee1c44] | 139 | Note about compression:
|
---|
[13f51bbc] | 140 | There has been a discussion on blfs-support about compression ratios of
|
---|
| 141 | both gzip and bzip2 on man pages, taking into account the hosting fs,
|
---|
| 142 | the architecture, etc... On the overall, the conclusion was that gzip
|
---|
[e0d33ef] | 143 | was much more efficient on 'small' files, and bzip2 on 'big' files,
|
---|
[4ee1c44] | 144 | small and big being very dependent on the content of the files.
|
---|
[13f51bbc] | 145 |
|
---|
[e0d33ef] | 146 | See the original post from Mickael A. Peters, titled
|
---|
[4ee1c44] | 147 | "Bootable Utility CD", dated 20030409.1816(+0200), and subsequent posts:
|
---|
[4a570af1] | 148 | https://&lfs-domainname;/pipermail/blfs-support/2003-April/038817.html
|
---|
[13f51bbc] | 149 |
|
---|
[e0d33ef] | 150 | On my system (x86, ext3), man pages were 35564KB before compression.
|
---|
| 151 | gzip -9 compressed them down to 20372KB (57.28%), bzip2 -9 got down to
|
---|
[4ee1c44] | 152 | 19812KB (55.71%). That is a 1.57% gain in space. YMMV.
|
---|
| 153 |
|
---|
[e0d33ef] | 154 | What was not taken into consideration was the decompression speed. But
|
---|
| 155 | does it make sense to? You gain fast access with uncompressed man
|
---|
| 156 | pages, or you gain space at the expense of a slight overhead in time.
|
---|
[4ee1c44] | 157 | Well, my P4-2.5GHz does not even let me notice this... :-)
|
---|
[13f51bbc] | 158 |
|
---|
| 159 | EOT
|
---|
[2fa79e3b] | 160 | ) | less
|
---|
[13f51bbc] | 161 | }
|
---|
| 162 |
|
---|
[e0d33ef] | 163 | # This function checks that the man page is unique amongst bzip2'd,
|
---|
[4ee1c44] | 164 | # gzip'd and uncompressed versions.
|
---|
[13f51bbc] | 165 | # $1 the directory in which the file resides
|
---|
| 166 | # $2 the file name for the man page
|
---|
[e0d33ef] | 167 | # Returns 0 (true) if the file is the latest and must be taken care of,
|
---|
| 168 | # and 1 (false) if the file is not the latest (and has therefore been
|
---|
[4ee1c44] | 169 | # deleted).
|
---|
[13f51bbc] | 170 | function check_unique ()
|
---|
| 171 | {
|
---|
[e6da9e5] | 172 | # NB. When there are hard-links to this file, these are
|
---|
| 173 | # _not_ deleted. In fact, if there are hard-links, they
|
---|
[13f51bbc] | 174 | # all have the same date/time, thus making them ready
|
---|
| 175 | # for deletion later on.
|
---|
| 176 |
|
---|
[e6da9e5] | 177 | # Build the list of all man pages with the same name
|
---|
| 178 | DIR=$1
|
---|
[13f51bbc] | 179 | BASENAME=`basename "${2}" .bz2`
|
---|
| 180 | BASENAME=`basename "${BASENAME}" .gz`
|
---|
[408e76d] | 181 | GZ_FILE="$BASENAME".gz
|
---|
[b614eb09] | 182 | BZ_FILE="$BASENAME".bz2
|
---|
[13f51bbc] | 183 |
|
---|
| 184 | # Look for, and keep, the most recent one
|
---|
[4ee1c44] | 185 | LATEST=`(cd "$DIR"; ls -1rt "${BASENAME}" "${GZ_FILE}" "${BZ_FILE}" \
|
---|
| 186 | 2>/dev/null | tail -n 1)`
|
---|
[a72f9b7] | 187 | for i in "${BASENAME}" "${GZ_FILE}" "${BZ_FILE}"; do
|
---|
[e6da9e5] | 188 | [ "$LATEST" != "$i" ] && rm -f "$DIR"/"$i"
|
---|
[13f51bbc] | 189 | done
|
---|
| 190 |
|
---|
| 191 | # In case the specified file was the latest, return 0
|
---|
[e6da9e5] | 192 | [ "$LATEST" = "$2" ] && return 0
|
---|
[13f51bbc] | 193 | # If the file was not the latest, return 1
|
---|
| 194 | return 1
|
---|
[70919be] | 195 | }
|
---|
| 196 |
|
---|
[4067e17b] | 197 | # Name of the script
|
---|
| 198 | MY_NAME=`basename $0`
|
---|
| 199 |
|
---|
[e0d33ef] | 200 | # OK, parse the command-line for arguments, and initialize to some
|
---|
| 201 | # sensible state, that is: don't change links state, parse
|
---|
[9545a987] | 202 | # /etc/man_db.conf, be most silent, search man_db.conf in /etc, and don't
|
---|
[4ee1c44] | 203 | # force (re-)compression.
|
---|
[13f51bbc] | 204 | COMP_METHOD=
|
---|
| 205 | COMP_SUF=
|
---|
| 206 | COMP_LVL=
|
---|
[2fa79e3b] | 207 | FORCE_OPT=
|
---|
[13f51bbc] | 208 | LN_OPT=
|
---|
| 209 | MAN_DIR=
|
---|
[e6da9e5] | 210 | VERBOSE_LVL=0
|
---|
[13f51bbc] | 211 | BACKUP=no
|
---|
| 212 | FAKE=no
|
---|
| 213 | MAN_CONF=/etc
|
---|
| 214 | while [ -n "$1" ]; do
|
---|
| 215 | case $1 in
|
---|
| 216 | --gzip|--gz|-g)
|
---|
| 217 | COMP_SUF=.gz
|
---|
| 218 | COMP_METHOD=$1
|
---|
| 219 | shift
|
---|
| 220 | ;;
|
---|
| 221 | --bzip2|--bz2|-b)
|
---|
| 222 | COMP_SUF=.bz2
|
---|
| 223 | COMP_METHOD=$1
|
---|
| 224 | shift
|
---|
| 225 | ;;
|
---|
[62b2fb0] | 226 | --automatic)
|
---|
| 227 | COMP_SUF=TBD
|
---|
| 228 | COMP_METHOD=$1
|
---|
| 229 | shift
|
---|
| 230 | ;;
|
---|
[13f51bbc] | 231 | --decompress|-d)
|
---|
| 232 | COMP_SUF=
|
---|
| 233 | COMP_LVL=
|
---|
| 234 | COMP_METHOD=$1
|
---|
| 235 | shift
|
---|
| 236 | ;;
|
---|
| 237 | -[1-9]|--fast|--best)
|
---|
| 238 | COMP_LVL=$1
|
---|
| 239 | shift
|
---|
| 240 | ;;
|
---|
[e6da9e5] | 241 | --force|-F)
|
---|
[2fa79e3b] | 242 | FORCE_OPT=-F
|
---|
| 243 | shift
|
---|
| 244 | ;;
|
---|
| 245 | --soft|-S)
|
---|
| 246 | LN_OPT=-S
|
---|
[e6da9e5] | 247 | shift
|
---|
| 248 | ;;
|
---|
[2fa79e3b] | 249 | --hard|-H)
|
---|
| 250 | LN_OPT=-H
|
---|
[13f51bbc] | 251 | shift
|
---|
| 252 | ;;
|
---|
| 253 | --conf=*)
|
---|
| 254 | MAN_CONF=`echo $1 | cut -d '=' -f2-`
|
---|
| 255 | shift
|
---|
| 256 | ;;
|
---|
| 257 | --conf)
|
---|
| 258 | MAN_CONF="$2"
|
---|
| 259 | shift 2
|
---|
| 260 | ;;
|
---|
[e6da9e5] | 261 | --verbose|-v)
|
---|
| 262 | let VERBOSE_LVL++
|
---|
[13f51bbc] | 263 | shift
|
---|
| 264 | ;;
|
---|
| 265 | --backup)
|
---|
| 266 | BACKUP=yes
|
---|
| 267 | shift
|
---|
| 268 | ;;
|
---|
| 269 | --fake|-f)
|
---|
| 270 | FAKE=yes
|
---|
| 271 | shift
|
---|
| 272 | ;;
|
---|
| 273 | --help|-h)
|
---|
| 274 | help
|
---|
| 275 | exit 0
|
---|
| 276 | ;;
|
---|
| 277 | /*)
|
---|
| 278 | MAN_DIR="${MAN_DIR} ${1}"
|
---|
| 279 | shift
|
---|
| 280 | ;;
|
---|
| 281 | -*)
|
---|
| 282 | help $1
|
---|
| 283 | exit 1
|
---|
| 284 | ;;
|
---|
| 285 | *)
|
---|
[e0d33ef] | 286 | echo "\"$1\" is not an absolute path name"
|
---|
[13f51bbc] | 287 | exit 1
|
---|
| 288 | ;;
|
---|
| 289 | esac
|
---|
| 290 | done
|
---|
| 291 |
|
---|
| 292 | # Redirections
|
---|
[e6da9e5] | 293 | case $VERBOSE_LVL in
|
---|
[13f51bbc] | 294 | 0)
|
---|
[e6da9e5] | 295 | # O, be silent
|
---|
| 296 | DEST_FD0=/dev/null
|
---|
| 297 | DEST_FD1=/dev/null
|
---|
| 298 | VERBOSE_OPT=
|
---|
[13f51bbc] | 299 | ;;
|
---|
| 300 | 1)
|
---|
[e6da9e5] | 301 | # 1, be a bit verbose
|
---|
[13f51bbc] | 302 | DEST_FD0=/dev/stdout
|
---|
| 303 | DEST_FD1=/dev/null
|
---|
[e6da9e5] | 304 | VERBOSE_OPT=-v
|
---|
[13f51bbc] | 305 | ;;
|
---|
| 306 | *)
|
---|
[e6da9e5] | 307 | # 2 and above, be most verbose
|
---|
| 308 | DEST_FD0=/dev/stdout
|
---|
| 309 | DEST_FD1=/dev/stdout
|
---|
| 310 | VERBOSE_OPT="-v -v"
|
---|
[13f51bbc] | 311 | ;;
|
---|
| 312 | esac
|
---|
[70919be] | 313 |
|
---|
[e0d33ef] | 314 | # Note: on my machine, 'man --path' gives /usr/share/man twice, once
|
---|
[4ee1c44] | 315 | # with a trailing '/', once without.
|
---|
[13f51bbc] | 316 | if [ -z "$MAN_DIR" ]; then
|
---|
[47aad0c8] | 317 | MAN_DIR=`manpath -q -C "$MAN_CONF"/man_db.conf \
|
---|
[13f51bbc] | 318 | | sed 's/:/\\n/g' \
|
---|
| 319 | | while read foo; do dirname "$foo"/.; done \
|
---|
| 320 | | sort -u \
|
---|
| 321 | | while read bar; do echo -n "$bar "; done`
|
---|
[70919be] | 322 | fi
|
---|
| 323 |
|
---|
[9545a987] | 324 | # If no MANDATORY_MANPATH in ${MAN_CONF}/man_db.conf, abort as well
|
---|
[13f51bbc] | 325 | if [ -z "$MAN_DIR" ]; then
|
---|
[9545a987] | 326 | echo "No directory specified, and no directory found with \`manpath'"
|
---|
[13f51bbc] | 327 | exit 1
|
---|
| 328 | fi
|
---|
[70919be] | 329 |
|
---|
[48e6b2a] | 330 | # Check that the specified directories actually exist and are readable
|
---|
| 331 | for DIR in $MAN_DIR; do
|
---|
| 332 | if [ ! -d "$DIR" -o ! -r "$DIR" ]; then
|
---|
| 333 | echo "Directory '$DIR' does not exist or is not readable"
|
---|
| 334 | exit 1
|
---|
| 335 | fi
|
---|
| 336 | done
|
---|
| 337 |
|
---|
[13f51bbc] | 338 | # Fake?
|
---|
| 339 | if [ "$FAKE" != "no" ]; then
|
---|
| 340 | echo "Actual parameters used:"
|
---|
| 341 | echo -n "Compression.......: "
|
---|
| 342 | case $COMP_METHOD in
|
---|
| 343 | --bzip2|--bz2|-b) echo -n "bzip2";;
|
---|
[62b2fb0] | 344 | --gzip|--gz|-g) echo -n "gzip";;
|
---|
| 345 | --automatic) echo -n "compressing";;
|
---|
[13f51bbc] | 346 | --decompress|-d) echo -n "decompressing";;
|
---|
| 347 | *) echo -n "unknown";;
|
---|
| 348 | esac
|
---|
| 349 | echo " ($COMP_METHOD)"
|
---|
| 350 | echo "Compression level.: $COMP_LVL"
|
---|
| 351 | echo "Compression suffix: $COMP_SUF"
|
---|
[2fa79e3b] | 352 | echo -n "Force compression.: "
|
---|
| 353 | [ "foo$FORCE_OPT" = "foo-F" ] && echo "yes" || echo "no"
|
---|
[9545a987] | 354 | echo "man_db.conf is....: ${MAN_CONF}/man_db.conf"
|
---|
[2fa79e3b] | 355 | echo -n "Hard-links........: "
|
---|
[e0d33ef] | 356 | [ "foo$LN_OPT" = "foo-S" ] &&
|
---|
[4ee1c44] | 357 | echo "convert to soft-links" || echo "leave as is"
|
---|
[2fa79e3b] | 358 | echo -n "Soft-links........: "
|
---|
[e0d33ef] | 359 | [ "foo$LN_OPT" = "foo-H" ] &&
|
---|
[4ee1c44] | 360 | echo "convert to hard-links" || echo "leave as is"
|
---|
[13f51bbc] | 361 | echo "Backup............: $BACKUP"
|
---|
| 362 | echo "Faking (yes!).....: $FAKE"
|
---|
| 363 | echo "Directories.......: $MAN_DIR"
|
---|
[2fa79e3b] | 364 | echo "Verbosity level...: $VERBOSE_LVL"
|
---|
[13f51bbc] | 365 | exit 0
|
---|
| 366 | fi
|
---|
[70919be] | 367 |
|
---|
[13f51bbc] | 368 | # If no method was specified, print help
|
---|
| 369 | if [ -z "${COMP_METHOD}" -a "${BACKUP}" = "no" ]; then
|
---|
| 370 | help
|
---|
| 371 | exit 1
|
---|
[70919be] | 372 | fi
|
---|
| 373 |
|
---|
[666f6de] | 374 | # In backup mode, do the backup solely
|
---|
[13f51bbc] | 375 | if [ "$BACKUP" = "yes" ]; then
|
---|
| 376 | for DIR in $MAN_DIR; do
|
---|
| 377 | cd "${DIR}/.."
|
---|
[48e6b2a] | 378 | if [ ! -w "`pwd`" ]; then
|
---|
| 379 | echo "Directory '`pwd`' is not writable"
|
---|
| 380 | exit 1
|
---|
| 381 | fi
|
---|
[13f51bbc] | 382 | DIR_NAME=`basename "${DIR}"`
|
---|
| 383 | echo "Backing up $DIR..." > $DEST_FD0
|
---|
| 384 | [ -f "${DIR_NAME}.tar.old" ] && rm -f "${DIR_NAME}.tar.old"
|
---|
[e0d33ef] | 385 | [ -f "${DIR_NAME}.tar" ] &&
|
---|
[4ee1c44] | 386 | mv "${DIR_NAME}.tar" "${DIR_NAME}.tar.old"
|
---|
[4fb71d8e] | 387 | tar -cvf "${DIR_NAME}.tar" "${DIR_NAME}" > $DEST_FD1
|
---|
[13f51bbc] | 388 | done
|
---|
| 389 | exit 0
|
---|
| 390 | fi
|
---|
| 391 |
|
---|
| 392 | # I know MAN_DIR has only absolute path names
|
---|
| 393 | # I need to take into account the localized man, so I'm going recursive
|
---|
| 394 | for DIR in $MAN_DIR; do
|
---|
[2fa79e3b] | 395 | MEM_DIR=`pwd`
|
---|
[48e6b2a] | 396 | if [ ! -w "$DIR" ]; then
|
---|
| 397 | echo "Directory '$DIR' is not writable"
|
---|
| 398 | exit 1
|
---|
| 399 | fi
|
---|
[13f51bbc] | 400 | cd "$DIR"
|
---|
| 401 | for FILE in *; do
|
---|
[e6da9e5] | 402 | # Fixes the case were the directory is empty
|
---|
[13f51bbc] | 403 | if [ "foo$FILE" = "foo*" ]; then continue; fi
|
---|
[e6da9e5] | 404 |
|
---|
| 405 | # Fixes the case when hard-links see their compression scheme change
|
---|
[e0d33ef] | 406 | # (from not compressed to compressed, or from bz2 to gz, or from gz
|
---|
[4ee1c44] | 407 | # to bz2)
|
---|
[e0d33ef] | 408 | # Also fixes the case when multiple version of the page are present,
|
---|
[4ee1c44] | 409 | # which are either compressed or not.
|
---|
[e6da9e5] | 410 | if [ ! -L "$FILE" -a ! -e "$FILE" ]; then continue; fi
|
---|
| 411 |
|
---|
[a72f9b7] | 412 | # Do not compress whatis files
|
---|
| 413 | if [ "$FILE" = "whatis" ]; then continue; fi
|
---|
| 414 |
|
---|
[13f51bbc] | 415 | if [ -d "$FILE" ]; then
|
---|
| 416 | # We are going recursive to that directory
|
---|
| 417 | echo "-> Entering ${DIR}/${FILE}..." > $DEST_FD0
|
---|
| 418 | # I need not pass --conf, as I specify the directory to work on
|
---|
[6deeee2] | 419 | # But I need exit in case of error. We must change back to the
|
---|
| 420 | # original directory so $0 is resolved correctly.
|
---|
| 421 | (cd "$MEM_DIR" && eval "$0" ${COMP_METHOD} ${COMP_LVL} ${LN_OPT} \
|
---|
| 422 | ${VERBOSE_OPT} ${FORCE_OPT} "${DIR}/${FILE}") || exit $?
|
---|
[13f51bbc] | 423 | echo "<- Leaving ${DIR}/${FILE}." > $DEST_FD1
|
---|
[e6da9e5] | 424 |
|
---|
[13f51bbc] | 425 | else # !dir
|
---|
[e6da9e5] | 426 | if ! check_unique "$DIR" "$FILE"; then continue; fi
|
---|
| 427 |
|
---|
[62b2fb0] | 428 | # With automatic compression, get the uncompressed file size of
|
---|
| 429 | # the file (dereferencing symlinks), and choose an appropriate
|
---|
| 430 | # compression method.
|
---|
| 431 | if [ "$COMP_METHOD" = "--automatic" ]; then
|
---|
| 432 | declare -i SIZE
|
---|
| 433 | case "$FILE" in
|
---|
| 434 | *.bz2)
|
---|
| 435 | SIZE=$(bzcat "$FILE" | wc -c) ;;
|
---|
| 436 | *.gz)
|
---|
| 437 | SIZE=$(zcat "$FILE" | wc -c) ;;
|
---|
| 438 | *)
|
---|
| 439 | SIZE=$(wc -c < "$FILE") ;;
|
---|
| 440 | esac
|
---|
| 441 | if (( $SIZE >= (5 * 2**10) )); then
|
---|
| 442 | COMP_SUF=.bz2
|
---|
| 443 | elif (( $SIZE >= (1 * 2**10) )); then
|
---|
| 444 | COMP_SUF=.gz
|
---|
| 445 | else
|
---|
| 446 | COMP_SUF=
|
---|
| 447 | fi
|
---|
| 448 | fi
|
---|
| 449 |
|
---|
[e6da9e5] | 450 | # Check if the file is already compressed with the specified method
|
---|
[b614eb09] | 451 | BASE_FILE=`basename "$FILE" .gz`
|
---|
[408e76d] | 452 | BASE_FILE=`basename "$BASE_FILE" .bz2`
|
---|
[4ee1c44] | 453 | if [ "${FILE}" = "${BASE_FILE}${COMP_SUF}" \
|
---|
| 454 | -a "foo${FORCE_OPT}" = "foo" ]; then continue; fi
|
---|
[13f51bbc] | 455 |
|
---|
| 456 | # If we have a symlink
|
---|
| 457 | if [ -h "$FILE" ]; then
|
---|
[b614eb09] | 458 | case "$FILE" in
|
---|
[13f51bbc] | 459 | *.bz2)
|
---|
| 460 | EXT=bz2 ;;
|
---|
| 461 | *.gz)
|
---|
| 462 | EXT=gz ;;
|
---|
| 463 | *)
|
---|
| 464 | EXT=none ;;
|
---|
| 465 | esac
|
---|
| 466 |
|
---|
[e6da9e5] | 467 | if [ ! "$EXT" = "none" ]; then
|
---|
[4ee1c44] | 468 | LINK=`ls -l "$FILE" | cut -d ">" -f2 \
|
---|
| 469 | | tr -d " " | sed s/\.$EXT$//`
|
---|
[13f51bbc] | 470 | NEWNAME=`echo "$FILE" | sed s/\.$EXT$//`
|
---|
| 471 | mv "$FILE" "$NEWNAME"
|
---|
| 472 | FILE="$NEWNAME"
|
---|
| 473 | else
|
---|
[b614eb09] | 474 | LINK=`ls -l "$FILE" | cut -d ">" -f2 | tr -d " "`
|
---|
[13f51bbc] | 475 | fi
|
---|
| 476 |
|
---|
[2fa79e3b] | 477 | if [ "$LN_OPT" = "-H" ]; then
|
---|
| 478 | # Change this soft-link into a hard- one
|
---|
| 479 | rm -f "$FILE" && ln "${LINK}$COMP_SUF" "${FILE}$COMP_SUF"
|
---|
| 480 | chmod --reference "${LINK}$COMP_SUF" "${FILE}$COMP_SUF"
|
---|
| 481 | else
|
---|
| 482 | # Keep this soft-link a soft- one.
|
---|
| 483 | rm -f "$FILE" && ln -s "${LINK}$COMP_SUF" "${FILE}$COMP_SUF"
|
---|
| 484 | fi
|
---|
[13f51bbc] | 485 | echo "Relinked $FILE" > $DEST_FD1
|
---|
| 486 |
|
---|
| 487 | # else if we have a plain file
|
---|
| 488 | elif [ -f "$FILE" ]; then
|
---|
| 489 | # Take care of hard-links: build the list of files hard-linked
|
---|
| 490 | # to the one we are {de,}compressing.
|
---|
[e0d33ef] | 491 | # NB. This is not optimum has the file will eventually be
|
---|
| 492 | # compressed as many times it has hard-links. But for now,
|
---|
[4ee1c44] | 493 | # that's the safe way.
|
---|
[13f51bbc] | 494 | inode=`ls -li "$FILE" | awk '{print $1}'`
|
---|
| 495 | HLINKS=`find . \! -name "$FILE" -inum $inode`
|
---|
| 496 |
|
---|
| 497 | if [ -n "$HLINKS" ]; then
|
---|
| 498 | # We have hard-links! Remove them now.
|
---|
| 499 | for i in $HLINKS; do rm -f "$i"; done
|
---|
| 500 | fi
|
---|
| 501 |
|
---|
| 502 | # Now take care of the file that has no hard-link
|
---|
[666f6de] | 503 | # We do decompress first to re-compress with the selected
|
---|
[13f51bbc] | 504 | # compression ratio later on...
|
---|
[b614eb09] | 505 | case "$FILE" in
|
---|
[13f51bbc] | 506 | *.bz2)
|
---|
| 507 | bunzip2 $FILE
|
---|
[e6da9e5] | 508 | FILE=`basename "$FILE" .bz2`
|
---|
[13f51bbc] | 509 | ;;
|
---|
| 510 | *.gz)
|
---|
| 511 | gunzip $FILE
|
---|
[e6da9e5] | 512 | FILE=`basename "$FILE" .gz`
|
---|
[13f51bbc] | 513 | ;;
|
---|
| 514 | esac
|
---|
| 515 |
|
---|
[2fa79e3b] | 516 | # Compress the file with the given compression ratio, if needed
|
---|
[13f51bbc] | 517 | case $COMP_SUF in
|
---|
| 518 | *bz2)
|
---|
| 519 | bzip2 ${COMP_LVL} "$FILE" && chmod 644 "${FILE}${COMP_SUF}"
|
---|
| 520 | echo "Compressed $FILE" > $DEST_FD1
|
---|
| 521 | ;;
|
---|
| 522 | *gz)
|
---|
| 523 | gzip ${COMP_LVL} "$FILE" && chmod 644 "${FILE}${COMP_SUF}"
|
---|
| 524 | echo "Compressed $FILE" > $DEST_FD1
|
---|
| 525 | ;;
|
---|
| 526 | *)
|
---|
| 527 | echo "Uncompressed $FILE" > $DEST_FD1
|
---|
| 528 | ;;
|
---|
| 529 | esac
|
---|
| 530 |
|
---|
| 531 | # If the file had hard-links, recreate those (either hard or soft)
|
---|
| 532 | if [ -n "$HLINKS" ]; then
|
---|
| 533 | for i in $HLINKS; do
|
---|
[b614eb09] | 534 | NEWFILE=`echo "$i" | sed s/\.gz$// | sed s/\.bz2$//`
|
---|
[2fa79e3b] | 535 | if [ "$LN_OPT" = "-S" ]; then
|
---|
| 536 | # Make this hard-link a soft- one
|
---|
| 537 | ln -s "${FILE}$COMP_SUF" "${NEWFILE}$COMP_SUF"
|
---|
| 538 | else
|
---|
| 539 | # Keep the hard-link a hard- one
|
---|
| 540 | ln "${FILE}$COMP_SUF" "${NEWFILE}$COMP_SUF"
|
---|
| 541 | fi
|
---|
[4ee1c44] | 542 | # Really work only for hard-links. Harmless for soft-links
|
---|
| 543 | chmod 644 "${NEWFILE}$COMP_SUF"
|
---|
[13f51bbc] | 544 | done
|
---|
| 545 | fi
|
---|
| 546 |
|
---|
| 547 | else
|
---|
[e0d33ef] | 548 | # There is a problem when we get neither a symlink nor a plain
|
---|
[4ee1c44] | 549 | # file. Obviously, we shall never ever come here... :-(
|
---|
| 550 | echo -n "Whaooo... \"${DIR}/${FILE}\" is neither a symlink "
|
---|
| 551 | echo "nor a plain file. Please check:"
|
---|
[b614eb09] | 552 | ls -l "${DIR}/${FILE}"
|
---|
[13f51bbc] | 553 | exit 1
|
---|
| 554 | fi
|
---|
| 555 | fi
|
---|
| 556 | done # for FILE
|
---|
[53819cbb] | 557 | done # for DIR</literal>
|
---|
[4ee1c44] | 558 |
|
---|
[9545a987] | 559 | EOF</userinput></screen>
|
---|
[f3429309] | 560 |
|
---|
[f0dfc28] | 561 | <note>
|
---|
| 562 | <para>
|
---|
| 563 | Doing a very large copy/paste directly to a terminal may result in a
|
---|
| 564 | corrupted file. Copying to an editor may overcome this issue.
|
---|
| 565 | </para>
|
---|
| 566 | </note>
|
---|
[9545a987] | 567 |
|
---|
| 568 | <para>As <systemitem class="username">root</systemitem>, make
|
---|
| 569 | <command>compressdoc</command> executable for all users:</para>
|
---|
| 570 |
|
---|
| 571 | <screen><userinput>chmod -v 755 /usr/sbin/compressdoc</userinput></screen>
|
---|
[70919be] | 572 |
|
---|
[853ae568] | 573 | <para>Now, as <systemitem class="username">root</systemitem>, you can issue
|
---|
| 574 | the command <command>compressdoc --bz2</command> to compress all your system man
|
---|
[e0d33ef] | 575 | pages. You can also run <command>compressdoc --help</command> to get
|
---|
[53819cbb] | 576 | comprehensive help about what the script is able to do.</para>
|
---|
[13f51bbc] | 577 |
|
---|
[9545a987] | 578 | <para>Don't forget that a few programs, like the <application>X Window
|
---|
[853ae568] | 579 | System</application> and <application>XEmacs</application> also
|
---|
| 580 | install their documentation in non-standard places (such as
|
---|
| 581 | <filename class="directory">/usr/X11R6/man</filename>, etc.). Be sure
|
---|
[9545a987] | 582 | to add these locations to the file <filename>/etc/man_db.conf</filename>, as
|
---|
[3597eb6] | 583 | <envar>MANDATORY_MANPATH</envar> <replaceable></path></replaceable>
|
---|
[9545a987] | 584 | lines.</para>
|
---|
[4ee1c44] | 585 |
|
---|
[9545a987] | 586 | <para>Example:</para>
|
---|
[4ee1c44] | 587 |
|
---|
[53819cbb] | 588 | <screen><literal> ...
|
---|
[9545a987] | 589 | MANDATORY_MANPATH /usr/share/man
|
---|
| 590 | MANDATORY_MANPATH /usr/X11R6/man
|
---|
| 591 | MANDATORY_MANPATH /usr/local/man
|
---|
| 592 | MANDATORY_MANPATH /opt/qt/doc/man
|
---|
[53819cbb] | 593 | ...</literal></screen>
|
---|
| 594 |
|
---|
[e0d33ef] | 595 | <para>Generally, package installation systems do not compress man/info pages,
|
---|
| 596 | which means you will need to run the script again if you want to keep the size
|
---|
[53819cbb] | 597 | of your documentation as small as possible. Also, note that running the script
|
---|
[e0d33ef] | 598 | after upgrading a package is safe; when you have several versions of a page
|
---|
[53819cbb] | 599 | (for example, one compressed and one uncompressed), the most recent one is kept
|
---|
[853ae568] | 600 | and the others are deleted.</para>
|
---|
[70919be] | 601 |
|
---|
| 602 | </sect1>
|
---|