Check fs errors before saving the file 61/199461/7
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Mon, 11 Feb 2019 16:04:46 +0000 (17:04 +0100)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Wed, 27 Mar 2019 15:44:39 +0000 (15:44 +0000)
GetFd(os) on a non-existing file causes segfault.

Change-Id: I8365dfbddace160ae99b1e7d1f6070ee1032f6cd

src/manager/service/file-system.cpp

index 6a70c20..079a4cd 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2000 - 2019 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.
@@ -122,11 +122,18 @@ void FileSystem::saveFile(const std::string &path,
 {
        std::ofstream os(path, std::ios::out | std::ofstream::binary |
                                         std::ofstream::trunc);
+
+       if (os.fail())
+               ThrowErr(Exc::FileSystemFailed, "Can't open file for writing: ", path);
+
        std::copy(buffer.begin(), buffer.end(), std::ostreambuf_iterator<char>(os));
 
-       // Prevent desynchronization in batter remove test.
+       // Prevent desynchronization in battery remove test.
        os.flush();
-       fsync(FstreamAccessors<std::ofstream>::GetFd(os)); // flush kernel space buffer
+       if (fsync(FstreamAccessors<std::ofstream>::GetFd(os)) != 0) { // flush kernel space buffer
+               auto desc = GetErrnoString(errno);
+               ThrowErr(Exc::FileSystemFailed, "Failed to sync file: ", path, " Reason: ", desc);
+       }
        os.close();
 
        if (os.fail())