From 83c4b2792b9aaa2a579011c273f92934b71e9f27 Mon Sep 17 00:00:00 2001 From: Takeo Sawada Date: Wed, 8 May 2019 15:19:12 +0900 Subject: [PATCH] PosixFileSystem::CopyFile: Truncate file before overwrite Without O_TRUNC, the original content of the target file will be left over if the src file is shorter. --- tensorflow/core/platform/posix/posix_file_system.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tensorflow/core/platform/posix/posix_file_system.cc b/tensorflow/core/platform/posix/posix_file_system.cc index 435dbc522d0..f8774a69bb9 100644 --- a/tensorflow/core/platform/posix/posix_file_system.cc +++ b/tensorflow/core/platform/posix/posix_file_system.cc @@ -333,11 +333,13 @@ Status PosixFileSystem::CopyFile(const string& src, const string& target) { return IOError(src, errno); } string translated_target = TranslateName(target); - // O_WRONLY | O_CREAT: + // O_WRONLY | O_CREAT | O_TRUNC: // Open file for write and if file does not exist, create the file. + // If file exists, truncate its size to 0. // When creating file, use the same permissions as original mode_t mode = sbuf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); - int target_fd = open(translated_target.c_str(), O_WRONLY | O_CREAT, mode); + int target_fd = + open(translated_target.c_str(), O_WRONLY | O_CREAT | O_TRUNC, mode); if (target_fd < 0) { close(src_fd); return IOError(target, errno);