diff options
author | Krzysztof Jackiewicz <k.jackiewicz@samsung.com> | 2019-02-11 17:04:46 +0100 |
---|---|---|
committer | Krzysztof Jackiewicz <k.jackiewicz@samsung.com> | 2019-03-27 15:44:39 +0000 |
commit | 7bc501bef926142ed84d330749608e05ab55facb (patch) | |
tree | 113effeb0cb4bcdaa49dfcf3902f88747ad9de98 /src | |
parent | f46ff380dc48eb30db6ee75d510c655c8e5aaa34 (diff) | |
download | key-manager-7bc501bef926142ed84d330749608e05ab55facb.tar.gz key-manager-7bc501bef926142ed84d330749608e05ab55facb.tar.bz2 key-manager-7bc501bef926142ed84d330749608e05ab55facb.zip |
Check fs errors before saving the file
GetFd(os) on a non-existing file causes segfault.
Change-Id: I8365dfbddace160ae99b1e7d1f6070ee1032f6cd
Diffstat (limited to 'src')
-rw-r--r-- | src/manager/service/file-system.cpp | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/src/manager/service/file-system.cpp b/src/manager/service/file-system.cpp index 6a70c206..079a4cd8 100644 --- a/src/manager/service/file-system.cpp +++ b/src/manager/service/file-system.cpp @@ -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()) |