Fix the coverity issues
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / services / mesh / bt-service-mesh-util.c
index 12629e2..2d44d0b 100644 (file)
@@ -19,6 +19,7 @@
  *
  */
 #include <dirent.h>
+#include <fcntl.h>
 #include <ftw.h>
 #include <unistd.h>
 #include <stdio.h>
@@ -124,8 +125,9 @@ uint16_t _bt_mesh_util_opcode_set(uint32_t opcode,
                buf[0] = (opcode >> 16) & 0xff;
                l_put_be16(opcode, buf + 1);
                return 3;
-       } else
+       } else {
                return 0;
+       }
 }
 
 bool _bt_mesh_util_opcode_get(const uint8_t *buf,
@@ -262,6 +264,37 @@ bool _bt_mesh_util_create_directory(const char *mesh_dir)
        return true;
 }
 
+bool _bt_mesh_util_delete_file(const char *filename)
+{
+       struct stat st;
+       int fd;
+
+       fd = open(filename, O_RDONLY);
+       if (fd < 0) {
+               BT_ERR("Mesh: Failed to open [%s]", filename);
+               return false;
+       }
+
+       if (fstat(fd, &st) < 0) {
+               BT_ERR("Mesh: Failed to stat [%s]", filename);
+               close(fd);
+               return false;
+       }
+
+       if (S_ISREG(st.st_mode)) {
+               BT_INFO("Mesh: Failed stat: success [%s]", filename);
+               close(fd);
+               remove(filename);
+               return true;
+       } else {
+               BT_ERR("Mesh: [%s] Is not a regular file", filename);
+       }
+
+       BT_INFO("Mesh: File [%s] deleted", filename);
+       close(fd);
+       return false;
+}
+
 bool _bt_mesh_util_is_directory_exists(const char *dir_path)
 {
        struct stat st;