Set NULL after free
[platform/core/multimedia/libmedia-service.git] / src / media-svc-util.c
index 0473704..d2faaa4 100644 (file)
@@ -128,44 +128,6 @@ RETRY_GEN:
        return g_strdup(uuid_unparsed);
 }
 
-static char * __media_svc_get_exif_datetaken(ExifData *ed)
-{
-       ExifEntry *entry;
-       char tmp[MEDIA_SVC_METADATA_LEN_MAX + 1] = { 0, };
-
-       media_svc_retv_if(!ed, NULL);
-
-       entry = exif_data_get_entry(ed, EXIF_TAG_DATE_TIME_ORIGINAL);
-       if (entry) {
-               exif_entry_get_value(entry, tmp, MEDIA_SVC_METADATA_LEN_MAX);
-               if (strlen(tmp) > 0)
-                       return g_strdup(tmp);
-       }
-
-       entry = exif_data_get_entry(ed, EXIF_TAG_DATE_TIME);
-       if (entry) {
-               exif_entry_get_value(entry, tmp, MEDIA_SVC_METADATA_LEN_MAX);
-               if (strlen(tmp) > 0)
-                       return g_strdup(tmp);
-       }
-
-       return NULL;
-}
-
-static bool __media_svc_get_exif_short(ExifData *ed, ExifTag tagtype, unsigned short *value)
-{
-       ExifEntry *entry;
-
-       media_svc_retv_if(!ed, false);
-       media_svc_retvm_if(!value, false, "value is NULL");
-
-       entry = exif_data_get_entry(ed, tagtype);
-       media_svc_retv_if(!entry, false);
-       *value = exif_get_short(entry->data, exif_data_get_byte_order(ed));
-
-       return true;
-}
-
 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
 {
        int idx = 0;
@@ -386,6 +348,7 @@ int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char
        struct stat st = { 0, };
 
        media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
+       media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "info is NULL");
 
        content_info->path = g_strdup(path);
        content_info->file_name = g_path_get_basename(path);
@@ -397,30 +360,22 @@ int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char
                media_svc_stderror("stat failed");
        }
 
-       /* refresh is TRUE when file modified. so only modified_time and size are changed*/
-       if (refresh) {
-               media_svc_debug("refresh");
-               return MS_MEDIA_ERR_NONE;
+       if (!refresh) {
+               media_svc_retv_del_if(!storage_id, MS_MEDIA_ERR_INTERNAL, content_info);
+               content_info->storage_uuid = g_strdup(storage_id);
+               content_info->media_uuid = __media_info_generate_uuid();
        }
 
-       content_info->storage_uuid = g_strdup(storage_id);
-       media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
-
-       content_info->media_uuid = __media_info_generate_uuid();
-       media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
-
        ret = __media_svc_get_mime_type(path, mime_type);
        media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
 
-       media_svc_debug("mime [%s]", mime_type);
-
        ret = __media_svc_get_media_type(path, mime_type, &media_type);
        media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
 
        content_info->mime_type = g_strdup(mime_type);
        media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
 
-       media_svc_sec_debug("path[%s], media_type[%d]", content_info->path, media_type);
+       media_svc_sec_debug("path[%s], media_type[%d] mime[%s]", content_info->path, media_type, content_info->mime_type);
 
        content_info->media_type = media_type;
 
@@ -451,6 +406,44 @@ char * _media_svc_get_title_from_filename(const char *filename)
        return __media_svc_get_title_from_filename(filename);
 }
 
