Ajout option pour effacer un fichier avant de le re-telecharger
[hvlinux.git] / functions / fpkg
index 020443b..c420d7b 100644 (file)
@@ -71,6 +71,16 @@ update_packages_init()
        install -v -m755 -d ${LFS_PKG_DIR} 1> ${LFS_LOG_FILE} 2>&1 || exit 1
     fi
 
+    if [ -n "${TEST_INTEGRITY}" ]; then
+        # En mode test intégrité, on ne veut pas télécharger quoi que ce soit
+        return 0
+    fi
+
+    if [ ! -x /usr/bin/wget ]; then
+       echo "${FUNCNAME}() error, the wget package was not found."
+       return 1
+    fi
+
     if [ -n "${USE_LFS_PATCHES}" ]; then
         # Getting list of all patches from LFS server.
         rcmd "Fetching LFS patches list" static_fetch_patches_list ${LFS_PATCHES_URL} ${LFS_PATCHES_LIST}
@@ -153,17 +163,15 @@ static_getpatch()
 
 detect_file_not_found()
 {
-    # HTTP: will return error code 404.
-    # FTP: will say "File not found"
-    if grep "404" ${WGET_LOG_FILE} 1> /dev/null 2>&1; then
+    # HTTP: will return "ERROR 404: Not Found"
+    # FTP: will say "File not found" or "No such file"
+    if grep --ignore-case -e "not found" -e "no such file" ${WGET_LOG_FILE} \
+        1> /dev/null 2>&1; then
         #echo "404 NOTFOUND"
         return 0
-    elif grep "No such file" ${WGET_LOG_FILE} 1> /dev/null 2>&1; then
-        #echo "No such file"
-        return 0
-    else
-        return 1
     fi
+    
+    return 1
 }
 
 # Get package if it is not in the repository
@@ -198,7 +206,8 @@ static_getpkg()
         # so we can safely remove any file prior to trying to download it.
         rm -f ${LFS_PKG_DIR}/${PACK}.${arch_ext}
 
-        rcmd "Fetching ${PACK}.${arch_ext}" ${WGETCMD} ${URL}/${PACK}.${arch_ext}
+        rcmd "Fetching ${PACK}.${arch_ext}" \
+            ${WGETCMD} ${URL}/${PACK}.${arch_ext}
         wget_status=$?
 
         # Append log to global log file
@@ -206,7 +215,8 @@ static_getpkg()
 
         # Failure: if it was a connection timeout, don't try for other file
         # extensions.
-        if grep "failed: Connection timed out" ${WGET_LOG_FILE} 1> /dev/null 2>&1; then
+        if grep "failed: Connection timed out" ${WGET_LOG_FILE} \
+            1> /dev/null 2>&1; then
             echo "Error, wget reported: Connection timed out"
             return 1
         fi
@@ -255,7 +265,8 @@ test_archive_integrity()
     local EXT=${3}
 
     if [ ! -f ${FILE} ]; then
-        return 1
+        # The file may be absent, in this case simply abort without error
+        return 0
     fi
 
     local TESTCMD=""
@@ -290,8 +301,8 @@ test_archive_integrity()
 #            pm (Perl module via CPAN)
 #            fd (freedesktop.org)
 #   -o     Option specific to mode
-#   -h     Display this help and returns
 #   -s     Subdirectory on server
+#   -w     First erase destination file if it exists (except in test mode)
 fpkg()
 {
     local ORIG_ARGS=${*}
@@ -302,8 +313,9 @@ fpkg()
     local MODE_OPT=""
     local SRC_DIR=""
     local FD_SUBDIR_FINAL=""
+    unset ERASE_FIRST
 
-    while getopts "d:e:f:m:o:s:" flag ;do
+    while getopts "d:e:f:m:o:s:w" flag ;do
         case ${flag} in
            d)
                 # Fetch directory (where to put file)
@@ -326,6 +338,9 @@ fpkg()
            s)
                SRC_DIR=${OPTARG}
                 ;;
+            w)
+                ERASE_FIRST="y"
+               ;;
            ?)
                echo "${FUNCNAME}(): Invalid option: ${OPTARG}."
                return 1
@@ -398,30 +413,29 @@ fpkg()
                 # Most common layout:
                 # http://pkg-config.freedesktop.org/releases/
 
-                if [ -z "${SRC_DIR}" ]; then
+                if [ -z "${MODE_OPT}" ]; then
                     # Default subdirectory or module on server
-                   SRC_DIR=$(get_pkg_name ${PACK})
+                   MODE_OPT=$(get_pkg_name ${PACK})
                 fi
-
                 if [ -z "${FILE_EXT}" ]; then
                     # Default file extension is always tar.gz???
                     FILE_EXT="tar.gz"
                 fi
 
                 FD_SUBDIR_FINAL="releases"
