blob: ecaaae496f449b9a28950fb78665e3947953bca7 [file] [log] [blame]
Christopher Wiley4a2884b2015-10-07 11:27:45 -07001/*
2 * Copyright (C) 2015, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "io_delegate.h"
18
Christopher Wiley054afbd2015-10-16 17:08:43 -070019#include <cstring>
Christopher Wiley4a2884b2015-10-07 11:27:45 -070020#include <fstream>
Christopher Wiley054afbd2015-10-16 17:08:43 -070021#include <vector>
22
23#ifdef _WIN32
24#include <direct.h>
25#else
26#include <sys/stat.h>
27#endif
28
29#include "logging.h"
30#include "os.h"
Christopher Wiley4a2884b2015-10-07 11:27:45 -070031
32using std::string;
33using std::unique_ptr;
Christopher Wiley054afbd2015-10-16 17:08:43 -070034using std::vector;
Christopher Wiley4a2884b2015-10-07 11:27:45 -070035
36namespace android {
37namespace aidl {
38
39unique_ptr<string> IoDelegate::GetFileContents(
40 const string& filename,
41 const string& content_suffix) const {
42 unique_ptr<string> contents;
43 std::ifstream in(filename, std::ios::in | std::ios::binary);
44 if (!in) {
45 return contents;
46 }
47 contents.reset(new string);
48 in.seekg(0, std::ios::end);
49 ssize_t file_size = in.tellg();
50 contents->resize(file_size + content_suffix.length());
51 in.seekg(0, std::ios::beg);
52 // Read the file contents into the beginning of the string
53 in.read(&(*contents)[0], file_size);
54 // Drop the suffix in at the end.
55 contents->replace(file_size, content_suffix.length(), content_suffix);
56 in.close();
57
58 return contents;
59}
60
Christopher Wiley72877ac2015-10-06 14:41:42 -070061bool IoDelegate::FileIsReadable(const string& path) const {
62#ifdef _WIN32
63 // check that the file exists and is not write-only
64 return (0 == _access(path.c_str(), 0)) && // mode 0=exist
65 (0 == _access(path.c_str(), 4)); // mode 4=readable
66#else
67 return (0 == access(path.c_str(), R_OK));
68#endif
69}
Christopher Wiley054afbd2015-10-16 17:08:43 -070070
71bool IoDelegate::CreatedNestedDirs(
72 const string& caller_base_dir,
73 const vector<string>& nested_subdirs) const {
74 string base_dir = caller_base_dir;
75 if (base_dir.empty()) {
76 base_dir = ".";
77 }
78 for (const string& subdir : nested_subdirs) {
79 if (base_dir[base_dir.size() - 1] != OS_PATH_SEPARATOR) {
80 base_dir += OS_PATH_SEPARATOR;
81 }
82 base_dir += subdir;
83 bool success;
84#ifdef _WIN32
85 success = _mkdir(base_dir.c_str()) == 0;
86#else
87 success = mkdir(base_dir.c_str(),
88 S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0;
89#endif
90 if (!success && errno != EEXIST) {
91 LOG(ERROR) << "Error while creating directories: " << strerror(errno);
92 return false;
93 }
94 }
95 return true;
96}
97
98unique_ptr<CodeWriter> IoDelegate::GetCodeWriter(
99 const string& file_path) const {
100 return GetFileWriter(file_path);
101}
102
Christopher Wiley4a2884b2015-10-07 11:27:45 -0700103} // namespace android
104} // namespace aidl