automorphism88 / snapraid-btrfs

Script for using snapraid with btrfs snapshots
GNU General Public License v3.0
138 stars 16 forks source link

A non "en_US.UTF-8" LC_ALL causes problems on cleanup. #7

Closed Suika closed 4 years ago

Suika commented 4 years ago

LANG=en_US.UTF-8 LC_ALL=de_DE.UTF-8

The LC_ALL causes problems when the language is not set to en_US, due to the script looking at column tab names.

It would brobably be better to enforce LC_ALL=en_US.UTF-8 to make sure that the script works in different environments. snapraid-btrfs cleanup

snapraid-btrfs: 883: awk -F '|' -v key="${1-}" -v value="${2-}" -v ORS="${3:-
}" -f <(cat <<'_EOF_'
# read column titles in header, so as to work with different versions of
# snapper that reorder columns
NR==1 {
    for (i=1;i<=NF;i++) {
        # remove padding spaces, then store column number indexed by title
        gsub(/[ ]+/,"",$i)
        column[$i] = i
    }
    # check to make sure we found columns labelled "#" and "Userdata"
    if (column["#"] == "" || column["Userdata"] == "") {
        printf("error: expected snapper ls column names not found\n",
               "/dev/stderr")
        exit 1
    }
}
# snapshot data begins on line 3
NR>=3 {
    # remove nonnumeric characters (padding spaces, mount status) from #
    gsub(/[^0123456789]+/,"",$column["#"])
    if (key == "") {
        # match all snapshots
        print $column["#"]
    } else {
        # split userdata column into key=value pairs in case
        # multiple userdata keys are defined for a snapshot
        split($column["Userdata"],u,",")
        # construct a new array v where the keys are the values from u
        for (i in u) {
            # remove padding spaces
            gsub(/^[ ]+/,"",u[i])
            gsub(/[ ]+$/,"",u[i])
            if (value == "") {
                # We don't care about the value of the userdata key, so
                # split key=value pairs and store only the key as a key in v
                split(u[i],w,"=")
                v[w[1]]
            } else {
                # We care about both halves of the userdata key=value
                # pair, so store the whole key=value string as a key in v
                v[u[i]]
            }
        }
        # find and print our matches
        if (value == "") {
            if (key in v) {
                print $column["#"]
            }
        } else {
            if (key "=" value in v) {
                print $column["#"]
            }
        }
        # Wipe v so one match doesn't result in matching all subsequent lines
        # delete v only works in gawk
        split("",v," ")
    }
}
_EOF_
            ) failed with exit status 1
Call stack:
/sbin/snapraid-btrfs: parse_snapper_ls: 883
/sbin/snapraid-btrfs: find_snapshots: 522
/sbin/snapraid-btrfs: setup_use_snapshot: 1165
/sbin/snapraid-btrfs: setup_config: 1142
/sbin/snapraid-btrfs: main: 834
/sbin/snapraid-btrfs: main: 1634
snapraid-btrfs: 523: tail -n 1 failed with exit status 1
Call stack:
/sbin/snapraid-btrfs: find_snapshots: 523
/sbin/snapraid-btrfs: setup_use_snapshot: 1165
/sbin/snapraid-btrfs: setup_config: 1142
/sbin/snapraid-btrfs: main: 834
/sbin/snapraid-btrfs: main: 1634
snapraid-btrfs: 521: use_snapshot[$i]="$(snapper_ls_wrapper "$i" |
                    parse_snapper_ls "$snapper_userdata_key" synced |
                    tail -n 1)" failed with exit status 1
Call stack:
/sbin/snapraid-btrfs: find_snapshots: 521
/sbin/snapraid-btrfs: setup_use_snapshot: 1165
/sbin/snapraid-btrfs: setup_config: 1142
/sbin/snapraid-btrfs: main: 834
/sbin/snapraid-btrfs: main: 1634
automorphism88 commented 4 years ago

Can you test if the commit I just submitted fixes the issue? I went ahead and just set LC_ALL=C instead of en_US-UTF-8.

automorphism88 commented 4 years ago

On second thought, it would be nice to only use LC_ALL=C for the commands whose output is being parsed by the script and allow commands whose output is being displayed to the user to run with the user's locale. I'll look at doing that.

automorphism88 commented 4 years ago

This commit should apply LC_ALL=C only when needed: https://github.com/automorphism88/snapraid-btrfs/commit/fb66a38fe622d7bb6e48d24b21d5d7fe4cf66c32

Suika commented 4 years ago

Checked against 55d877c, works.