Fix of: TizenRefApp-8771 Saving image to Gear fails 69/135769/2 submit/tizen/20170703.071445 submit/tizen/20170725.094758 submit/tizen_4.0/20170811.094300
authorIgor Nazarov <i.nazarov@samsung.com>
Mon, 26 Jun 2017 09:39:58 +0000 (12:39 +0300)
committerIgor Nazarov <i.nazarov@samsung.com>
Mon, 26 Jun 2017 09:44:31 +0000 (12:44 +0300)
- Added acuireMediaDbConnection/acuireMediaDbConnection functions;
- Added copyFile() temp function.

Change-Id: If5277a52c0af5b3ed9eeb2bc599ed9149bbd5315

src/common.h
src/model/Gallery.cpp
src/model/MediaItem.cpp
src/model/helpers.cpp
src/model/helpers.h

index 43890c017eac4d05515841d482a509b421e31d22..eff0cb08c39568e8d567de40f0c7938f223945da 100644 (file)
@@ -39,6 +39,7 @@ namespace gallery {
        using ucl::RES_INVALID_ARGUMENTS;
        using ucl::RES_ILLEGAL_STATE;
        using ucl::RES_NOT_SUPPORTED;
+       using ucl::RES_IO_ERROR;
        using ucl::RES_INVALID_DATA;
        using ucl::RES_FATAL;
 
index a2c9accca74d8455a875a04594c577f896ce9d7c..ad7c1c19907d3a9dbcef5a6848156b543ff07049 100644 (file)
@@ -30,10 +30,7 @@ namespace gallery {
        Gallery::~Gallery()
        {
                if (m_isMediaDbConnected) {
-                       const int ret = media_content_disconnect();
-                       if (ret != 0) {
-                               WLOG("media_content_disconnect() failed: %d", ret);
-                       }
+                       releaseMediaDbConnection();
                }
        }
 
@@ -46,10 +43,8 @@ namespace gallery {
 
        Result Gallery::prepare()
        {
-               const int ret = media_content_connect();
-               if (ret != 0) {
-                       LOG_RETURN(RES_FAIL, "media_content_connect() failed: %d", ret);
-               }
+               FAIL_RETURN(acquireMediaDbConnection(),
+                               "acquireMediaDbConnection() failed!");
 
                m_isMediaDbConnected = true;
 
index a0db727f3415095b47e76784ebabae2fb752ca7a..c20148ac92c620edb05b3b0aab254ff2e0ef757a 100644 (file)
@@ -52,6 +52,50 @@ namespace gallery { namespace { namespace impl {
 
                return MediaType::OTHERS;
        }
+
+       // Temp function until ecore_file_cp() fix.
+       Result copyFile(const std::string &src, const std::string &dst)
+       {
+               FILE *f1 = fopen(src.c_str(), "rb");
+               if (!f1) {
+                       LOG_RETURN(RES_IO_ERROR, "fopen(rb) failed!");
+               }
+
+               FILE *f2 = fopen(dst.c_str(), "wb");
+               if (!f2) {
+                       fclose(f1);
+                       LOG_RETURN(RES_IO_ERROR, "fopen(wb) failed!");
+               }
+
+               bool badCopy = false;
+
+               constexpr auto COPY_BUF_SIZE = 16384;
+               char buf[COPY_BUF_SIZE];
+               while (!feof(f1)) {
+                       const auto num = fread(buf, 1, sizeof(buf), f1);
+                       if (num == 0) {
+                               badCopy = true;
+                               LOG_BREAK(RES_IO_ERROR, "fread() failed!");
+                       }
+                       if (fwrite(buf, 1, num, f2) != num) {
+                               badCopy = true;
+                               LOG_BREAK(RES_IO_ERROR, "fwrite() failed!");
+                       }
+               }
+
+               fclose(f1);
+               fclose(f2);
+
+               if (badCopy) {
+                       const int r = remove(dst.c_str());
+                       if (r != 0) {
+                               WLOG("remove() failed: %d;", r);
+                       }
+                       return RES_IO_ERROR;
+               }
+
+               return RES_OK;
+       }
 }}}
 
 namespace gallery {
@@ -384,13 +428,14 @@ namespace gallery {
                const std::string savePath = util::makeUniqueFilePath(
                                m_filePath, imagesDir);
 
-               if (!ecore_file_cp(m_filePath.c_str(), savePath.c_str())) {
-                       LOG_RETURN(RES_FAIL, "ecore_file_cp() failed!");
-               }
+               FAIL_RETURN(impl::copyFile(m_filePath, savePath), "copyFile() failed!");
 
                {
                        MutexLock lock(getMediaMutex());
+                       FAIL_RETURN(acquireMediaDbConnection(),
+                                       "acquireMediaDbConnection() failed!");
                        const int ret = media_content_scan_file(savePath.c_str());
+                       releaseMediaDbConnection();
                        if (ret == 0) {
                                return RES_OK;
                        }
index 34c5a00aa0eb5a43cef824909553f02de5e484f3..9bf5042179c9261890fa19ee7abcbbba1df1ba6e 100644 (file)
@@ -24,6 +24,8 @@
 namespace gallery { namespace { namespace impl {
 
        constexpr auto UNIQUE_PATH_RESERVE = 10;
+
+       int MEDIA_DB_CONNECTION_COUNTER = 0;
 }}}
 
 namespace gallery {
@@ -70,6 +72,36 @@ namespace gallery {
 
                return RES_OK;
        }
+
+       Result acquireMediaDbConnection()
+       {
+               if (impl::MEDIA_DB_CONNECTION_COUNTER > 0) {
+                       ++impl::MEDIA_DB_CONNECTION_COUNTER;
+                       return RES_OK;
+               }
+
+               FAIL_RETURN(util::call(media_content_connect),
+                                       "media_content_connect() failed!");
+
+               impl::MEDIA_DB_CONNECTION_COUNTER = 1;
+
+               return RES_OK;
+       }
+
+       void releaseMediaDbConnection()
+       {
+               if (impl::MEDIA_DB_CONNECTION_COUNTER == 0) {
+                       WLOG("Not connected!");
+                       return;
+               }
+
+               if (impl::MEDIA_DB_CONNECTION_COUNTER == 1) {
+                       FAIL_LOG(util::call(media_content_disconnect),
+                                       "media_content_disconnect() failed!");
+               }
+
+               --impl::MEDIA_DB_CONNECTION_COUNTER;
+       }
 }
 
 namespace gallery { namespace util {
index 4fa988f3e19f5a68b48a4e9434041f328b4ebf3e..2c956c4d5e502d65f125a4616d046b1a48c39d6c 100644 (file)
@@ -30,6 +30,9 @@ namespace gallery {
        ucl::Mutex &getMediaMutex();
 
        ucl::Result getInternalStorageId(int &result);
+
+       ucl::Result acquireMediaDbConnection();
+       void releaseMediaDbConnection();
 }
 
 namespace gallery { namespace util {