Relax FileSystem::removeUserData and check its return value
authorKonrad Lipinski <k.lipinski2@samsung.com>
Fri, 27 Mar 2020 10:47:39 +0000 (11:47 +0100)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Fri, 27 Mar 2020 20:06:52 +0000 (21:06 +0100)
Said function no longer returns errors on ENOENT.

Change-Id: I10051ab71028d02b5c6708e20f1f91b45ff67457

src/manager/service/ckm-logic.cpp
src/manager/service/file-system.cpp
src/manager/service/file-system.h

index c557e8ca23c8088465652123c2968f7a12931c1b..18e9e6938208d520fef875b1d70bf596b09614f7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2014-2019 Samsung Electronics Co., Ltd. All rights reserved
+ *  Copyright (c) 2014-2020 Samsung Electronics Co., Ltd. All rights reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -237,15 +237,14 @@ RawBuffer CKMLogic::lockUserKey(uid_t user)
 
 RawBuffer CKMLogic::removeUserData(uid_t user)
 {
-       int retCode = CKM_API_SUCCESS;
-
        if (m_accessControl.isSystemService(user))
                user = SYSTEM_DB_UID;
 
        m_userDataMap.erase(user);
 
-       FileSystem fs(user);
-       fs.removeUserData();
+       const int retCode = FileSystem(user).removeUserData()
+               ? CKM_API_ERROR_FILE_SYSTEM
+               : CKM_API_SUCCESS;
 
        return MessageBuffer::Serialize(retCode).Pop();
 }
index 5ab97b2d46089de14e5d63053a1d832ce3aa2776..0b50d373b6b629a419478ad41cb51ca68650d43c 100644 (file)
@@ -233,37 +233,21 @@ UidVector FileSystem::getUIDsFromDBFile()
 
 int FileSystem::removeUserData() const
 {
-       int err, retCode = 0;
-
-       if (unlink(getDBPath().c_str())) {
-               retCode = -1;
-               err = errno;
-               LogDebug("Error in unlink user database: " << getDBPath()
-                                << "Errno: " << errno << " " << GetErrnoString(err));
-       }
-
-       if (unlink(getDKEKPath().c_str())) {
-               retCode = -1;
-               err = errno;
-               LogDebug("Error in unlink user DKEK: " << getDKEKPath()
-                                << "Errno: " << errno << " " << GetErrnoString(err));
-       }
-
-       if (unlink(getDBDEKPath().c_str())) {
-               retCode = -1;
-               err = errno;
-               LogDebug("Error in unlink user DBDEK: " << getDBDEKPath()
-                                << "Errno: " << errno << " " << GetErrnoString(err));
-       }
-
-       if (unlink(getRemovedAppsPath().c_str())) {
-               retCode = -1;
-               err = errno;
-               LogDebug("Error in unlink user's Removed Apps File: " << getRemovedAppsPath()
-                                << "Errno: " << errno << " " << GetErrnoString(err));
-       }
-
-       return retCode;
+       const auto unlinkUserPath = [](const std::string &path, const char *logDesc) {
+               if (!unlink(path.c_str()))
+                       return 0;
+               const auto err = errno;
+               if (ENOENT == err)
+                       return 0;
+               LogDebug("Error in unlink user" << logDesc << ": " << path
+                                << "Errno: " << err << " " << GetErrnoString(err));
+               return -1;
+       };
+
+       return unlinkUserPath(getDBPath(), " database")
+               | unlinkUserPath(getDKEKPath(), " DKEK")
+               | unlinkUserPath(getDBDEKPath(), " DBDEK")
+               | unlinkUserPath(getRemovedAppsPath(), "'s Removed Apps File");
 }
 
 FileLock FileSystem::lock()
index 8c7775585a4f77c04053a8bccfa086dc5af794a1..31f57ed346f667a25cda27bc52078c8fc0448b97 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2000-2019 Samsung Electronics Co., Ltd. All rights reserved
+ *  Copyright (c) 2000-2020 Samsung Electronics Co., Ltd. All rights reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
 #include <string>
 #include <file-lock.h>
 
+#define wur __attribute__((warn_unused_result))
+
 namespace CKM {
 
 typedef std::vector<ClientId> ClientIdVector;
 typedef std::vector<uid_t> UidVector;
 
-class FileSystem {
+class FileSystem final {
 public:
        explicit FileSystem(uid_t uid);
 
-       std::string getDBPath() const;
+       wur std::string getDBPath() const;
 
        // Domain Key Encryption Key
-       RawBuffer getDKEK() const;
+       wur RawBuffer getDKEK() const;
        void saveDKEK(const RawBuffer &buffer) const;
 
        // Database Data Encryption Key
-       RawBuffer getDBDEK() const;
+       wur RawBuffer getDBDEK() const;
        void saveDBDEK(const RawBuffer &buffer) const;
 
        // Remove all ckm data related to user
-       int removeUserData() const;
+       wur int removeUserData() const;
 
        void addRemovedApp(const ClientId &app) const;
-       ClientIdVector clearRemovedsApps() const;
-
-       static int init();
-       static UidVector getUIDsFromDBFile();
-       static FileLock lock();
+       wur ClientIdVector clearRemovedsApps() const;
 
-       virtual ~FileSystem() {}
+       wur static int init();
+       wur static UidVector getUIDsFromDBFile();
+       wur static FileLock lock();
 
-protected:
-       std::string getDKEKPath() const;
-       std::string getDBDEKPath() const;
-       RawBuffer loadFile(const std::string &path) const;
+private:
+       wur std::string getDKEKPath() const;
+       wur std::string getDBDEKPath() const;
+       wur RawBuffer loadFile(const std::string &path) const;
        void saveFile(const std::string &path, const RawBuffer &buffer) const;
-       std::string getRemovedAppsPath() const;
+       wur std::string getRemovedAppsPath() const;
 
        uid_t m_uid;
 };