1 // Copyright 2016 The Chromium OS Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "file_utils.h" 6 7 #include <sys/stat.h> 8 9 #include "base/logging.h" 10 #include "file_reader.h" 11 12 namespace quipper { 13 FileToBuffer(const string & filename,std::vector<char> * contents)14bool FileToBuffer(const string& filename, std::vector<char>* contents) { 15 FileReader reader(filename); 16 if (!reader.IsOpen()) return false; 17 size_t file_size = reader.size(); 18 contents->resize(file_size); 19 // Do not read anything if the file exists but is empty. 20 if (file_size > 0 && !reader.ReadData(file_size, contents->data())) { 21 LOG(ERROR) << "Failed to read " << file_size << " bytes from file " 22 << filename << ", only read " << reader.Tell(); 23 return false; 24 } 25 return true; 26 } 27 FileExists(const string & filename)28bool FileExists(const string& filename) { 29 struct stat st; 30 return stat(filename.c_str(), &st) == 0; 31 } 32 33 } // namespace quipper 34