Get the device node dynamically 83/14083/1
authorHyunbin Lee <hyunbin.lee@samsung.com>
Thu, 28 Nov 2013 03:54:56 +0000 (12:54 +0900)
committerHyunbin Lee <hyunbin.lee@samsung.com>
Mon, 23 Dec 2013 06:45:57 +0000 (15:45 +0900)
Change-Id: Ie867a2b99f415449d07633643e7c51a933410e97
Signed-off-by: Hyunbin Lee <hyunbin.lee@samsung.com>
inc/FIo_MmcStorageManagerService.h
src/FIo_MmcStorageManagerService.cpp

index 8f3576e..ef7b497 100644 (file)
@@ -79,12 +79,12 @@ private:
 
        static result CreateExternalDirectories(const char srcPath[][_MAX_PATH_LENGTH], const char destPath[][_MAX_PATH_LENGTH]);
 
-       static int GetMmcblockNumber(void);
-
-       static result FindDeviceNode(const char *pPath, char* pBuf, int bufLen);
+       static result FindDeviceNode(const char* pPath, char* pBuf, int bufLen);
 
        static result ChangeMountMode(unsigned long mountFlags);
 
+       static result MountExternalPath(const char* pDestPath);
+
 private:
        static _MmcStorageManagerService* __pMmcStorageManagerService;
        _IMmcStorageManagerServiceListener* __pStub;
