Shaka Player Embedded
file_system.cc
Go to the documentation of this file.
1 // Copyright 2017 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/util/file_system.h"
16 
17 #ifdef OS_IOS
18 # include <CoreFoundation/CoreFoundation.h>
19 #endif
20 
21 #include <glog/logging.h>
22 
23 #include <fstream>
24 
25 #include "src/util/cfref.h"
26 
27 namespace shaka {
28 namespace util {
29 
30 namespace {
31 
32 #ifdef OS_IOS
33 std::string GetBundleDir() {
34  CFRef<CFURLRef> url(CFBundleCopyBundleURL(CFBundleGetMainBundle()));
35  CFRef<CFStringRef> str(CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle));
36  return CFStringGetCStringPtr(str, CFStringGetSystemEncoding());
37 }
38 #endif
39 
40 } // namespace
41 
44 
45 // static
46 std::string FileSystem::GetPathForStaticFile(const std::string& static_data_dir,
47  bool is_bundle_relative, // NOLINT
48  const std::string& file) {
49 #ifdef OS_IOS
50  if (is_bundle_relative)
51  return PathJoin(PathJoin(GetBundleDir(), static_data_dir), file);
52 #endif
53  return PathJoin(static_data_dir, file);
54 }
55 
56 // static
58  const std::string& dynamic_data_dir, const std::string& file) {
59  return PathJoin(dynamic_data_dir, file);
60 }
61 
62 ssize_t FileSystem::FileSize(const std::string& path) const {
63  std::ifstream file(path, std::ios::binary | std::ios::in);
64  if (!file) {
65  PLOG(ERROR) << "Error opening file '" << path << "'";
66  return -1;
67  }
68 
69  // Get the total file size.
70  file.seekg(0, std::ios::end);
71  if (file.fail()) {
72  PLOG(ERROR) << "Error seeking in file";
73  return -1;
74  }
75 
76  return file.tellg();
77 }
78 
79 bool FileSystem::ReadFile(const std::string& path,
80  std::vector<uint8_t>* data) const {
81  std::ifstream file(path, std::ios::binary | std::ios::in);
82  if (!file) {
83  PLOG(ERROR) << "Error opening file '" << path << "'";
84  return false;
85  }
86 
87  // Get the total file size.
88  file.seekg(0, std::ios::end);
89  CHECK(!file.fail());
90  const std::streamsize file_size = file.tellg();
91  file.seekg(0);
92  DCHECK(file);
93 
94  data->resize(file_size);
95  file.read(reinterpret_cast<char*>(data->data()), file_size);
96  if (!file) {
97  PLOG(ERROR) << "Error reading file '" << path << "'";
98  return false;
99  }
100  DCHECK_EQ(file_size, file.gcount());
101 
102  return true;
103 }
104 
105 bool FileSystem::WriteFile(const std::string& path,
106  const std::vector<uint8_t>& data) const {
107  std::ofstream file(path, std::ios::binary | std::ios::out | std::ios::trunc);
108  if (!file) {
109  PLOG(ERROR) << "Error opening file '" << path << "'";
110  return false;
111  }
112 
113  // Write will set file.bad() if it doesn't write all the characters.
114  file.write(reinterpret_cast<const char*>(data.data()), data.size());
115  if (!file) {
116  PLOG(ERROR) << "Error writing file '" << path << "'";
117  return false;
118  }
119  DCHECK_EQ(static_cast<std::streamsize>(data.size()), file.tellp());
120 
121  return true;
122 }
123 
124 } // namespace util
125 } // namespace shaka
static std::string GetPathForStaticFile(const std::string &static_data_dir, bool is_bundle_relative, const std::string &file)
Definition: file_system.cc:46
virtual MUST_USE_RESULT bool WriteFile(const std::string &path, const std::vector< uint8_t > &data) const
Definition: file_system.cc:105
static std::string GetPathForDynamicFile(const std::string &dynamic_data_dir, const std::string &file)
Definition: file_system.cc:57
virtual ssize_t FileSize(const std::string &path) const
Definition: file_system.cc:62
virtual MUST_USE_RESULT bool ReadFile(const std::string &path, std::vector< uint8_t > *data) const
Definition: file_system.cc:79
static std::string PathJoin(const std::string &a, const std::string &b)