sdcard: Fix failing re-attaching on Windows 50/32750/7
authorMunkyu Im <munkyu.im@samsung.com>
Tue, 23 Dec 2014 12:16:02 +0000 (21:16 +0900)
committermunkyu im <munkyu.im@samsung.com>
Wed, 24 Dec 2014 09:33:35 +0000 (01:33 -0800)
failed when try to attach > detach > attach on Windows.
becuase of failing unlock when detach sdcard image.

Change-Id: If400812633a4a22c937911cc3086125923e9a6fd
Signed-off-by: Munkyu Im <munkyu.im@samsung.com>
tizen/src/ecs/ecs_msg_injector.c
tizen/src/util/osutil-win32.c
tizen/src/util/osutil.h

index 84423fb..77ade75 100644 (file)
@@ -369,7 +369,7 @@ static int handle_sdcard(char* dataBuf, size_t dataLen)
             INFO("datalen: %d\n", dataLen);
             char* sdcard_img_path = get_sdcard_img_path(dataBuf + 2, dataLen);
             err_no = remove_sdcard_lock_os(sdcard_img_path);
-            if (errno == 0 && is_sdcard_attached()) {
+            if (err_no == 0 && is_sdcard_attached()) {
                 do_hotplug(DETACH_SDCARD, NULL, 0);
             } else {
                 ERR("failed to umount: %s\n", sdcard_img_path);
index 9dc28b1..40cf9b3 100644 (file)
@@ -57,7 +57,7 @@ static char *g_pBuf;
 
 extern char tizen_target_img_path[];
 static char g_sdcard[256] = {0,};
-
+static sdcard_info info;
 static const char *pactempfile = ".autoproxy";
 
 static void check_vm_lock_os(void)
@@ -399,16 +399,16 @@ bool make_sdcard_lock_os(char *sdcard)
     char *lock_file = g_strdup_printf("%s.lck", sdcard);
 
     HANDLE hFile = CreateFile(lock_file, GENERIC_READ,
-            0,
+            FILE_SHARE_READ | FILE_SHARE_DELETE,
             NULL,
             CREATE_ALWAYS,
-            FILE_ATTRIBUTE_NORMAL,
+            FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE,
             NULL);
 
     if (hFile == INVALID_HANDLE_VALUE) {
         ERR("file open error : (%d)\n", GetLastError());
-        if(GetLastError() == 32) {
-            if(strcmp(g_sdcard, sdcard) == 0) {
+        if (GetLastError() == ERROR_SHARING_VIOLATION) {
+            if (strcmp(g_sdcard, sdcard) == 0) {
                 INFO("try to mount same sdcard!\n");
             }
             return false;
@@ -426,56 +426,30 @@ int remove_sdcard_lock_os(char *sdcard)
 {
     char *lock_file = g_strdup_printf("%s.lck", sdcard);
     HANDLE hFile2;
-    BOOL fSuccess;
     hFile2 = CreateFile(lock_file, GENERIC_READ,
             0,
             NULL,
             OPEN_EXISTING,
             FILE_ATTRIBUTE_NORMAL,
             NULL);
-
+    /* to check if previous lock exists */
     if (hFile2 == INVALID_HANDLE_VALUE) {
-        ERR("CreateFile() error : (%d)\n", GetLastError());
-        if (GetLastError() == 32) {
+        INFO("CreateFile() return : (%d)\n", GetLastError());
+        if (GetLastError() == ERROR_SHARING_VIOLATION) {
             if (strncmp(g_sdcard, sdcard, strlen(sdcard)) != 0) {
                 INFO("not same sdcard!!!\n");
                 return ERR_UNLCK;
             }
 
-            INFO("return ERR_SUCCESS\n");
+            INFO("unlock success: %s\n", lock_file);
             g_sdcard[0] = '\0';
-            fSuccess = DeleteFile(lock_file);
-            if (!fSuccess) {
-                // Handle the error.
-                ERR("DeleteFile failed (%d)\n", GetLastError());
-                return ERR_UNLCK;
-            }
-            return ERR_SUCCESS;
+            CloseHandle(info.handle);
+        } else {
+            return ERR_UNLCK;
         }
-        return ERR_NOENT;
-    }
-
-    hFile2 = CreateFile(lock_file, GENERIC_READ,
-            0,
-            NULL,
-            CREATE_ALWAYS,
-            FILE_ATTRIBUTE_NORMAL,
-            NULL);
-
-    if (hFile2 == INVALID_HANDLE_VALUE) {
-        ERR("CreateFile() error : (%d)\n", GetLastError());
-        return ERR_UNLCK;
-    }
-    CloseHandle(hFile2);
-    fSuccess = DeleteFile(lock_file);
-    if (!fSuccess) {
-        // Handle the error.
-        ERR("DeleteFile failed (%d)\n", GetLastError());
-        return ERR_UNLCK;
+    } else {
+        ERR("lockfile exists but, it is not locked.\n");
+        CloseHandle(hFile2);
     }
-    INFO("unlock success: %s\n", lock_file);
-    CloseHandle(info.handle);
-
     return ERR_SUCCESS;
-
 }
index e75dc94..ed51849 100644 (file)
@@ -80,8 +80,6 @@ typedef struct sdcard_info
 #endif
     char* lock_file; /* reserved for future use */
 } sdcard_info;
-extern sdcard_info info;
-
 #ifndef CONFIG_WIN32
 bool make_sdcard_lock_posix(char *sdcard);
 int remove_sdcard_lock_posix(char *sdcard);