[download] Fix get path function
authorDeqing Huang <deqing.huang@intel.com>
Mon, 14 Oct 2013 06:55:08 +0000 (14:55 +0800)
committerDeqing Huang <deqing.huang@intel.com>
Mon, 14 Oct 2013 07:02:40 +0000 (15:02 +0800)
By this the correct path is returned when it is already exists.

download/download_context.h
download/download_context_mobile.cc

index 517d271..65f4a90 100644 (file)
@@ -93,7 +93,7 @@ class DownloadContext {
   // FullDestPath = HomePath + DestPath
   // TODO(hdq): This depends on filesystem api?
   const std::string GetFullDestinationPath(const std::string destination) const;
-  const std::string GetRealLocation(const std::string& destination) const;
+  const std::string GetActualFolder(const std::string& destination) const;
 
   bool GetDownloadID(const picojson::value& msg,
                      int& downloadID, DownloadArgs** args);
index 5544a4a..3c42370 100644 (file)
@@ -8,7 +8,7 @@
 
 #include "download/download_context.h"
 
-const std::string DownloadContext::GetRealLocation(
+const std::string DownloadContext::GetActualFolder(
     const std::string& destination) const {
   typedef std::map<std::string, std::string> LocationMap;
   static const LocationMap::value_type data[] = {
@@ -31,19 +31,16 @@ const std::string DownloadContext::GetFullDestinationPath(
     const std::string destination) const {
   // TODO(hdq): User should be able to choose store to external storage
   //            i.e. /opt/storage/sdcard/Downloads
-  std::string path("/opt/usr/media/");
-  std::string location = destination;
-  if (destination.empty()) {
-    location = "Downloads";
-  }
-  std::string default_path = path+location;
-  path += GetRealLocation(location);
+  const std::string directory("/opt/usr/media/");
+  const std::string default_folder("Downloads");
+  const std::string location = destination.empty() ? default_folder : destination;
+  std::string path = directory + GetActualFolder(location);
 
   // Create path if not exist
   struct stat path_stat;
-  if (stat(path.c_str(), &path_stat) != -1
-      || mkdir(path.c_str(), 0777) != 0) {
-    path = default_path;
+  if (stat(path.c_str(), &path_stat) == -1
+      && mkdir(path.c_str(), 0777) != 0) {
+    path = directory + default_folder;
   }
 
   return path;