Skip to content

Fixed ICU extraction for multiple processes. #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 130 additions & 19 deletions src/net/sqlcipher/database/SQLiteDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
import net.sqlcipher.DatabaseUtils;
import net.sqlcipher.SQLException;
import net.sqlcipher.database.SQLiteDebug.DbStats;
import net.sqlcipher.database.SQLiteDatabaseHook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand All @@ -52,6 +50,12 @@
import android.util.Log;
import android.util.Pair;

import java.io.Closeable;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

/**
* Exposes methods to manage a SQLite database.
* <p>SQLiteDatabase has methods to create, delete, execute SQL commands, and
Expand All @@ -71,7 +75,17 @@ public class SQLiteDatabase extends SQLiteClosable {
private static final String TAG = "Database";
private static final int EVENT_DB_OPERATION = 52000;
private static final int EVENT_DB_CORRUPT = 75004;
private static FileLock icuFileLock = null;

static {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
icuLockRelease();
}
});
}

public int status(int operation, boolean reset){
return native_status(operation, reset);
}
Expand All @@ -83,29 +97,127 @@ public void changePassword(String password) throws SQLiteException {
public void changePassword(char[] password) throws SQLiteException {
native_rekey(password);
}

private static void loadICUData(Context context, File workingDir) {

private static boolean icuLockAcquire(final File lockDir, final long waitMS) {
if (icuFileLock != null || waitMS <= 0) {
return icuFileLock != null;
}

try {
final long deadline = System.currentTimeMillis() + waitMS;
final File file = new File(lockDir, "icu.lock");
final RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
final FileChannel fileChannel = randomAccessFile.getChannel();
while (System.currentTimeMillis() < deadline) {
try {
icuFileLock = fileChannel.tryLock();
if (icuFileLock != null) {
break;
}
} catch(Exception ex){
icuFileLock = null;
}
Thread.sleep(250);
}
} catch (Exception e) {
Log.e(TAG, "Exception during lock acquire", e);
}

return icuFileLock != null;
}

private static void icuLockRelease() {
if (icuFileLock == null) {
return;
}

try {
icuFileLock.release();
icuFileLock = null;
} catch (Exception e) {
Log.e(TAG, "Exception during lock release", e);
}
}

private static void closeSilently(Closeable cl){
if (cl==null){
return;
}
try {
cl.close();
} catch(Exception ex){

}
}

private static void loadICUData(Context context, File workingDir) {
try {
File icuDir = new File(workingDir, "icu");
if(!icuDir.exists()) icuDir.mkdirs();
File icuDataFile = new File(icuDir, "icudt46l.dat");
if(!icuDataFile.exists()) {
ZipInputStream in = new ZipInputStream(context.getAssets().open("icudt46l.zip"));

File icuDataFile = new File(icuDir, "icudt46l.dat");
if (!icuLockAcquire(icuDir, 7000)){
Log.w(TAG, "Could not acquire ICU extraction lock");
// Continue anyway, there may be a problem with file locking.
// 7 seconds is long enough.
}

if (icuDataFile.exists()){
return;
}

File tmpIcuFile = null;
ZipInputStream in = null;
FileOutputStream out = null;
FileChannel c = null;
try {
tmpIcuFile = File.createTempFile("icudt46l.dat", ".tmp", icuDir);
in = new ZipInputStream(context.getAssets().open("icudt46l.zip"));
in.getNextEntry();
OutputStream out = new FileOutputStream(icuDataFile);

out = new FileOutputStream(tmpIcuFile);
c = out.getChannel();

byte[] buf = new byte[1024];
ByteBuffer bbuf = ByteBuffer.allocate(1024);

int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
bbuf.put(buf, 0, len);
bbuf.flip();
c.write(bbuf);
bbuf.clear();
}
in.close();
out.flush();

closeSilently(in);
c.force(true);
out.getFD().sync();

c.close();
c = null;

out.close();
out = null;

// Already exists?
// Another process might create it in the background.
if (!icuDataFile.exists()){
// Move to final destination.
tmpIcuFile.renameTo(icuDataFile);
//Log.v(TAG, String.format("Extraction complete; file=%s, exists=%s", icuDataFile.getAbsoluteFile(), icuDataFile.exists()));
} else {
tmpIcuFile.delete();
}
} catch(Exception ex){
Log.e(TAG, "Exception during ICU extraction", ex);
} finally {
closeSilently(c);
closeSilently(out);
}
}
catch (Exception e) {
} catch (Exception e) {
Log.e(TAG, "Error copying icu data file", e);
} finally {
icuLockRelease();
}
}

Expand All @@ -115,17 +227,16 @@ public static void loadLibs (Context context) {

public static void loadLibs (Context context, File workingDir)
{
System.loadLibrary("stlport_shared");
System.loadLibrary("sqlcipher_android");
System.loadLibrary("database_sqlcipher");

boolean systemICUFileExists = new File("/system/usr/icu/icudt46l.dat").exists();

String icuRootPath = systemICUFileExists ? "/system/usr" : workingDir.getAbsolutePath();
setICURoot(icuRootPath);
if(!systemICUFileExists){
loadICUData(context, workingDir);
}

System.loadLibrary("stlport_shared");
System.loadLibrary("sqlcipher_android");
System.loadLibrary("database_sqlcipher");
setICURoot(icuRootPath);
}

/**
Expand Down