1 /* 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef RTC_BASE_SYSTEM_FILE_WRAPPER_H_ 12 #define RTC_BASE_SYSTEM_FILE_WRAPPER_H_ 13 14 #include <stddef.h> 15 #include <stdio.h> 16 17 #include <string> 18 19 #include "absl/strings/string_view.h" 20 21 // Implementation that can read (exclusive) or write from/to a file. 22 23 namespace webrtc { 24 25 // This class is a thin wrapper around FILE*. It's main features are that it 26 // owns the FILE*, calling fclose on destruction, and that on windows, file 27 // names passed to the open methods are always treated as utf-8, regardless of 28 // system code page. 29 30 // Most of the methods return only a success/fail indication. When needed, an 31 // optional argument |int* error| should be added to all methods, in the same 32 // way as for the OpenWriteOnly methods. 33 class FileWrapper final { 34 public: 35 // Opens a file, in read or write mode. Use the is_open() method on the 36 // returned object to check if the open operation was successful. On failure, 37 // and if `error` is non-null, the system errno value is stored at |*error|. 38 // The file is closed by the destructor. 39 static FileWrapper OpenReadOnly(absl::string_view file_name_utf8); 40 static FileWrapper OpenWriteOnly(absl::string_view file_name_utf8, 41 int* error = nullptr); 42 43 FileWrapper() = default; 44 45 // Takes over ownership of `file`, closing it on destruction. Calling with 46 // null `file` is allowed, and results in a FileWrapper with is_open() false. FileWrapper(FILE * file)47 explicit FileWrapper(FILE* file) : file_(file) {} ~FileWrapper()48 ~FileWrapper() { Close(); } 49 50 // Copying is not supported. 51 FileWrapper(const FileWrapper&) = delete; 52 FileWrapper& operator=(const FileWrapper&) = delete; 53 54 // Support for move semantics. 55 FileWrapper(FileWrapper&&); 56 FileWrapper& operator=(FileWrapper&&); 57 58 // Returns true if a file has been opened. If the file is not open, no methods 59 // but is_open and Close may be called. is_open()60 bool is_open() const { return file_ != nullptr; } 61 62 // Closes the file, and implies Flush. Returns true on success, false if 63 // writing buffered data fails. On failure, the file is nevertheless closed. 64 // Calling Close on an already closed file does nothing and returns success. 65 bool Close(); 66 67 // Releases and returns the wrapped file without closing it. This call passes 68 // the ownership of the file to the caller, and the wrapper is no longer 69 // responsible for closing it. Similarly the previously wrapped file is no 70 // longer available for the wrapper to use in any aspect. 71 FILE* Release(); 72 73 // Write any buffered data to the underlying file. Returns true on success, 74 // false on write error. Note: Flushing when closing, is not required. 75 bool Flush(); 76 77 // Seeks to the beginning of file. Returns true on success, false on failure, 78 // e.g., if the underlying file isn't seekable. Rewind()79 bool Rewind() { return SeekTo(0); } 80 // TODO(nisse): The seek functions are used only by the WavReader. If that 81 // code is demoted to test code, seek functions can be deleted from this 82 // utility. 83 // Seek relative to current file position. 84 bool SeekRelative(int64_t offset); 85 // Seek to given position. 86 bool SeekTo(int64_t position); 87 88 // Returns the file size or -1 if a size could not be determined. 89 // (A file size might not exists for non-seekable files or file-like 90 // objects, for example /dev/tty on unix.) 91 long FileSize(); 92 93 // Returns number of bytes read. Short count indicates EOF or error. 94 size_t Read(void* buf, size_t length); 95 96 // If the most recent Read() returned a short count, this methods returns true 97 // if the short count was due to EOF, and false it it was due to some i/o 98 // error. 99 bool ReadEof() const; 100 101 // Returns true if all data was successfully written (or buffered), or false 102 // if there was an error. Writing buffered data can fail later, and is 103 // reported with return value from Flush or Close. 104 bool Write(const void* buf, size_t length); 105 106 private: 107 FILE* file_ = nullptr; 108 }; 109 110 } // namespace webrtc 111 112 #endif // RTC_BASE_SYSTEM_FILE_WRAPPER_H_ 113