@@ -894,6 +894,20 @@ public boolean exists() {
894894 */
895895 @ InterfaceAudience .Private
896896 public String getAttachmentStorePath () {
897+ String attachmentStorePath = path ;
898+ int lastDotPosition = attachmentStorePath .lastIndexOf ('.' );
899+ if ( lastDotPosition > 0 ) {
900+ attachmentStorePath = attachmentStorePath .substring (0 , lastDotPosition );
901+ }
902+ attachmentStorePath = attachmentStorePath + " attachments" ;
903+ return attachmentStorePath ;
904+ }
905+
906+ /**
907+ * @exclude
908+ */
909+ @ InterfaceAudience .Private
910+ private String getObsoletedAttachmentStorePath () {
897911 String attachmentStorePath = path ;
898912 int lastDotPosition = attachmentStorePath .lastIndexOf ('.' );
899913 if ( lastDotPosition > 0 ) {
@@ -903,6 +917,19 @@ public String getAttachmentStorePath() {
903917 return attachmentStorePath ;
904918 }
905919
920+ /**
921+ * @exclude
922+ */
923+ @ InterfaceAudience .Private
924+ private String getObsoletedAttachmentStoreParentPath () {
925+ String attachmentStorePath = path ;
926+ int lastDotPosition = attachmentStorePath .lastIndexOf ('.' );
927+ if ( lastDotPosition > 0 ) {
928+ attachmentStorePath = attachmentStorePath .substring (0 , lastDotPosition );
929+ }
930+ return attachmentStorePath ;
931+ }
932+
906933 /**
907934 * @exclude
908935 */
@@ -1187,6 +1214,28 @@ public synchronized boolean open() {
11871214 optimizeSQLIndexes (); // runs ANALYZE query
11881215 }
11891216
1217+ // NOTE: Migrate attachment directory path if necessary
1218+ // https://github.com/couchbase/couchbase-lite-java-core/issues/604
1219+ File obsoletedAttachmentStorePath = new File (getObsoletedAttachmentStorePath ());
1220+ if (obsoletedAttachmentStorePath != null && obsoletedAttachmentStorePath .exists () && obsoletedAttachmentStorePath .isDirectory ()) {
1221+ File attachmentStorePath = new File (getAttachmentStorePath ());
1222+ if (attachmentStorePath != null && !attachmentStorePath .exists ()) {
1223+ boolean success = obsoletedAttachmentStorePath .renameTo (attachmentStorePath );
1224+ if (!success ) {
1225+ Log .e (Database .TAG , "Could not rename attachment store path" );
1226+ database .close ();
1227+ return false ;
1228+ }
1229+ }
1230+
1231+ }
1232+ // NOTE: obsoleted directory is /files/<database name>/attachments/xxxx
1233+ // Needs to delete /files/<database name>/ too
1234+ File obsoletedAttachmentStoreParentPath = new File (getObsoletedAttachmentStoreParentPath ());
1235+ if (obsoletedAttachmentStoreParentPath != null && obsoletedAttachmentStoreParentPath .exists ()) {
1236+ obsoletedAttachmentStoreParentPath .delete ();
1237+ }
1238+
11901239 try {
11911240 if (isBlobstoreMigrated () || !manager .isAutoMigrateBlobStoreFilename ()){
11921241 attachments = new BlobStore (getAttachmentStorePath (), false );
0 commit comments