Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/bash
0002 # SPDX-License-Identifier: GPL-2.0
0003 #
0004 # Run filesystem operations tests on an 1 MiB disk image that is formatted with
0005 # a vfat filesystem and mounted in a temporary directory using a loop device.
0006 #
0007 # Copyright 2022 Red Hat Inc.
0008 # Author: Javier Martinez Canillas <javierm@redhat.com>
0009 
0010 set -e
0011 set -u
0012 set -o pipefail
0013 
0014 BASE_DIR="$(dirname $0)"
0015 TMP_DIR="$(mktemp -d /tmp/fat_tests_tmp.XXXX)"
0016 IMG_PATH="${TMP_DIR}/fat.img"
0017 MNT_PATH="${TMP_DIR}/mnt"
0018 
0019 cleanup()
0020 {
0021     mountpoint -q "${MNT_PATH}" && unmount_image
0022     rm -rf "${TMP_DIR}"
0023 }
0024 trap cleanup SIGINT SIGTERM EXIT
0025 
0026 create_loopback()
0027 {
0028     touch "${IMG_PATH}"
0029     chattr +C "${IMG_PATH}" >/dev/null 2>&1 || true
0030 
0031     truncate -s 1M "${IMG_PATH}"
0032     mkfs.vfat "${IMG_PATH}" >/dev/null 2>&1
0033 }
0034 
0035 mount_image()
0036 {
0037     mkdir -p "${MNT_PATH}"
0038     sudo mount -o loop "${IMG_PATH}" "${MNT_PATH}"
0039 }
0040 
0041 rename_exchange_test()
0042 {
0043     local rename_exchange="${BASE_DIR}/rename_exchange"
0044     local old_path="${MNT_PATH}/old_file"
0045     local new_path="${MNT_PATH}/new_file"
0046 
0047     echo old | sudo tee "${old_path}" >/dev/null 2>&1
0048     echo new | sudo tee "${new_path}" >/dev/null 2>&1
0049     sudo "${rename_exchange}" "${old_path}" "${new_path}" >/dev/null 2>&1
0050     sudo sync -f "${MNT_PATH}"
0051     grep new "${old_path}" >/dev/null 2>&1
0052     grep old "${new_path}" >/dev/null 2>&1
0053 }
0054 
0055 rename_exchange_subdir_test()
0056 {
0057     local rename_exchange="${BASE_DIR}/rename_exchange"
0058     local dir_path="${MNT_PATH}/subdir"
0059     local old_path="${MNT_PATH}/old_file"
0060     local new_path="${dir_path}/new_file"
0061 
0062     sudo mkdir -p "${dir_path}"
0063     echo old | sudo tee "${old_path}" >/dev/null 2>&1
0064     echo new | sudo tee "${new_path}" >/dev/null 2>&1
0065     sudo "${rename_exchange}" "${old_path}" "${new_path}" >/dev/null 2>&1
0066     sudo sync -f "${MNT_PATH}"
0067     grep new "${old_path}" >/dev/null 2>&1
0068     grep old "${new_path}" >/dev/null 2>&1
0069 }
0070 
0071 unmount_image()
0072 {
0073     sudo umount "${MNT_PATH}" &> /dev/null
0074 }
0075 
0076 create_loopback
0077 mount_image
0078 rename_exchange_test
0079 rename_exchange_subdir_test
0080 unmount_image
0081 
0082 exit 0