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;
Gallery::~Gallery()
{
if (m_isMediaDbConnected) {
- const int ret = media_content_disconnect();
- if (ret != 0) {
- WLOG("media_content_disconnect() failed: %d", ret);
- }
+ releaseMediaDbConnection();
}
}
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;
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 {
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;
}
namespace gallery { namespace { namespace impl {
constexpr auto UNIQUE_PATH_RESERVE = 10;
+
+ int MEDIA_DB_CONNECTION_COUNTER = 0;
}}}
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 {
ucl::Mutex &getMediaMutex();
ucl::Result getInternalStorageId(int &result);
+
+ ucl::Result acquireMediaDbConnection();
+ void releaseMediaDbConnection();
}
namespace gallery { namespace util {