Shaka Packager SDK
file.h
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #ifndef PACKAGER_FILE_FILE_H_
8 #define PACKAGER_FILE_FILE_H_
9 
10 #include <stdint.h>
11 
12 #include <string>
13 
14 #include "packager/base/macros.h"
15 #include "packager/file/public/buffer_callback_params.h"
16 
17 namespace shaka {
18 
19 extern const char* kCallbackFilePrefix;
20 extern const char* kLocalFilePrefix;
21 extern const char* kMemoryFilePrefix;
22 extern const char* kUdpFilePrefix;
23 extern const char* kHttpFilePrefix;
24 const int64_t kWholeFile = -1;
25 
27 class File {
28  public:
35  static File* Open(const char* file_name, const char* mode);
36 
43  static File* OpenWithNoBuffering(const char* file_name, const char* mode);
44 
48  static bool Delete(const char* file_name);
49 
55  virtual bool Close() = 0;
56 
63  virtual int64_t Read(void* buffer, uint64_t length) = 0;
64 
69  virtual int64_t Write(const void* buffer, uint64_t length) = 0;
70 
73  virtual int64_t Size() = 0;
74 
80  virtual bool Flush() = 0;
81 
85  virtual bool Seek(uint64_t position) = 0;
86 
91  virtual bool Tell(uint64_t* position) = 0;
92 
95  const std::string& file_name() const { return file_name_; }
96 
97  // ************************************************************
98  // * Static Methods: File-on-the-filesystem status
99  // ************************************************************
100 
103  static int64_t GetFileSize(const char* file_name);
104 
109  static bool ReadFileToString(const char* file_name, std::string* contents);
110 
115  static bool WriteStringToFile(const char* file_name,
116  const std::string& contents);
117 
122  static bool WriteFileAtomically(const char* file_name,
123  const std::string& contents);
124 
131  static bool Copy(const char* from_file_name, const char* to_file_name);
132 
137  static int64_t CopyFile(File* source, File* destination);
138 
144  static int64_t CopyFile(File* source, File* destination, int64_t max_copy);
145 
148  static bool IsLocalRegularFile(const char* file_name);
149 
156  static std::string MakeCallbackFileName(
157  const BufferCallbackParams& callback_params,
158  const std::string& name);
159 
166  static bool ParseCallbackFileName(
167  const std::string& callback_file_name,
168  const BufferCallbackParams** callback_params,
169  std::string* name);
170 
171  protected:
172  explicit File(const std::string& file_name) : file_name_(file_name) {}
175  virtual ~File() {}
176 
178  virtual bool Open() = 0;
179 
180  private:
181  friend class ThreadedIoFile;
182 
183  // This is a file factory method, it creates a proper file, e.g.
184  // LocalFile, MemFile based on prefix.
185  static File* Create(const char* file_name, const char* mode);
186 
187  static File* CreateInternalFile(const char* file_name, const char* mode);
188 
189  // Note that the file type prefix has been stripped off.
190  std::string file_name_;
191 
192  DISALLOW_COPY_AND_ASSIGN(File);
193 };
194 
195 } // namespace shaka
196 
197 #endif // PACKAGER_FILE_FILE_H_
Define an abstract file interface.
Definition: file.h:27
virtual bool Tell(uint64_t *position)=0
static File * OpenWithNoBuffering(const char *file_name, const char *mode)
Definition: file.cc:201
static bool Delete(const char *file_name)
Definition: file.cc:212
static bool WriteStringToFile(const char *file_name, const std::string &contents)
Definition: file.cc:248
virtual bool Seek(uint64_t position)=0
const std::string & file_name() const
Definition: file.h:95
static bool WriteFileAtomically(const char *file_name, const std::string &contents)
Definition: file.cc:277
virtual bool Flush()=0
virtual bool Open()=0
Internal open. Should not be used directly.
virtual int64_t Read(void *buffer, uint64_t length)=0
static std::string MakeCallbackFileName(const BufferCallbackParams &callback_params, const std::string &name)
Definition: file.cc:399
static int64_t CopyFile(File *source, File *destination)
Definition: file.cc:333
virtual int64_t Write(const void *buffer, uint64_t length)=0
static bool IsLocalRegularFile(const char *file_name)
Definition: file.cc:374
virtual ~File()
Definition: file.h:175
virtual bool Close()=0
static int64_t GetFileSize(const char *file_name)
Definition: file.cc:221
static bool ReadFileToString(const char *file_name, std::string *contents)
Definition: file.cc:230
static bool ParseCallbackFileName(const std::string &callback_file_name, const BufferCallbackParams **callback_params, std::string *name)
Definition: file.cc:409
virtual int64_t Size()=0
static bool Copy(const char *from_file_name, const char *to_file_name)
Definition: file.cc:297
Declaration of class which implements a thread-safe circular buffer.
All the methods that are virtual are virtual for mocking.