Improve metadata_editor_set_path() API 58/231658/11
authorhj kim <backto.kim@samsung.com>
Thu, 23 Apr 2020 08:38:44 +0000 (17:38 +0900)
committerhj kim <backto.kim@samsung.com>
Wed, 6 May 2020 07:23:07 +0000 (16:23 +0900)
Change-Id: I969f3da6669f7c9c62db44af9be9321f97581a67

include/metadata_editor_private.h
src/metadata_editor.cpp

index eafbc2a..ffcd21e 100755 (executable)
@@ -86,7 +86,7 @@ extern "C" {
                } while (0)
 
 typedef struct {
-       void*   file;
+       TagLib::File*   file;
        int     filetype;
        bool    isOpen;
        bool    isReadOnly;
index f424edc..1533010 100755 (executable)
@@ -796,222 +796,84 @@ extern "C" int metadata_editor_create(metadata_editor_h *metadata) {
 
 // *** This function is used to open the file. It creates the instance that is responsible for connection with file *** //
 extern "C" int metadata_editor_set_path(metadata_editor_h metadata, const char *path) {
-       // Check if we have valid arguments to work with
-       metadata_editor_retvm_if(!metadata, METADATA_EDITOR_ERROR_INVALID_PARAMETER, "Invalid metadata");
-       metadata_editor_retvm_if(!path, METADATA_EDITOR_ERROR_INVALID_PARAMETER, "Invalid path");
+       int media_type = METADATA_EDITOR_FORMAT_NOTYPE;
+       metadata_editor_s *_metadata = (metadata_editor_s*)metadata;
+       TagLib::File* _file = NULL;
 
-       int exist;
+       metadata_editor_retvm_if(!_metadata, METADATA_EDITOR_ERROR_INVALID_PARAMETER, "Invalid metadata");
+       metadata_editor_retvm_if(!path, METADATA_EDITOR_ERROR_INVALID_PARAMETER, "Invalid path");
 
-       /* check the file exits actually */
-       exist = open(path, O_RDONLY);
-       if(exist < 0) {
+       if (access(path, R_OK) < 0) {
                if (errno == EACCES || errno == EPERM) {
                        metadata_editor_error("Permission denied");
                        return METADATA_EDITOR_ERROR_PERMISSION_DENIED;
                } else {
-                       metadata_editor_error("Not exist file");
+                       metadata_editor_error("Fail to open path");
                        return METADATA_EDITOR_ERROR_FILE_EXISTS;
                }
        }
 
-       close(exist);
-
-       metadata_editor_s *_metadata = (metadata_editor_s*)metadata;
-       int media_type = METADATA_EDITOR_FORMAT_NOTYPE;
-
        media_type = __metadata_editor_get_file_type(path);
 
-       switch (media_type) {                                                   // Parse file according the specified type
-               case METADATA_EDITOR_FORMAT_MP3: {
-                       if (_metadata->file) {
-                               TagLib::MPEG::File* _file = (TagLib::MPEG::File*)_metadata->file;
-                               metadata_editor_info("file free [%p]", _metadata->file);
-                               delete _file;
-                               _metadata->file = NULL;
-                               _metadata->filetype = METADATA_EDITOR_FORMAT_NOTYPE;
-                               _metadata->isOpen = false;
-                               _metadata->isReadOnly = true;
-                       }
-
-                       // Allocate the file object in memory to work with it later on
-                       TagLib::MPEG::File* _file = new TagLib::MPEG::File(path);
-
-                       metadata_editor_retvm_if(_file == NULL, METADATA_EDITOR_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
-
-                       _metadata->file = _file;                                // Copy file pointer to the structure
-
-                       _metadata->filetype = METADATA_EDITOR_FORMAT_MP3;
-
-                       if (_file->isOpen()) {                                  // Check if the file was opened successfully
-                               metadata_editor_info("The file is successfully opened. Address is %p", _metadata->file);
-                               _metadata->isOpen = true;
-                       } else {                                                        // The file does not exist or you have no permission to process it
-                               metadata_editor_error("The file was not found. Pointer address is %p", _metadata->file);
-                               _metadata->isOpen = false;
-                               return METADATA_EDITOR_ERROR_PERMISSION_DENIED;
-                       }
-
-                       if (_file->readOnly()) {                                        // Check if the file is readonly
-                               metadata_editor_info("File is readonly");
-                               _metadata->isReadOnly = true;
-                       } else {                                                        // or not
-                               metadata_editor_info("The file is writable");
-                               _metadata->isReadOnly = false;
-                       }
+       if (_metadata->file) {
+               metadata_editor_info("file free [%p]", _metadata->file);
+               delete _metadata->file;
 
-                       return METADATA_EDITOR_ERROR_NONE;
-               }
-               case METADATA_EDITOR_FORMAT_MP4: {
-                       if (_metadata->file) {
-                               TagLib::MP4::File* _file = (TagLib::MP4::File*)_metadata->file;
-                               metadata_editor_info("file free [%p]", _metadata->file);
-                               delete _file;
-                               _metadata->file = NULL;
-                               _metadata->filetype = METADATA_EDITOR_FORMAT_NOTYPE;
-                               _metadata->isOpen = false;
-                               _metadata->isReadOnly = true;
-                       }
-
-                       // Allocate the file object in memory to work with it later on
-                       TagLib::MP4::File* _file = new TagLib::MP4::File(path);
-
-                       metadata_editor_retvm_if(_file == NULL, METADATA_EDITOR_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
+               _metadata->file = NULL;
+               _metadata->filetype = METADATA_EDITOR_FORMAT_NOTYPE;
+               _metadata->isOpen = false;
+               _metadata->isReadOnly = true;
+       }
 
-                       _metadata->file = _file;                                // Copy file pointer to the structure
+       try {
+       switch (media_type) {
+       case METADATA_EDITOR_FORMAT_MP3:
+               _file = new TagLib::MPEG::File(path);
+               break;
 
-                       _metadata->filetype = METADATA_EDITOR_FORMAT_MP4;
+       case METADATA_EDITOR_FORMAT_MP4:
+               _file = new TagLib::MP4::File(path);
+               break;
 
-                       if (_file->isOpen()) {                                  // Check if the file was opened successfully
-                               metadata_editor_info("The file is successfully opened. Address is %p", _metadata->file);
-                               _metadata->isOpen = true;
-                       } else {                                                        // The file does not exist or you have no permission to process it
-                               metadata_editor_error("The file was not found. Pointer address is %p", _metadata->file);
-                               _metadata->isOpen = false;
-                               return METADATA_EDITOR_ERROR_FILE_EXISTS;
-                       }
-                       if (_file->readOnly()) {                                        // Check if the file is readonly
-                               metadata_editor_info("File is readonly");
-                               _metadata->isReadOnly = true;
-                       } else {                                                        // or not
-                               metadata_editor_info("The file is writable");
-                               _metadata->isReadOnly = false;
-                       }
-                       return METADATA_EDITOR_ERROR_NONE;
-               }
 #if 0
-               case METADATA_EDITOR_FORMAT_FLAC: {
-                       // Allocate the file object in memory to work with it later on
-                       TagLib::FLAC::File* _file = new TagLib::FLAC::File(path);
-
-                       metadata_editor_retvm_if(_file == NULL, METADATA_EDITOR_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
-
-                       _metadata->file = _file;                                // Copy file pointer to the structure
-
-                       _metadata->filetype = METADATA_EDITOR_FORMAT_FLAC;
-
-                       if (_file->isOpen()) {                          // Check if the file was opened successfully
-                               metadata_editor_info("The file is successfully opened. Address is %p", _metadata->file);
-                               _metadata->isOpen = true;
-                       } else {                                                        // The file does not exist or you have no permission to process it
-                               metadata_editor_error("The file was not found. Pointer address is %p", _metadata->file);
-                               _metadata->isOpen = false;
-                               return METADATA_EDITOR_ERROR_FILE_EXISTS;
-                       }
-                       if (_file->readOnly()) {                                        // Check if the file is readonly
-                               metadata_editor_info("File is readonly");
-                               _metadata->isReadOnly = true;
-                       } else {                                                        // or not
-                               metadata_editor_info("The file is writable");
-                               _metadata->isReadOnly = false;
-                       }
-                       return METADATA_EDITOR_ERROR_NONE;
-               }
-               case METADATA_EDITOR_FORMAT_OGG_VORBIS: {
-                       // Allocate the file object in memory to work with it later on
-                       TagLib::Ogg::Vorbis::File* _file = new TagLib::Ogg::Vorbis::File(path);
-
-                       metadata_editor_retvm_if(_file == NULL, METADATA_EDITOR_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
-
-                       _metadata->file = _file;                                // Copy file pointer to the structure
-
-                       _metadata->filetype = METADATA_EDITOR_FORMAT_OGG_VORBIS;
-
-                       if (_file->isOpen()) {                          // Check if the file was opened successfully
-                               metadata_editor_info("The file is successfully opened. Address is %p", _metadata->file);
-                               _metadata->isOpen = true;
-                       } else {                                                        // The file does not exist or you have no permission to process it
-                               metadata_editor_error("The file was not found. Pointer address is %p", _metadata->file);
-                               _metadata->isOpen = false;
-                               return METADATA_EDITOR_ERROR_FILE_EXISTS;
-                       }
-                       if (_file->readOnly()) {                                        // Check if the file is readonly
-                               metadata_editor_info("File is readonly");
-                               _metadata->isReadOnly = true;
-                       } else {                                                        // or not
-                               metadata_editor_info("The file is writable");
-                               _metadata->isReadOnly = false;
-                       }
-                       return METADATA_EDITOR_ERROR_NONE;
-               }
-               case METADATA_EDITOR_FORMAT_OGG_FLAC: {
-                       // Allocate the file object in memory to work with it later on
-                       TagLib::Ogg::FLAC::File* _file = new TagLib::Ogg::FLAC::File(path);
+       case METADATA_EDITOR_FORMAT_FLAC:
+               _file = new TagLib::FLAC::File(path);
+               break;
 
-                       metadata_editor_retvm_if(_file == NULL, METADATA_EDITOR_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
+       case METADATA_EDITOR_FORMAT_OGG_VORBIS:
+               _file = new TagLib::Ogg::Vorbis::File(path);
+               break;
 
-                       _metadata->file = _file;                                // Copy file pointer to the structure
+       case METADATA_EDITOR_FORMAT_OGG_FLAC:
+               _file = new TagLib::Ogg::FLAC::File(path);
+               break;
 
-                       _metadata->filetype = METADATA_EDITOR_FORMAT_OGG_FLAC;
-
-                       if (_file->isOpen()) {                                  // Check if the file was opened successfully
-                               metadata_editor_info("The file is successfully opened. Address is %p", _metadata->file);
-                               _metadata->isOpen = true;
-                       } else {                                                        // The file does not exist or you have no permission to process it
-                               metadata_editor_error("The file was not found. Pointer address is %p", _metadata->file);
-                               _metadata->isOpen = false;
-                               return METADATA_EDITOR_ERROR_FILE_EXISTS;
-                       }
-                       if (_file->readOnly()) {                                        // Check if the file is readonly
-                               metadata_editor_info("File is readonly");
-                               _metadata->isReadOnly = true;
-                       } else {                                                        // or not
-                               metadata_editor_info("The file is writable");
-                               _metadata->isReadOnly = false;
-                       }
-                       return METADATA_EDITOR_ERROR_NONE;
-               }
-               case METADATA_EDITOR_FORMAT_WAV: {
-                       // Allocate the file object in memory to work with it later on
-                       TagLib::RIFF::WAV::File* _file = new TagLib::RIFF::WAV::File(path);
+       case METADATA_EDITOR_FORMAT_WAV:
+               // Allocate the file object in memory to work with it later on
+               _file = new TagLib::RIFF::WAV::File(path);
+               break;
 
-                       metadata_editor_retvm_if(_file == NULL, METADATA_EDITOR_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
+#endif
+       default:
+               metadata_editor_error("Wrong file type");
+               return METADATA_EDITOR_ERROR_NOT_SUPPORTED;
+       }
+       } catch (const std::bad_alloc &ex) {
+               metadata_editor_retvm_if(!_file, METADATA_EDITOR_ERROR_OUT_OF_MEMORY, "OUT_OF_MEMORY");
+       }
 
-                       _metadata->file = _file;                                // Copy file pointer to the structure
+       if (!_file->isOpen()) {
+               metadata_editor_error("The file was not found. Pointer address is %p", _file);
+               delete _file;
+               return METADATA_EDITOR_ERROR_PERMISSION_DENIED;
+       }
 
-                       _metadata->filetype = METADATA_EDITOR_FORMAT_WAV;
+       _metadata->file = _file;
+       _metadata->filetype = media_type;
+       _metadata->isOpen = true;
+       _metadata->isReadOnly = _file->readOnly();
 
-                       if (_file->isOpen()) {                                  // Check if the file was opened successfully
-                               metadata_editor_info("The file is successfully opened. Address is %p", _metadata->file);
-                               _metadata->isOpen = true;
-                       } else {                                                        // The file does not exist or you have no permission to process it
-                               metadata_editor_error("The file was not found. Pointer address is %p", _metadata->file);
-                               _metadata->isOpen = false;
-                               return METADATA_EDITOR_ERROR_FILE_EXISTS;
-                       }
-                       if (_file->readOnly()) {                                        // Check if the file is readonly
-                               metadata_editor_info("File is readonly");
-                               _metadata->isReadOnly = true;
-                       } else {                                                        // or not
-                               metadata_editor_info("The file is writable");
-                               _metadata->isReadOnly = false;
-                       }
-                       return METADATA_EDITOR_ERROR_NONE;
-               }
-#endif
-               default:
-                       metadata_editor_error("Wrong file type");
-                       return METADATA_EDITOR_ERROR_NOT_SUPPORTED;
-       }
+       return METADATA_EDITOR_ERROR_NONE;
 }
 
 static int __metadata_editor_get_mp3_metadata(metadata_editor_s *metadata, metadata_editor_attr_e attribute, char **value)