remove existing file before writing mode accepted/tizen/unified/20200514.050644 submit/tizen/20200513.090930
authorYoungjae Shin <yj99.shin@samsung.com>
Wed, 13 May 2020 09:02:45 +0000 (18:02 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Wed, 13 May 2020 09:03:50 +0000 (18:03 +0900)
supervisor/ModeManager.cpp
supervisor/ModesEx.cpp
supervisor/ModesEx.h
supervisor/XMLGenerator.cpp

index 3a1d6d67ef5e81d2e33c29a8464893c9e32e4133..0ad3cc612b18fa9bef5f382fab0e20660dae9de0 100644 (file)
@@ -107,9 +107,16 @@ int ModeManager::applyMode(const string &modeName, ClientPrivilege &priv, bool i
                        careTaker.pushMode(mode);
                }
        } catch (ModesEx &e) {
-               //Ususally, it is reached by Invalid Mode File
                ERR("apply Fail(%s)", e.what());
-               return MODES_ERROR_NO_DATA;
+
+               switch (e.getErrCode()) {
+               case ModesEx::INVALID_ARG:
+                       //Ususally, it is reached by Invalid Mode File
+                       return MODES_ERROR_NO_DATA;
+               case ModesEx::SYSTEM_ERROR:
+               default:
+                       return MODES_ERROR_SYSTEM;
+               }
        }
        notifyObservers(modeName, ModeObserver::ON);
        return MODES_ERROR_NONE;
@@ -132,7 +139,14 @@ int ModeManager::addMode(const Mode &mode)
        } catch (ModesEx &e) {
                ERR("XML generator Fail(%s)", e.what());
                modeMap.erase(result.first);
-               return MODES_ERROR_INVALID_PARAMETER;
+
+               switch (e.getErrCode()) {
+               case ModesEx::INVALID_ARG:
+                       return MODES_ERROR_INVALID_PARAMETER;
+               case ModesEx::SYSTEM_ERROR:
+               default:
+                       return MODES_ERROR_SYSTEM;
+               }
        }
 
        DBG("[%zu]addMode : Name(%s), Path(%s), Type(%d)", modeMap.size(), mode.getName().c_str(),
index 764e42c10bd6114c5ba9cbacbd7ad579ff6a0b4b..951b0d65ee00f6c0c73c0e0633317ec25c16892c 100644 (file)
@@ -30,6 +30,11 @@ ModesEx::ModesEx(ErrCode code, const std::string &msg)
 {
 }
 
+ModesEx::ErrCode ModesEx::getErrCode()
+{
+       return err;
+}
+
 std::string ModesEx::getExtraMsg()
 {
        return extraMsg;
index 234de0e4083583dc62abd86b53941ff36e356225..596feb60ac5f7a8b4e1696b599faf170b38a48e9 100644 (file)
@@ -34,6 +34,7 @@ public:
        ModesEx(ErrCode code);
        ModesEx(ErrCode code, const std::string &msg);
 
+       ErrCode getErrCode();
        std::string getExtraMsg();
        virtual const char* what() const throw() override;
 private:
index 53d862a491ccf697f7e70ab459041666a70fa382..a43d23edba432cdded960b0bf300cd2f5ec6aa5a 100644 (file)
@@ -66,13 +66,18 @@ const std::string XMLGenerator::getModeTypeStr(Mode::ModeType t)
        }
 }
 
-void XMLGenerator::makeModeXML(const std::string &filename, const Mode &mode)
+void XMLGenerator::makeModeXML(const std::string &path, const Mode &mode)
 {
-       if (filename.empty()) {
-               ERR("filename empty!!");
+       if (path.empty()) {
+               ERR("NULL path");
                throw ModesEx(ModesEx::INVALID_ARG);
        }
 
+       if (0 != remove(path.c_str()) && ENOENT != errno) {
+               ERR("remove(%s) Fail(%d)", path.c_str(), errno);
+               throw ModesEx(ModesEx::SYSTEM_ERROR);
+       }
+
        createRootNode();
 
        const std::string modeType = getModeTypeStr(mode.getModeType());
@@ -106,20 +111,25 @@ void XMLGenerator::makeModeXML(const std::string &filename, const Mode &mode)
                xmlAddChild(modeNode, actionNode);
        }
 
-       saveToFile(filename);
+       saveToFile(path);
 
        doc->encoding = NULL;
        xmlFreeDoc(doc);
        return;
 }
 
-void XMLGenerator::makeUndoInfoXML(const std::string &filename, const Mode &mode)
+void XMLGenerator::makeUndoInfoXML(const std::string &path, const Mode &mode)
 {
-       if (filename.empty()) {
-               ERR("filename empty!!");
+       if (path.empty()) {
+               ERR("NULL path");
                throw ModesEx(ModesEx::INVALID_ARG);
        }
 
+       if (0 != remove(path.c_str()) && ENOENT != errno) {
+               ERR("remove(%s) Fail(%d)", path.c_str(), errno);
+               throw ModesEx(ModesEx::SYSTEM_ERROR);
+       }
+
        createRootNode();
 
        xmlNodePtr undoInfoNode = xmlNewNode(NULL, ModesXMLTag::UNDO_INFO);
@@ -141,7 +151,7 @@ void XMLGenerator::makeUndoInfoXML(const std::string &filename, const Mode &mode
                xmlAddChild(undoInfoNode, infoNode);
        }
 
-       saveToFile(filename);
+       saveToFile(path);
 
        doc->encoding = NULL;
        xmlFreeDoc(doc);