Bug 746444 - Add native function to force unlock on database file (r=blassey, a=akeybl)
authorLucas Rocha <lucasr@mozilla.com>
Tue, 24 Apr 2012 16:34:03 -0400
changeset 95534 80a502d60a87037cbdea7ea699d7f2d61edb74d2
parent 95533 2bc277479ad2d48493675cbc6ac917e0d409e8bf
child 95535 018f6b2913681b6663fae87a2e539cfd402f365e
push id886
push userlsblakk@mozilla.com
push dateMon, 04 Jun 2012 19:57:52 +0000
treeherdermozilla-beta@bbd8d5efd6d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersblassey, akeybl
bugs746444
milestone14.0a2
Bug 746444 - Add native function to force unlock on database file (r=blassey, a=akeybl)
mobile/android/base/GeckoAppShell.java
mozglue/android/nsGeckoUtils.cpp
--- a/mobile/android/base/GeckoAppShell.java
+++ b/mobile/android/base/GeckoAppShell.java
@@ -211,16 +211,18 @@ public class GeckoAppShell
     public static native void notifyReadingMessageListFailed(int aError, int aRequestId, long aProcessId);
 
     public static native ByteBuffer allocateDirectBuffer(long size);
     public static native void freeDirectBuffer(ByteBuffer buf);
     public static native void scheduleComposite();
     public static native void schedulePauseComposition();
     public static native void scheduleResumeComposition(int width, int height);
 
+    public static native void unlockDatabaseFile(String databasePath);
+
     private static class GeckoMediaScannerClient implements MediaScannerConnectionClient {
         private String mFile = "";
         private String mMimeType = "";
         private MediaScannerConnection mScanner = null;
 
         public GeckoMediaScannerClient(Context aContext, String aFile, String aMimeType) {
             mFile = aFile;
             mMimeType = aMimeType;
--- a/mozglue/android/nsGeckoUtils.cpp
+++ b/mozglue/android/nsGeckoUtils.cpp
@@ -39,17 +39,17 @@
 
 #ifdef MOZ_MEMORY
 // Wrap malloc and free to use jemalloc
 #define malloc __wrap_malloc
 #define free __wrap_free
 #endif
 
 #include <stdlib.h>
-
+#include <fcntl.h>
 
 extern "C"
 __attribute__ ((visibility("default")))
 void JNICALL
 Java_org_mozilla_gecko_GeckoAppShell_putenv(JNIEnv *jenv, jclass, jstring map)
 {
     const char* str;
     // XXX: java doesn't give us true UTF8, we should figure out something 
@@ -72,8 +72,39 @@ Java_org_mozilla_gecko_GeckoAppShell_all
 extern "C"
 __attribute__ ((visibility("default")))
 void JNICALL
 Java_org_mozilla_gecko_GeckoAppShell_freeDirectBuffer(JNIEnv *jenv, jclass, jobject buf)
 {
     free(jenv->GetDirectBufferAddress(buf));
 }
 
+extern "C"
+__attribute__ ((visibility("default")))
+void JNICALL
+Java_org_mozilla_gecko_GeckoAppShell_unlockDatabaseFile(JNIEnv *jenv, jclass, jstring jDatabasePath)
+{
+    const char *databasePath = jenv->GetStringUTFChars(jDatabasePath, NULL);
+    int fd = open(databasePath, O_RDWR);
+    jenv->ReleaseStringUTFChars(jDatabasePath, databasePath);
+
+    // File could not be open, do nothing
+    if (fd < 0) {
+        return;
+    }
+
+    struct flock lock;
+
+    lock.l_type = F_WRLCK;
+    lock.l_whence = SEEK_SET;
+    lock.l_start = 0;
+    lock.l_len = 0;
+
+    int result = fcntl(fd, F_GETLK, &lock);
+
+    // Release any existing lock in the file
+    if (result != -1 && lock.l_type == F_WRLCK) {
+        lock.l_type = F_UNLCK;
+        fcntl(fd, F_SETLK, &lock);
+    }
+
+    close(fd);
+}
\ No newline at end of file