Skip to content

Commit c451bcd

Browse files
committed
Merge pull request #605 from couchbase/feature/issue_604_incompatible_attachment_path
Fixed #604 - incompatible attachment path
2 parents e45b65e + 5a1c007 commit c451bcd

3 files changed

Lines changed: 61 additions & 4 deletions

File tree

src/main/java/com/couchbase/lite/BlobStore.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ public BlobStore(String path, boolean autoMigrate) {
5656
this.path = path;
5757
File directory = new File(path);
5858

59-
directory.mkdirs();
59+
if (!directory.exists()) {
60+
directory.mkdirs();
61+
}
6062
if (!directory.isDirectory()) {
6163
throw new IllegalStateException(String.format("Unable to create directory for: %s", directory));
6264
}
@@ -375,7 +377,9 @@ public File tempDir() {
375377
File directory = new File(path);
376378
File tempDirectory = new File(directory, "temp_attachments");
377379

378-
tempDirectory.mkdirs();
380+
if (!tempDirectory.exists()) {
381+
tempDirectory.mkdirs();
382+
}
379383
if (!tempDirectory.isDirectory()) {
380384
throw new IllegalStateException(String.format("Unable to create directory for: %s", tempDirectory));
381385
}

src/main/java/com/couchbase/lite/Database.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

src/main/java/com/couchbase/lite/Manager.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ public Manager(Context context, ManagerOptions options) throws IOException {
130130
this.databases = new HashMap<String, Database>();
131131
this.replications = new ArrayList<Replication>();
132132

133-
directoryFile.mkdirs();
133+
if (!directoryFile.exists()) {
134+
directoryFile.mkdirs();
135+
}
134136
if (!directoryFile.isDirectory()) {
135137
throw new IOException(String.format("Unable to create directory for: %s", directoryFile));
136138
}
@@ -289,7 +291,9 @@ private void replaceDatabase(String databaseName, InputStream databaseStream, It
289291
StreamUtils.copyStream(databaseStream, destStream);
290292
File attachmentsFile = new File(dstAttachmentsPath);
291293
FileDirUtils.deleteRecursive(attachmentsFile);
292-
attachmentsFile.mkdirs();
294+
if (!attachmentsFile.exists()) {
295+
attachmentsFile.mkdirs();
296+
}
293297
if (attachmentStreams != null) {
294298
StreamUtils.copyStreamsToFolder(attachmentStreams, attachmentsFile);
295299
}

0 commit comments

Comments
 (0)