+static char * __media_svc_get_exif_datetaken(ExifData *ed)
+{
+       ExifEntry *entry;
+       char tmp[MEDIA_SVC_METADATA_LEN_MAX + 1] = { 0, };
+
+       media_svc_retv_if(!ed, NULL);
+
+       entry = exif_data_get_entry(ed, EXIF_TAG_DATE_TIME_ORIGINAL);
+       if (entry) {
+               exif_entry_get_value(entry, tmp, MEDIA_SVC_METADATA_LEN_MAX);
+               if (strlen(tmp) > 0)
+                       return g_strdup(tmp);
+       }
+
+       entry = exif_data_get_entry(ed, EXIF_TAG_DATE_TIME);
+       if (entry) {
+               exif_entry_get_value(entry, tmp, MEDIA_SVC_METADATA_LEN_MAX);
+               if (strlen(tmp) > 0)
+                       return g_strdup(tmp);
+       }
+
+       return NULL;
+}
+
+static bool __media_svc_get_exif_short(ExifData *ed, ExifTag tagtype, unsigned short *value)
+{
+       ExifEntry *entry;
+
+       media_svc_retv_if(!ed, false);
+       media_svc_retvm_if(!value, false, "value is NULL");
+
+       entry = exif_data_get_entry(ed, tagtype);
+       media_svc_retv_if(!entry, false);
+       *value = exif_get_short(entry->data, exif_data_get_byte_order(ed));
+
+       return true;
+}
+
 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
 {
        unsigned short orient_value = 0;
@@ -580,7 +573,7 @@ void _media_svc_extract_audio_metadata(sqlite3 *handle, bool is_direct, media_sv
        if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
                if (g_str_has_suffix(content_info->mime_type, "mp4") || g_str_has_suffix(content_info->mime_type, "3gpp")) {
                        /*Creation time format is 20130101 00:00:00 +0000. change it to 2013:01:01 00:00:00  +0000 like exif time format*/
-                       char *p_value = g_strdelimit(g_strdup(p), "", ':');
+                       char *p_value = g_strdelimit(g_strdup(p), "-", ':');
                        content_info->media_meta.recorded_date = g_strdup_printf("%s +0000", p_value);
                        g_free(p_value);
                } else {
@@ -629,13 +622,14 @@ void _media_svc_extract_video_metadata(media_svc_content_info_s *content_info)
                if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
                        if (g_str_has_suffix(content_info->mime_type, "mp4") || g_str_has_suffix(content_info->mime_type, "3gpp")) {
                                /*Creation time format is 20130101 00:00:00 +0000. change it to 2013:01:01 00:00:00  +0000 like exif time format*/
-                               char *p_value = g_strdelimit(g_strdup(p), "", ':');
+                               char *p_value = g_strdelimit(g_strdup(p), "-", ':');
                                content_info->media_meta.recorded_date = g_strdup_printf("%s +0000", p_value);
                                g_free(p_value);
                        } else {
                                content_info->media_meta.recorded_date = g_strdup(p);
                        }
                }
+
                if (content_info->media_meta.recorded_date == NULL)
                        content_info->media_meta.recorded_date = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
                content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
@@ -644,6 +638,7 @@ void _media_svc_extract_video_metadata(media_svc_content_info_s *content_info)
                if (mmf_error != FILEINFO_ERROR_NONE)
                        media_svc_error("fail to free tag attr - err(%x)", mmf_error);
        }
+
        /* All metadata fields must be empty strings until media_video is deleted */
        content_info->media_meta.title = __media_svc_get_title_from_filename(content_info->file_name);
        content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
@@ -896,8 +891,7 @@ static int __media_svc_get_pdf_metadata(media_svc_content_info_s *content_info)
                        found = NULL;
                }
 
-
-               if (start_pos != 0 && (found = strstr(tmp, "</x:xmpmeta>"))) {
+               if (start_pos > 0 && (found = strstr(tmp + ((cur_pos > start_pos) ? 0 : (start_pos - cur_pos)), "</x:xmpmeta>"))) {
                        end_pos = cur_pos + (found - tmp) + MEDIA_SVC_PDF_TAG_TAIL_LEN;
 //                     media_svc_error("FIND END_POS[%d]", end_pos);
                        found = NULL;
@@ -957,23 +951,23 @@ void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
        media_svc_retm_if(!content_info, "content info is NULL");
 
        /* Delete media_svc_content_info_s */
-       g_free(content_info->media_uuid);
-       g_free(content_info->path);
-       g_free(content_info->file_name);
-       g_free(content_info->mime_type);
-       g_free(content_info->thumbnail_path);
-       g_free(content_info->storage_uuid);
+       g_clear_pointer(&content_info->media_uuid, g_free);
+       g_clear_pointer(&content_info->path, g_free);
+       g_clear_pointer(&content_info->file_name, g_free);
+       g_clear_pointer(&content_info->mime_type, g_free);
+       g_clear_pointer(&content_info->thumbnail_path, g_free);
+       g_clear_pointer(&content_info->storage_uuid, g_free);
 
        /* Delete media_svc_content_meta_s */
-       g_free(content_info->media_meta.title);
-       g_free(content_info->media_meta.album);
-       g_free(content_info->media_meta.artist);
-       g_free(content_info->media_meta.album_artist);
-       g_free(content_info->media_meta.genre);
-       g_free(content_info->media_meta.year);
-       g_free(content_info->media_meta.recorded_date);
-       g_free(content_info->media_meta.track_num);
-       g_free(content_info->media_meta.datetaken);
+       g_clear_pointer(&content_info->media_meta.title, g_free);
+       g_clear_pointer(&content_info->media_meta.album, g_free);
+       g_clear_pointer(&content_info->media_meta.artist, g_free);
+       g_clear_pointer(&content_info->media_meta.album_artist, g_free);
+       g_clear_pointer(&content_info->media_meta.genre, g_free);
+       g_clear_pointer(&content_info->media_meta.year, g_free);
+       g_clear_pointer(&content_info->media_meta.recorded_date, g_free);
+       g_clear_pointer(&content_info->media_meta.track_num, g_free);
+       g_clear_pointer(&content_info->media_meta.datetaken, g_free);
 }
 
 int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_media_type_e media_type, uid_t uid)
