Skip to content

Commit 511b5b2

Browse files
authored
Improve solvable_files.sh from Nextcloud GmbH (#2487)
1 parent 2e02b21 commit 511b5b2

File tree

2 files changed

+101
-5
lines changed

2 files changed

+101
-5
lines changed

addons/fix_invalid_modification_time.sh

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/bin/bash
22

33
# T&M Hansson IT AB © - 2023, https://www.hanssonit.se/
4+
# Based on: https://raw.githubusercontent.com/nextcloud-gmbh/mtime_fixer_tool_kit/master/solvable_files.sh
45

56
true
67
SCRIPT_NAME="Fix 'Could not update metadata due to invalid modified time'."
@@ -24,9 +25,6 @@ msg_box "OK, let's go!
2425
2526
Please note, this script might take several hours to run, depening on the size of your datadir. Don't abort it!"
2627

27-
# Download the script
28-
curl_to_dir https://raw.githubusercontent.com/nextcloud-gmbh/mtime_fixer_tool_kit/master solvable_files.sh $NCPATH
29-
3028
# Run all the needed variables
3129
ncdb
3230

@@ -38,8 +36,7 @@ fi
3836

3937
# Run the script and remove it
4038
print_text_in_color "$ICyan" "Running the scan and fixing broken files..."
41-
bash "$NCPATH"/solvable_files.sh "$NCDATA" "$NCDBTYPE" "$NCDBHOST" "$NCDBUSER" "$NCDBPASS" "$NCDB" fix use_birthday verbose
42-
rm "$NCPATH"/solvable_files.sh
39+
run_script ADDONS solvable_files
4340

4441
# Scan all files
4542
nextcloud_occ files:scan --all

addons/solvable_files.sh

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/bin/bash
2+
3+
# Based on: https://raw.githubusercontent.com/nextcloud-gmbh/mtime_fixer_tool_kit/master/solvable_files.sh
4+
5+
true
6+
SCRIPT_NAME="Fix 'Could not update metadata due to invalid modified time'."
7+
# shellcheck source=lib.sh
8+
source /var/scripts/fetch_lib.sh
9+
10+
# Get needed variables for database management
11+
ncdb
12+
13+
# Check if root
14+
root_check
15+
16+
#2023-05-04 Customized the original script to fit the Nextcloud VM users setup. Also fixed some shellcheck issues.
17+
18+
data_dir="$(realpath "$NCDATA")"
19+
export data_dir
20+
export db_type=$NCDBTYPE
21+
export db_host=$NCDBHOST
22+
export db_user=$NCDBUSER
23+
export db_pwd=$NCDBPASS
24+
export db_name=$NCDB
25+
export action=fix
26+
export scan_action=noscan
27+
export use_birthday=use_birthday
28+
export verbose=verbose
29+
30+
# In case you're using a different database table prefix, set this to your config's `dbtableprefix` value.
31+
export dbtableprefix="oc_"
32+
33+
# 1. Return if fs mtime <= 86400
34+
# 2. Compute username from filepath
35+
# 3. Query mtime from the database with the filename and the username
36+
# 4. Return if mtime_on_fs != mtime_in_db
37+
# 5. Correct the fs mtime with touch (optionally using the files change date/timestamp)
38+
correct_mtime() {
39+
filepath=$NCDATA
40+
41+
if [ ! -e "$filepath" ]
42+
then
43+
echo "File or directory $filepath does not exist. Skipping."
44+
return
45+
fi
46+
47+
relative_filepath="${filepath/#$data_dir\//}"
48+
mtime_on_fs="$(stat -c '%Y' "$filepath")"
49+
50+
username=$relative_filepath
51+
while [ "$(dirname "$username")" != "." ]
52+
do
53+
username=$(dirname "$username")
54+
done
55+
56+
relative_filepath_without_username="${relative_filepath/#$username\//}"
57+
58+
base64_relative_filepath="$(printf '%s' "$relative_filepath" | base64)"
59+
base64_relative_filepath_without_username="$(printf '%s' "$relative_filepath_without_username" | base64)"
60+
61+
if [ "$username" == "__groupfolders" ]
62+
then
63+
mtime_in_db=$(sudo -u postgres psql nextcloud_db --tuples-only --no-align -c "SELECT mtime FROM ${dbtableprefix}storages JOIN ${dbtableprefix}filecache ON ${dbtableprefix}storages.numeric_id = ${dbtableprefix}filecache.storage WHERE ${dbtableprefix}storages.id='local::$data_dir/' AND ${dbtableprefix}filecache.path=CONVERT_FROM(DECODE('$base64_relative_filepath', 'base64'), 'UTF-8')")
64+
else
65+
mtime_in_db=$(sudo -u postgres psql nextcloud_db --tuples-only --no-align -c "SELECT mtime FROM ${dbtableprefix}storages JOIN ${dbtableprefix}filecache ON ${dbtableprefix}storages.numeric_id = ${dbtableprefix}filecache.storage WHERE ${dbtableprefix}storages.id='home::$username' AND ${dbtableprefix}filecache.path=CONVERT_FROM(DECODE('$base64_relative_filepath_without_username', 'base64'), 'UTF-8')")
66+
fi
67+
68+
if [ "$mtime_in_db" == "" ]
69+
then
70+
echo "No mtime in database. File not indexed. Skipping $filepath"
71+
return
72+
fi
73+
74+
if [ "$mtime_in_db" != "$mtime_on_fs" ]
75+
then
76+
echo "mtime in database do not match fs mtime (fs: $mtime_on_fs, db: $mtime_in_db). Skipping $filepath"
77+
return
78+
fi
79+
80+
if [ -e "$filepath" ]
81+
then
82+
newdate=$(stat -c "%w" "$filepath")
83+
if [ "$newdate" == "-" ]
84+
then
85+
newdate=$(stat -c "%z" "$filepath")
86+
touch -c -d "$newdate" "$filepath"
87+
else
88+
touch -c "$filepath"
89+
fi
90+
echo mtime for "$filepath" updated to "$(stat -c "%y" "$filepath")"
91+
elif [ ! -e "$filepath" ]
92+
then
93+
echo "File or directory $filepath does not exist. Skipping."
94+
return
95+
fi
96+
}
97+
export -f correct_mtime
98+
99+
find "$data_dir" -type f ! -newermt "@86400" -exec bash -c 'correct_mtime "$0"' {} \;

0 commit comments

Comments
 (0)