56
56
import java .io .File ;
57
57
import java .io .FileInputStream ;
58
58
import java .io .FileOutputStream ;
59
+ import java .io .FileWriter ;
59
60
import java .io .IOException ;
60
61
import java .io .InputStream ;
61
62
import java .math .BigDecimal ;
64
65
import java .util .ArrayList ;
65
66
import java .util .HashMap ;
66
67
import java .util .List ;
68
+ import java .util .Locale ;
67
69
import java .util .Map ;
68
70
import java .util .TimeZone ;
69
71
@@ -167,12 +169,17 @@ private static String getGnuCashRootAccountUID(SQLiteDatabase db){
167
169
* @throws IOException if an error occurred during the file copy
168
170
*/
169
171
static void moveFile (File src , File dst ) throws IOException {
172
+ Log .d (LOG_TAG , String .format (Locale .US , "Moving %s from %s to %s" ,
173
+ src .getName (), src .getParent (), dst .getParent ()));
170
174
FileChannel inChannel = new FileInputStream (src ).getChannel ();
171
175
FileChannel outChannel = new FileOutputStream (dst ).getChannel ();
172
176
try {
173
177
long bytesCopied = inChannel .transferTo (0 , inChannel .size (), outChannel );
174
- if (bytesCopied >= src .length ())
175
- src .delete ();
178
+ if (bytesCopied >= src .length ()) {
179
+ boolean result = src .delete ();
180
+ String msg = result ? "Deleted src file: " : "Could not delete src: " ;
181
+ Log .d (LOG_TAG , msg + src .getPath ());
182
+ }
176
183
} finally {
177
184
if (inChannel != null )
178
185
inChannel .close ();
@@ -194,7 +201,7 @@ public void run() {
194
201
for (File src : oldExportFolder .listFiles ()) {
195
202
if (src .isDirectory ())
196
203
continue ;
197
- File dst = new File (Exporter .BASE_FOLDER_PATH + "/exports/" + src .getName ());
204
+ File dst = new File (Exporter .LEGACY_BASE_FOLDER_PATH + "/exports/" + src .getName ());
198
205
try {
199
206
MigrationHelper .moveFile (src , dst );
200
207
} catch (IOException e ) {
@@ -210,7 +217,7 @@ public void run() {
210
217
File oldBackupFolder = new File (oldExportFolder , "backup" );
211
218
if (oldBackupFolder .exists ()){
212
219
for (File src : new File (oldExportFolder , "backup" ).listFiles ()) {
213
- File dst = new File (Exporter .BASE_FOLDER_PATH + "/backups/" + src .getName ());
220
+ File dst = new File (Exporter .LEGACY_BASE_FOLDER_PATH + "/backups/" + src .getName ());
214
221
try {
215
222
MigrationHelper .moveFile (src , dst );
216
223
} catch (IOException e ) {
@@ -490,8 +497,8 @@ static int upgradeDbToVersion7(SQLiteDatabase db) {
490
497
static int upgradeDbToVersion8 (SQLiteDatabase db ) {
491
498
Log .i (DatabaseHelper .LOG_TAG , "Upgrading database to version 8" );
492
499
int oldVersion = 7 ;
493
- new File (Exporter .BASE_FOLDER_PATH + "/backups/" ).mkdirs ();
494
- new File (Exporter .BASE_FOLDER_PATH + "/exports/" ).mkdirs ();
500
+ new File (Exporter .LEGACY_BASE_FOLDER_PATH + "/backups/" ).mkdirs ();
501
+ new File (Exporter .LEGACY_BASE_FOLDER_PATH + "/exports/" ).mkdirs ();
495
502
//start moving the files in background thread before we do the database stuff
496
503
new Thread (moveExportedFilesToNewDefaultLocation ).start ();
497
504
@@ -1474,4 +1481,75 @@ static int upgradeDbToVersion13(SQLiteDatabase db){
1474
1481
1475
1482
return oldVersion ;
1476
1483
}
1484
+
1485
+ /**
1486
+ * Move files from {@code srcDir} to {@code dstDir}
1487
+ * Subdirectories will be created in the target as necessary
1488
+ * @param srcDir Source directory which should already exist
1489
+ * @param dstDir Destination directory which should already exist
1490
+ * @see #moveFile(File, File)
1491
+ */
1492
+ private static void moveDirectory (File srcDir , File dstDir ){
1493
+ if (!srcDir .exists () || !srcDir .isDirectory () || !dstDir .isDirectory () || !dstDir .exists ()){
1494
+ throw new IllegalArgumentException ("Source is not a directory, use MigrationHelper.moveFile(...)" );
1495
+ }
1496
+
1497
+ for (File src : srcDir .listFiles ()){
1498
+ if (src .isDirectory ()){
1499
+ File dst = new File (dstDir , src .getName ());
1500
+ dst .mkdir ();
1501
+ moveDirectory (src , dst );
1502
+ if (!src .delete ())
1503
+ Log .i (LOG_TAG , "Failed to delete directory: " + src .getPath ());
1504
+ continue ;
1505
+ }
1506
+
1507
+ try {
1508
+ File dst = new File (dstDir , src .getName ());
1509
+ MigrationHelper .moveFile (src , dst );
1510
+ } catch (IOException e ) {
1511
+ Log .e (LOG_TAG , "Error moving file " + src .getPath ());
1512
+ Crashlytics .logException (e );
1513
+ }
1514
+ }
1515
+ }
1516
+
1517
+ /**
1518
+ * Upgrade the database to version 14
1519
+ * <p>
1520
+ * This migration actually does not change anything in the database
1521
+ * It moves the backup files to a new backup location which does not require SD CARD write permission
1522
+ * </p>
1523
+ * @param db SQLite database to be upgraded
1524
+ * @return
1525
+ */
1526
+ public static int upgradeDbToVersion14 (SQLiteDatabase db ){
1527
+ Log .i (DatabaseHelper .LOG_TAG , "Upgrading database to version 14" );
1528
+ int oldDbVersion = 13 ;
1529
+ File backupFolder = new File (Exporter .BASE_FOLDER_PATH );
1530
+ backupFolder .mkdir ();
1531
+
1532
+ new Thread (new Runnable () {
1533
+ @ Override
1534
+ public void run () {
1535
+ File srcDir = new File (Exporter .LEGACY_BASE_FOLDER_PATH );
1536
+ File dstDir = new File (Exporter .BASE_FOLDER_PATH );
1537
+ moveDirectory (srcDir , dstDir );
1538
+ File readmeFile = new File (Exporter .LEGACY_BASE_FOLDER_PATH , "README.txt" );
1539
+ FileWriter writer = null ;
1540
+ try {
1541
+ writer = new FileWriter (readmeFile );
1542
+ writer .write ("Backup files have been moved to " + dstDir .getPath () +
1543
+ "\n You can now delete this folder" );
1544
+ writer .flush ();
1545
+ } catch (IOException e ) {
1546
+ e .printStackTrace ();
1547
+ Log .e (LOG_TAG , "Error creating README file" );
1548
+ }
1549
+
1550
+ }
1551
+ }).start ();
1552
+
1553
+ return 14 ;
1554
+ }
1477
1555
}
0 commit comments