@@ -1056,7 +1050,7 @@ static int __media_svc_create_wordbook_db(const char *path, sqlite3 **handle)
 {
        int ret = SQLITE_OK;
        sqlite3 *db_handle = NULL;
-       char *err = NULL;
+       sql_autoptr err = NULL;
 
        ret = sqlite3_open_v2(path, &db_handle, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
        media_svc_retvm_if(ret != SQLITE_OK, ret, "sqlite3_open_v2 failed : %d", ret);
@@ -1083,7 +1077,6 @@ static int __media_svc_create_wordbook_db(const char *path, sqlite3 **handle)
 
 ERROR:
        media_svc_error("sqlite3_exec failed : %s", err);
-       SQLITE3_SAFE_FREE(err);
        sqlite3_close_v2(db_handle);
 
        return ret;
@@ -1114,32 +1107,23 @@ static bool __media_svc_get_wordbook_handle(uid_t uid, sqlite3 **handle)
 
 static bool __media_svc_is_exist_in_wordbook(sqlite3 *db_handle, const char *path)
 {
-       int ret = SQLITE_OK;
-       char *err = NULL;
-       char *query = NULL;
-
-       query = sqlite3_mprintf("UPDATE files SET validity=1 WHERE path = %Q", path);
+       sql_autoptr err = NULL;
+       sql_autoptr q = sqlite3_mprintf("UPDATE files SET validity=1 WHERE path = %Q", path);
 
-       ret = sqlite3_exec(db_handle, query, NULL, NULL, &err);
-       SQLITE3_SAFE_FREE(query);
-       if (ret != SQLITE_OK) {
+       if (sqlite3_exec(db_handle, q, NULL, NULL, &err) != SQLITE_OK) {
                media_svc_error("Query failed. [%s]", err);
-               SQLITE3_SAFE_FREE(err);
                return false;
        }
 
-       return sqlite3_changes(db_handle) > 0 ? true : false;
+       return (sqlite3_changes(db_handle) > 0);
 }
 
 static void __media_svc_insert_to_wordbook(sqlite3 *db_handle, const char *path)
 {
        void *handle = NULL;
        void (*svc_update) (sqlite3 *, const char *);
-       char *query = NULL;
-
-       query = sqlite3_mprintf("INSERT INTO files(path) VALUES(%Q);", path);
-       sqlite3_exec(db_handle, query, NULL, NULL, NULL);
-       sqlite3_free(query);
+       sql_autoptr q = sqlite3_mprintf("INSERT INTO files(path) VALUES(%Q);", path);
+       sqlite3_exec(db_handle, q, NULL, NULL, NULL);
 
        handle = dlopen(PATH_PLUGIN_LIB, RTLD_LAZY);
        if (!handle) {
@@ -1199,21 +1183,17 @@ void _media_svc_clean_wordbook(uid_t uid)
 
 bool _media_svc_get_matched_list(const char *keyword, uid_t uid, GList **list)
 {
-       int ret = SQLITE_OK;
        sqlite3 *handle = NULL;
        sqlite3_stmt *stmt = NULL;
-       char *query = NULL;
+       sql_autoptr q = NULL;
 
        media_svc_retvm_if(!list, false, "list is NULL");
        media_svc_retvm_if(!keyword, false, "keyword is NULL");
        media_svc_retvm_if(!__media_svc_get_wordbook_handle(uid, &handle), false, "Failed to get handle");
 
-       query = sqlite3_mprintf("SELECT files.path FROM files JOIN (SELECT file_id, sum(frequency) AS freq_sum FROM words WHERE word LIKE '%q%%' GROUP BY file_id ORDER BY freq_sum DESC) w ON files.id = w.file_id;", keyword);
-       ret = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
-       SQLITE3_SAFE_FREE(query);
-
-       if (ret != SQLITE_OK) {
-               media_svc_error("Query failed[%d]", ret);
+       q = sqlite3_mprintf("SELECT files.path FROM files JOIN (SELECT file_id, sum(frequency) AS freq_sum FROM words WHERE word LIKE '%q%%' GROUP BY file_id ORDER BY freq_sum DESC) w ON files.id = w.file_id;", keyword);
+       if (sqlite3_prepare_v2(handle, q, -1, &stmt, NULL) != SQLITE_OK) {
+               media_svc_error("query failed");
                sqlite3_close_v2(handle);
                return false;
        }