0
|
1 #!/bin/bash
|
|
2
|
|
3 # A script to find missing files in the f-spot database, and then delete them.
|
|
4 # At present these files crash f-spot. It's frustrating as all hell.
|
|
5
|
|
6 echo "Welcome to the f-spot database cleaner. All the usual disclaimers apply, as you might imagine."
|
|
7 echo "What would you like to do: "
|
|
8 echo " 1) Run in demo-mode "
|
|
9 echo " 2) Clean up your f-spot database"
|
|
10 echo " 3) Quit"
|
|
11 read -p "Your choice: " choice
|
|
12
|
|
13 case $choice in
|
|
14 1) demomode="true";;
|
|
15 2) demomode="false";;
|
|
16 3) exit 0;;
|
|
17 esac
|
|
18
|
|
19 # With that beginning stuff out of the way, let us do some functions
|
|
20 # First, a function to gather the database contents and to print out the ones that are orphans
|
|
21
|
|
22 function findAndFixOrphans {
|
|
23 # find our db, and set a var. Checking for XDG path first, since it's the more recent location of the db
|
|
24 if [ -f $XDG_CONFIG_DIR/f-spot/photos.db ] #checks if the $XDG_CONFIG_DIR variable is in use
|
|
25 then
|
|
26 DBPATH=$XDG_CONFIG_DIR/f-spot/photos.db
|
|
27 elif [ -f $HOME/.config/f-spot/photos.db ] #uses the default $XDG location, if that's being used.
|
|
28 then
|
|
29 DBPATH=$HOME/.config/f-spot/photos.db
|
|
30 elif [ -f $HOME/.gnome2/f-spot/photos.db ] #uses the old location of the DB, if the former aren't in use.
|
|
31 then
|
|
32 DBPATH=$HOME/.gnome2/f-spot/photos.db
|
|
33 else
|
|
34 echo "Error: Could not find database. Damn."
|
|
35 exit 1
|
|
36 fi
|
|
37
|
|
38 # Select the filenames, and put them in a variable.
|
|
39 filenames=$(sqlite3 $DBPATH "SELECT base_uri || '/' || filename FROM PHOTOS")
|
|
40 filenames_versions=$(sqlite3 $DBPATH "SELECT base_uri || '/' || filename FROM PHOTO_VERSIONS")
|
|
41
|
|
42 # Chomp off the first instance of file://, and replace the rest with newlines.
|
|
43 filenames=$(echo $filenames | sed 's/file:\/\///' | sed 's/file:\/\//\n/g' )
|
|
44 filenames_versions=$(echo $filenames_versions | sed 's/file:\/\///' | sed 's/file:\/\//\n/g' )
|
|
45
|
|
46 if [ $demomode == "true" ]
|
|
47 then
|
|
48 while read -r line
|
|
49 do
|
|
50 # Decode the filename
|
|
51 decodedLine=$(echo -e "${line//\%/\\x}")
|
|
52 if [ ! -f "$decodedLine" ]
|
|
53 then
|
|
54 # If the file doesn't exist, we output the filename, if in demomode, or we fix it if we are not in demomode.
|
|
55 echo "Errant record found in the photos table: $decodedLine"
|
|
56 fi
|
|
57 done <<< "$filenames"
|
|
58
|
|
59 # We do the same for the photo_versions table
|
|
60 while read -r line
|
|
61 do
|
|
62 # Decode filename
|
|
63 decodedLine=$(echo -e "${line//\%/\\x}")
|
|
64 if [ ! -f "$decodedLine" ]
|
|
65 then
|
|
66 # If the file doesn't exist, we output the filename
|
|
67 echo "Errant record found in the photo_versions table: $decodedLine"
|
|
68 fi
|
|
69 done <<< "$filenames_versions"
|
|
70
|
|
71 else
|
|
72 # We backup the database, and make the correction
|
|
73 cp $DBPATH $DBPATH.`date -I`.bak
|
|
74 if [ $? -eq 0 ]
|
|
75 then
|
|
76 #The backup worked, tell the user.
|
|
77 echo "Your database has been backed up to $DBPATH.`date -I`.bak"
|
|
78 else
|
|
79 echo "Error backing up your database."
|
|
80 exit 3
|
|
81 fi
|
|
82
|
|
83 # First we do the photos table
|
|
84 while read -r line
|
|
85 do
|
|
86 # Decode the filename
|
|
87 decodedLine=$(echo -e "${line//\%/\\x}")
|
|
88
|
|
89 if [ ! -f "$decodedLine" ]
|
|
90 then
|
|
91 # Do some sql here.
|
|
92 foo="file://${line}"
|
|
93 base_uri=${foo%/*}
|
|
94 filename=${foo##*/}
|
|
95 echo -n "Deleting URI $line from the database table photos..."
|
|
96 sqlite3 $DBPATH "DELETE FROM PHOTOS WHERE base_uri = '$base_uri' AND filename = '$filename'"
|
|
97 echo "done."
|
|
98 fi
|
|
99 done <<< "$filenames"
|
|
100
|
|
101 # Then we do the photo_versions table
|
|
102 while read -r line
|
|
103 do
|
|
104 # Decode the filename
|
|
105 decodedLine=$(echo -e "${line//\%/\\x}")
|
|
106
|
|
107 if [ ! -f "$decodedLine" ]
|
|
108 then
|
|
109 #Do some sql stuff
|
|
110 foo="file://${line}"
|
|
111 base_uri=${foo%/*}
|
|
112 filename=${foo##*/}
|
|
113 echo -n "Deleting URI $line from the database table photo_versions..."
|
|
114 sqlite3 $DBPATH "DELETE FROM PHOTO_VERSIONS WHERE base_uri = '$base_uri' AND filename = '$filename'"
|
|
115 echo "done."
|
|
116 fi
|
|
117 done <<< "$filenames_versions"
|
|
118
|
|
119 fi
|
|
120
|
|
121 }
|
|
122
|
|
123 if [ "$demomode" == "true" ]
|
|
124 then
|
|
125 echo "Great. Proceeding in demomode."
|
|
126
|
|
127 findAndFixOrphans
|
|
128
|
|
129 echo "Demomode successfully finished. Exiting."
|
|
130 exit 0;
|
|
131 elif [ "$demomode" == "false" ]
|
|
132 then
|
|
133 echo "Great. Cleaning up your database."
|
|
134
|
|
135 findAndFixOrphans
|
|
136
|
|
137 echo "Database cleaned successfully."
|
|
138 exit 0;
|
|
139 else
|
|
140 echo "Something strange happened. See the script for details. Exiting."
|
|
141 exit 2;
|
|
142 fi
|