-                if [ -n "${MODE_OPT}" ]; then
+                if [ -n "${SRC_DIR}" ]; then
                     # Optional final subdir:
                     #   releases (default)
                     #   dist
                     #   none (don't append anything)
-                    if [ "x${MODE_OPT}" = "xnone" ]; then
+                    if [ "x${SRC_DIR}" = "xnone" ]; then
                         FD_SUBDIR_FINAL=""
                     else
-                        FD_SUBDIR_FINAL=${MODE_OPT}
+                        FD_SUBDIR_FINAL=${SRC_DIR}
                     fi
                 fi
 
-                URL="http://${SRC_DIR}.freedesktop.org/${FD_SUBDIR_FINAL}"
+                URL="http://${MODE_OPT}.freedesktop.org/${FD_SUBDIR_FINAL}"
                 SRC_DIR=""
                 ;;
             apa)
@@ -497,23 +511,34 @@ fpkg()
 
     DEST_FILE=${DEST_DIR}/${PACK}.${FINAL_EXT}
 
-    if [ ! -f ${DEST_FILE} ]; then
-        set +e
-        static_getpkg ${SRC_FILENAME} ${URL} ${FILE_EXT}
-        set -e
+    if [ -z "${TEST_INTEGRITY}" ]; then
+        if [ -f ${DEST_FILE} -a -n "${ERASE_FIRST}" ]; then
+            rm ${DEST_FILE}
+        fi
 
-        # Move file if source filename is not equal to package name and/or
-        # destination directory is not the default:
-        DOWNLOADED_FILE=${LFS_PKG_DIR}/${SRC_FILENAME}.${FINAL_EXT}
-        DEST_FILE=${DEST_DIR}/${PACK}.${FINAL_EXT}
+        if [ !  -f ${DEST_FILE} ]; then
+            # Fetch package
+            set +e
+            static_getpkg ${SRC_FILENAME} ${URL} ${FILE_EXT}
+            set -e
 
-        if [ "${DEST_FILE}" != "${DOWNLOADED_FILE}" ]; then
-           mv ${DOWNLOADED_FILE} ${DEST_FILE}
-       fi
-    fi
+            # Move file if source filename is not equal to package name and/or
+            # destination directory is not the default:
+            DOWNLOADED_FILE=${LFS_PKG_DIR}/${SRC_FILENAME}.${FINAL_EXT}
+            DEST_FILE=${DEST_DIR}/${PACK}.${FINAL_EXT}
 
-    if [ -n "${TEST_INTEGRITY}" ]; then
+            if [ "${DEST_FILE}" != "${DOWNLOADED_FILE}" ]; then
+               mv ${DOWNLOADED_FILE} ${DEST_FILE}
+           fi
+        fi
+    else
+        set +e
         test_archive_integrity "${PACK}" "${DEST_FILE}" "${FINAL_EXT}"
+        set -e
+
+        # We do not want to fetch patches when testing integrity,
+        # so return immediately
+        return 0
     fi
 
     # Check for available patches with PACKAGE (TARGET) name.
@@ -531,43 +556,3 @@ fpkg()
         fi
     fi
 }
-
-# Create a symbolic link to a package that is located in another stage
-# repository (to not have the same file twice).
-# Arg. #1: Source stage number (1, 2, 3, etc)
-# Arg. #2: Package name
-lpkg()
-{
-    SRCSTAGE="stage${1}"
-    FILE="${2}.tar.bz2"
-    PACKAGE_NAME="${2}"
-
-    # Checking for correct number of arguments
-    if [ $# -ne 2 ]; then
-       echo "${FUNCNAME}(), wrong number of arguments: ${*}"
-       return 1
-    fi
-
-    if [ ! -h ${LFS_PKG_DIR}/${FILE} ]; then
-       # Issue a warning if source file doesn't exist.
-       if [ ! -f ${LFS_PKG_BASE}/${SRCSTAGE}/${FILE} ]; then
-            source_link_status=" (missing source file)"
-        else
-            source_link_status=""
-       fi
-
-       # Create link if it doesn't exist
-       rcmd "Linking ${PACKAGE_NAME}${source_link_status}" ln -s ../${SRCSTAGE}/${FILE} ${LFS_PKG_DIR}/${FILE}
-    fi
-
-    # Create link for patches corresponding to that package:
-    if ls ${LFS_PKG_BASE}/${SRCSTAGE}/${PACKAGE_NAME}-*.patch 1> /dev/null 2>&1; then
-       for patch in ${LFS_PKG_BASE}/${SRCSTAGE}/${PACKAGE_NAME}-*.patch; do
-           PATCHFILE=$(basename ${patch})
-           if [ ! -h ${LFS_PKG_DIR}/${PATCHFILE} ]; then
-               # Create link if it doesn't exist
-               rcmd "Linking ${PATCHFILE}" ln -s ../${SRCSTAGE}/${PATCHFILE} ${LFS_PKG_DIR}/${PATCHFILE}
-           fi
-       done
-    fi
-}