index 4ea9407..daa5f01 100644 (file)
@@ -270,25 +270,17 @@ _MmcStorageManagerService::MmcEventVconfCallback(keynode_t* node, void* userData
                                                        SysTryReturnVoidResult(NID_IO, !IsFailed(r), E_SYSTEM, "[%s] Failed to create external directories.",
                                                                        GetErrorMessage(r));
 
-                                                       r = ChangeMountMode(MS_PRIVATE);
-                                                       SysTryCatch(NID_IO, !IsFailed(r), , E_SYSTEM, "[E_SYSTEM] Changing to private mount mode is failed.");
+                                                       r = _MmcStorageManagerService::ChangeMountMode(MS_PRIVATE);
+                                                       SysTryCatch(NID_IO, !IsFailed(r), , E_SYSTEM, "[E_SYSTEM] Changing to private mount mode failed.");
 
                                                        for (int i = 1; i < _MAX_EXT_DIR_MOUNT_COUNT; ++i)
                                                        {
                                                                if (i == 1)
                                                                {
                                                                        SysLog(NID_IO, "destPath[%d]: %s", i, destPath[i]);
-#ifdef _OSP_EMUL_
-                                                                       int mmcblkNum = GetMmcblockNumber();
-                                                                       SysTryLog(NID_IO, mmcblkNum >= 0, "Failed to get mmcblk number");
-                                                                       char devPath[_MAX_PATH_LENGTH] = { 0, };
-                                                                       sprintf(devPath, "/dev/mmcblk%d", mmcblkNum);
-                                                                       res = mount(devPath, destPath[i], "ext4", 0, "");
-#else
-                                                                       res = mount("/dev/mmcblk1p1", destPath[i], "vfat", 0,
-                                                                                       "uid=0,gid=0,dmask=0000,fmask=0111,iocharset=iso8859-1,utf8,shortname=mixed,smackfsroot=*,smackfsdef=*");
-#endif
-                                                                       SysTryCatch(NID_IO, res == 0, , E_SYSTEM, "[E_SYSTEM] Failed to mount (%s), errno: %d (%s)",
+
+                                                                       r = _MmcStorageManagerService::MountExternalPath(destPath[i]);
+                                                                       SysTryCatch(NID_IO,  !IsFailed(r), , E_SYSTEM, "[E_SYSTEM] Failed to mount (%s), errno: %d (%s)",
                                                                                        destPath[i], errno, strerror(errno));
                                                                }
                                                                else
@@ -301,8 +293,8 @@ _MmcStorageManagerService::MmcEventVconfCallback(keynode_t* node, void* userData
                                                                }
                                                        }
 
-                                                       r = ChangeMountMode(MS_SHARED);
-                                                       SysTryCatch(NID_IO, !IsFailed(r), , E_SYSTEM, "[E_SYSTEM] Changing to shared mount mode is failed.");
+                                                       r = _MmcStorageManagerService::ChangeMountMode(MS_SHARED);
+                                                       SysTryCatch(NID_IO, !IsFailed(r), , E_SYSTEM, "[E_SYSTEM] Changing to shared mount mode failed.");
 
                                                        r = _MmcStorageManagerService::CreateFlag(mountFlag);
                                                        SysTryCatch(NID_IO, !IsFailed(r), , E_SYSTEM, "[%s] Failed to create mount flag (%s)",
@@ -391,23 +383,15 @@ _MmcStorageManagerService::MmcEventVconfCallback(keynode_t* node, void* userData
                                        {
                                                // /opt/storage/sdcard -> {app-root}/virtual-root/opt/storage/sdcard
 
-                                               r = ChangeMountMode(MS_PRIVATE);
-                                               SysTryCatch(NID_IO, !IsFailed(r), , E_SYSTEM, "[E_SYSTEM] Changing to private mount mode is failed.");
-#ifdef _OSP_EMUL_
-                                               int mmcblkNum = GetMmcblockNumber();
-                                               SysTryLog(NID_IO, mmcblkNum >= 0, "Failed to get mmcblk number");
-                                               char devPath[_MAX_PATH_LENGTH] = { 0, };
-                                               sprintf(devPath, "/dev/mmcblk%d", mmcblkNum);
-                                               res = mount(devPath, pDestPath.get(), "ext4", 0, "");
-#else
-                                               res = mount("/dev/mmcblk1p1", pDestPath.get(), "vfat", 0,
-                                                               "uid=0,gid=0,dmask=0000,fmask=0111,iocharset=iso8859-1,utf8,shortname=mixed,smackfsroot=*,smackfsdef=*");
-#endif
-                                               SysTryReturnVoidResult(NID_IO, res == 0, E_SYSTEM, "[E_SYSTEM] Failed to mount (%s), errno: %d (%s)",
+                                               r = _MmcStorageManagerService::ChangeMountMode(MS_PRIVATE);
+                                               SysTryCatch(NID_IO, !IsFailed(r), , E_SYSTEM, "[E_SYSTEM] Changing to private mount mode failed.");
+
+                                               r = _MmcStorageManagerService::MountExternalPath(pDestPath.get());
+                                               SysTryReturnVoidResult(NID_IO, !IsFailed(r), E_SYSTEM, "[E_SYSTEM] Failed to mount (%s), errno: %d (%s)",
                                                                pDestPath.get(), errno, strerror(errno));
 
-                                               r = ChangeMountMode(MS_SHARED);
-                                               SysTryCatch(NID_IO, !IsFailed(r), , E_SYSTEM, "[E_SYSTEM] Changing to shared mount mode is failed.");
+                                               r = _MmcStorageManagerService::ChangeMountMode(MS_SHARED);
+                                               SysTryCatch(NID_IO, !IsFailed(r), , E_SYSTEM, "[E_SYSTEM] Changing to shared mount mode failed.");
 
                                                SysLog(NID_IO, "mount() succeeded");
                                        }
@@ -446,10 +430,10 @@ CATCH:
                }
        }
 
-       r = ChangeMountMode(MS_SHARED);
+       r = _MmcStorageManagerService::ChangeMountMode(MS_SHARED);
        if (IsFailed(r))
        {
-               SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Restoring mount mode is failed.");
+               SysLogException(NID_IO, E_SYSTEM, "[E_SYSTEM] Restoring mount mode failed.");
        }
 
        return;
@@ -509,78 +493,6 @@ _MmcStorageManagerService::CreateExternalDirectories(const char srcPath[][_MAX_P
        return E_SUCCESS;
 }
 
-int
-_MmcStorageManagerService::GetMmcblockNumber(void)
-{
-       DIR* pDir = opendir("/sys/block");
-       SysTryReturn(NID_IO, pDir != null, -1, E_SYSTEM, "[E_SYSTEM] Failed to open /sys/block, errno: %d (%s)",
-                       errno, strerror(errno));
-
-       int mmcblkNum = -1;
-       struct dirent* pDirEntry;
-       while ((pDirEntry = readdir(pDir)) != null)
-       {
-               char entryPath[PATH_MAX] = { 0, };
-               sprintf(entryPath, "/sys/block/%s", pDirEntry->d_name);
-
-               struct stat stat;
-               memset(&stat, 0, sizeof(struct stat));
-
-               if (lstat(entryPath, &stat) < 0)
-               {
-                       continue;
-               }
-               if (S_ISDIR(stat.st_mode) || S_ISLNK(stat.st_mode))
-               {
-                       if (strncmp(".", pDirEntry->d_name, 1) == 0 || strncmp("..", pDirEntry->d_name, 2) == 0)
-                       {
-                               continue;
-                       }
-                       if (strncmp("mmcblk", pDirEntry->d_name, 6) == 0)
-                       {
-                               char buf[_MAX_PATH_LENGTH];
-                               snprintf(buf, _MAX_PATH_LENGTH, "/sys/block/%s/device/type", pDirEntry->d_name);
-
-                               int fd = open(buf, O_RDONLY);
-                               if (fd == -1)
-                               {
-                                       SysLog(NID_IO, "Failed to open device type, errno: %d (%s)", errno, strerror(errno));
-                                       continue;
-                               }
-
-                               int readBytes = read(fd, buf, 10);
-                               close(fd);
-                               if (readBytes < 0)
-                               {
-                                       SysLog(NID_IO, "Failed to read device type, errno: %d (%s)", errno, strerror(errno));
-                                       continue;
-                               }
-                               buf[readBytes] = '\0';
-
-                               if (strncmp("SD", buf, 2) == 0)
-                               {
-                                       char* num = strndup((pDirEntry->d_name) + 6, 1);
-                                       if (num == null)
-                                       {
-                                               SysLogException(NID_IO, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
-                                               closedir(pDir);
-                                               return -1;
-                                       }
-
-                                       mmcblkNum = atoi(num);
-                                       free(num);
-
-                                       SysLog(NID_IO, "mmcblk number is %d", mmcblkNum);
-                                       break;
-                               }
-                       }
-               }
-       }
-
-       closedir(pDir);
-       return mmcblkNum;
-}
-
 result
 _MmcStorageManagerService::FindDeviceNode(const char *pPath, char* pBuf, int bufLen)
 {
@@ -606,7 +518,7 @@ _MmcStorageManagerService::FindDeviceNode(const char *pPath, char* pBuf, int buf
 
        endmntent(pFile);
 
-       SysTryReturnResult(NID_IO, found == true, E_OBJ_NOT_FOUND, "Device node does not exist. path: %s", pPath);
+       SysTryReturnResult(NID_IO, found == true, E_OBJ_NOT_FOUND, "Device node for %s does not exist.", pPath);
 
        return E_SUCCESS;
 }
@@ -623,7 +535,7 @@ _MmcStorageManagerService::ChangeMountMode(unsigned long mountFlags)
        for (int i = 0; i < sizeof(dir)/32; ++i)
        {
                //SysLog(NID_IO, "dir: %s", dir[i]);
-               result r = FindDeviceNode(dir[i], devNode, PATH_MAX - 1);
+               result r = _MmcStorageManagerService::FindDeviceNode(dir[i], devNode, PATH_MAX - 1);
                if (r == E_SUCCESS)
                {
                        int ret = mount(devNode, dir[i], NULL, mountFlags, NULL);
@@ -646,5 +558,39 @@ _MmcStorageManagerService::ChangeMountMode(unsigned long mountFlags)
        return E_SUCCESS;
 }
 
+result
+_MmcStorageManagerService::MountExternalPath(const char* pDestPath)
+{
+       const char table[] = "/etc/mtab";
+       const char mmcPath[] = "/opt/storage/sdcard";
+       struct mntent ent;
+       char devNode[PATH_MAX] = { 0, };
+       bool found = false;
+
+       FILE* pFile = setmntent(table, "r");
+       SysTryReturnResult(NID_IO, pFile != null, E_SYSTEM, "Calling setmntent() with %s failed.", table);
+
+       while (getmntent_r(pFile, &ent, devNode, PATH_MAX - 1))
+       {
+               if (strncmp(ent.mnt_dir, mmcPath, strlen(mmcPath)) == 0)
+               {
+                       found = true;
+                       break;
+               }
+       }
+
+       endmntent(pFile);
+
+       SysTryReturnResult(NID_IO, found == true, E_OBJ_NOT_FOUND, "Device node for %s does not exist.", mmcPath);
+
+       SysLog(NID_IO, "mnt_fsname (devNode): %s (%s), mnt_dir: %s, mnt_type: %s, mnt_opts: %s",
+                       ent.mnt_fsname, devNode, ent.mnt_dir, ent.mnt_type, ent.mnt_opts);
+
+       int ret = mount(devNode, pDestPath, ent.mnt_type, 0, ent.mnt_opts);
+       SysTryReturnResult(NID_IO, ret == 0, E_SYSTEM, "Mounting %s failed, errno: %d (%s)", pDestPath, errno, strerror(errno));
+
+       return E_SUCCESS;
+}
+
 }} // Tizen::Io