Add a file check 91/262491/1
authorHwankyu Jhun <h.jhun@samsung.com>
Thu, 12 Aug 2021 03:46:07 +0000 (12:46 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Thu, 12 Aug 2021 03:46:07 +0000 (12:46 +0900)
This patch adds to check whether the file is a regular file or not.
If it's not a regular file, the mime_type_get_mime_type_for_file() funtion
returns a negative error value.

Change-Id: I832fea354bc01561db7030a5a6f75b7f92644ebe
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
mime-type/mime_type.cc

index f179c65..1d80921 100644 (file)
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#include <sys/stat.h>
+#include <sys/types.h>
 #include <unistd.h>
 
 #include <string>
@@ -128,6 +130,18 @@ EXPORT int mime_type_get_mime_type_for_file(const char* file_path,
     return MIME_TYPE_ERROR_IO_ERROR;
   }
 
+  struct stat statbuf;
+  int ret = stat(file_path, &statbuf);
+  if (ret != 0) {
+    _E("stat() is failed. path(%s), errno(%d)", file_path, errno);
+    return MIME_TYPE_ERROR_IO_ERROR;
+  }
+
+  if (!S_ISREG(statbuf.st_mode)) {
+    _E("%s is not regular file", file_path);
+    return MIME_TYPE_ERROR_INVALID_PARAMETER;
+  }
+
   auto xdg_mime_type = content::XdgMime::GetMimeTypeForFile(file_path);
   if (xdg_mime_type.empty()) {
     _E("GetMimeTypeForFile(%s) is failed", file_path);