libcommon: Add syscommon_is_mounted() 64/296264/2 accepted/tizen/unified/20230726.163532
authorYoungjae Cho <y0.cho@samsung.com>
Mon, 24 Jul 2023 11:46:52 +0000 (20:46 +0900)
committerYoungjae Cho <y0.cho@samsung.com>
Tue, 25 Jul 2023 01:55:46 +0000 (10:55 +0900)
syscmmon_is_mounted() checks whether the given path exists in /etc/mtab.

Change-Id: I994c4190a069330403e5eb516eb6fef7f5d5404f
Signed-off-by: Youngjae Cho <y0.cho@samsung.com>
include/libsyscommon/common.h
src/libcommon/common.c

index 7fd6f66da0ba7b0d0d9b2c282f3a30c5c906b7ac..f6cd1081098e28e83929eccb518d01ce14508e96 100644 (file)
@@ -43,6 +43,13 @@ bool syscommon_is_emulator(void);
  */
 bool syscommon_is_container(void);
 
+/**
+ * @brief Check if the path is mounted
+ *
+ * @return true if the path is mounted, otherwise return false
+ */
+bool syscommon_is_mounted(const char *path);
+
 #ifdef __cplusplus
 }
 #endif
index 2e96f2996908424bf3e1bca3cf9f62e8c547e5e8..0a220c7678331540ed76ed43648eb8af4e3d6367 100644 (file)
@@ -24,6 +24,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <mntent.h>
 #include <system_info.h>
 #include "shared/log.h"
 #include "libsyscommon/common.h"
@@ -72,3 +73,29 @@ bool syscommon_is_container(void)
 
        return is_container;
 }
+
+bool syscommon_is_mounted(const char *path)
+{
+       bool ret = false;
+       struct mntent *mnt;
+       const char *table = "/etc/mtab";
+       FILE *fp;
+       int len;
+
+       fp = setmntent(table, "r");
+       if (!fp)
+               return ret;
+
+       len = strlen(path) + 1;
+       while (1) {
+               mnt = getmntent(fp);
+               if (mnt == NULL)
+                       break;
+               if (!strncmp(mnt->mnt_dir, path, len)) {
+                       ret = true;
+                       break;
+               }
+       }
+       endmntent(fp);
+       return ret;
+}