5656import java .io .File ;
5757import java .io .FileInputStream ;
5858import java .io .FileOutputStream ;
59+ import java .io .FileWriter ;
5960import java .io .IOException ;
6061import java .io .InputStream ;
6162import java .math .BigDecimal ;
6465import java .util .ArrayList ;
6566import java .util .HashMap ;
6667import java .util .List ;
68+ import java .util .Locale ;
6769import java .util .Map ;
6870import java .util .TimeZone ;
6971
@@ -167,12 +169,17 @@ private static String getGnuCashRootAccountUID(SQLiteDatabase db){
167169 * @throws IOException if an error occurred during the file copy
168170 */
169171 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 ()));
170174 FileChannel inChannel = new FileInputStream (src ).getChannel ();
171175 FileChannel outChannel = new FileOutputStream (dst ).getChannel ();
172176 try {
173177 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+ }
176183 } finally {
177184 if (inChannel != null )
178185 inChannel .close ();
@@ -194,7 +201,7 @@ public void run() {
194201 for (File src : oldExportFolder .listFiles ()) {
195202 if (src .isDirectory ())
196203 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 ());
198205 try {
199206 MigrationHelper .moveFile (src , dst );
200207 } catch (IOException e ) {
@@ -210,7 +217,7 @@ public void run() {
210217 File oldBackupFolder = new File (oldExportFolder , "backup" );
211218 if (oldBackupFolder .exists ()){
212219 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 ());
214221 try {
215222 MigrationHelper .moveFile (src , dst );
216223 } catch (IOException e ) {
@@ -490,8 +497,8 @@ static int upgradeDbToVersion7(SQLiteDatabase db) {
490497 static int upgradeDbToVersion8 (SQLiteDatabase db ) {
491498 Log .i (DatabaseHelper .LOG_TAG , "Upgrading database to version 8" );
492499 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 ();
495502 //start moving the files in background thread before we do the database stuff
496503 new Thread (moveExportedFilesToNewDefaultLocation ).start ();
497504
@@ -1474,4 +1481,75 @@ static int upgradeDbToVersion13(SQLiteDatabase db){
14741481
14751482 return oldVersion ;
14761483 }
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+ }
14771555}
0 commit comments