blob: 80c9edf7c89343b0932d74ce4785a8bae30a09d5 [file] [log] [blame]
tkchin93411912015-07-22 12:12:17 -07001/*
2 * Copyright 2015 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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "rtc_base/file_rotating_stream.h"
tkchin93411912015-07-22 12:12:17 -070012
Jonas Olsson55378f42018-05-25 10:23:10 +020013#include <cstdio>
tkchin93411912015-07-22 12:12:17 -070014#include <string>
Yves Gerey988cc082018-10-23 12:03:01 +020015#include <utility>
tkchin93411912015-07-22 12:12:17 -070016
Niels Möller7b3c76b2018-11-07 09:54:28 +010017#if defined(WEBRTC_WIN)
18#include <windows.h>
Steve Anton10542f22019-01-11 09:11:00 -080019#include "rtc_base/string_utils.h"
Niels Möller7b3c76b2018-11-07 09:54:28 +010020#else
Niels Möller260770c2018-11-07 15:08:18 +010021#include <dirent.h>
Niels Möller7b3c76b2018-11-07 09:54:28 +010022#include <sys/stat.h>
Niels Möllerb7396662018-11-13 11:55:19 +010023#include <unistd.h>
Niels Möller7b3c76b2018-11-07 09:54:28 +010024#endif // WEBRTC_WIN
25
Steve Anton2acd1632019-03-25 13:48:30 -070026#include "absl/algorithm/container.h"
Niels Möller7b3c76b2018-11-07 09:54:28 +010027#include "absl/strings/match.h"
Yves Gerey3e707812018-11-28 16:47:49 +010028#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/checks.h"
Yves Gerey2e00abc2018-10-05 15:39:24 +020030#include "rtc_base/logging.h"
tkchin93411912015-07-22 12:12:17 -070031
Jonas Olsson55378f42018-05-25 10:23:10 +020032// Note: We use fprintf for logging in the write paths of this stream to avoid
tkchin93411912015-07-22 12:12:17 -070033// infinite loops when logging.
34
35namespace rtc {
36
Niels Möller7b3c76b2018-11-07 09:54:28 +010037namespace {
38
Niels Möllerd9ac0582019-01-03 14:21:38 +010039const char kCallSessionLogPrefix[] = "webrtc_log";
40
Niels Möller7b3c76b2018-11-07 09:54:28 +010041std::string AddTrailingPathDelimiterIfNeeded(std::string directory);
Niels Möller260770c2018-11-07 15:08:18 +010042
43// |dir| must have a trailing delimiter. |prefix| must not include wild card
44// characters.
45std::vector<std::string> GetFilesWithPrefix(const std::string& directory,
46 const std::string& prefix);
47bool DeleteFile(const std::string& file);
48bool MoveFile(const std::string& old_file, const std::string& new_file);
49bool IsFile(const std::string& file);
Niels Möller7b3c76b2018-11-07 09:54:28 +010050bool IsFolder(const std::string& file);
Niels Möller260770c2018-11-07 15:08:18 +010051absl::optional<size_t> GetFileSize(const std::string& file);
Niels Möller7b3c76b2018-11-07 09:54:28 +010052
53#if defined(WEBRTC_WIN)
54
55std::string AddTrailingPathDelimiterIfNeeded(std::string directory) {
56 if (absl::EndsWith(directory, "\\")) {
57 return directory;
58 }
59 return directory + "\\";
60}
61
Niels Möller260770c2018-11-07 15:08:18 +010062std::vector<std::string> GetFilesWithPrefix(const std::string& directory,
63 const std::string& prefix) {
64 RTC_DCHECK(absl::EndsWith(directory, "\\"));
65 WIN32_FIND_DATA data;
66 HANDLE handle;
67 handle = ::FindFirstFile(ToUtf16(directory + prefix + '*').c_str(), &data);
68 if (handle == INVALID_HANDLE_VALUE)
69 return {};
70
71 std::vector<std::string> file_list;
72 do {
73 file_list.emplace_back(directory + ToUtf8(data.cFileName));
74 } while (::FindNextFile(handle, &data) == TRUE);
75
76 ::FindClose(handle);
77 return file_list;
78}
79
80bool DeleteFile(const std::string& file) {
81 return ::DeleteFile(ToUtf16(file).c_str()) != 0;
82}
83
84bool MoveFile(const std::string& old_file, const std::string& new_file) {
85 return ::MoveFile(ToUtf16(old_file).c_str(), ToUtf16(new_file).c_str()) != 0;
86}
87
88bool IsFile(const std::string& file) {
89 WIN32_FILE_ATTRIBUTE_DATA data = {0};
90 if (0 == ::GetFileAttributesEx(ToUtf16(file).c_str(), GetFileExInfoStandard,
91 &data))
92 return false;
93 return (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0;
94}
95
Niels Möller7b3c76b2018-11-07 09:54:28 +010096bool IsFolder(const std::string& file) {
97 WIN32_FILE_ATTRIBUTE_DATA data = {0};
98 if (0 == ::GetFileAttributesEx(ToUtf16(file).c_str(), GetFileExInfoStandard,
99 &data))
100 return false;
101 return (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ==
102 FILE_ATTRIBUTE_DIRECTORY;
103}
104
Niels Möller260770c2018-11-07 15:08:18 +0100105absl::optional<size_t> GetFileSize(const std::string& file) {
106 WIN32_FILE_ATTRIBUTE_DATA data = {0};
107 if (::GetFileAttributesEx(ToUtf16(file).c_str(), GetFileExInfoStandard,
108 &data) == 0)
109 return absl::nullopt;
110 return data.nFileSizeLow;
111}
112
Niels Möller7b3c76b2018-11-07 09:54:28 +0100113#else // defined(WEBRTC_WIN)
114
115std::string AddTrailingPathDelimiterIfNeeded(std::string directory) {
116 if (absl::EndsWith(directory, "/")) {
117 return directory;
118 }
119 return directory + "/";
120}
121
Niels Möller260770c2018-11-07 15:08:18 +0100122std::vector<std::string> GetFilesWithPrefix(const std::string& directory,
123 const std::string& prefix) {
124 RTC_DCHECK(absl::EndsWith(directory, "/"));
125 DIR* dir = ::opendir(directory.c_str());
126 if (dir == nullptr)
127 return {};
128 std::vector<std::string> file_list;
129 for (struct dirent* dirent = ::readdir(dir); dirent;
130 dirent = ::readdir(dir)) {
131 std::string name = dirent->d_name;
132 if (name.compare(0, prefix.size(), prefix) == 0) {
133 file_list.emplace_back(directory + name);
134 }
135 }
136 ::closedir(dir);
137 return file_list;
138}
139
140bool DeleteFile(const std::string& file) {
141 return ::unlink(file.c_str()) == 0;
142}
143
144bool MoveFile(const std::string& old_file, const std::string& new_file) {
145 return ::rename(old_file.c_str(), new_file.c_str()) == 0;
146}
147
148bool IsFile(const std::string& file) {
149 struct stat st;
150 int res = ::stat(file.c_str(), &st);
151 // Treat symlinks, named pipes, etc. all as files.
152 return res == 0 && !S_ISDIR(st.st_mode);
153}
154
Niels Möller7b3c76b2018-11-07 09:54:28 +0100155bool IsFolder(const std::string& file) {
156 struct stat st;
157 int res = ::stat(file.c_str(), &st);
158 return res == 0 && S_ISDIR(st.st_mode);
159}
160
Niels Möller260770c2018-11-07 15:08:18 +0100161absl::optional<size_t> GetFileSize(const std::string& file) {
162 struct stat st;
163 if (::stat(file.c_str(), &st) != 0)
164 return absl::nullopt;
165 return st.st_size;
166}
167
Niels Möller7b3c76b2018-11-07 09:54:28 +0100168#endif
169
170} // namespace
171
tkchin93411912015-07-22 12:12:17 -0700172FileRotatingStream::FileRotatingStream(const std::string& dir_path,
tkchin93411912015-07-22 12:12:17 -0700173 const std::string& file_prefix,
174 size_t max_file_size,
175 size_t num_files)
Niels Möller7b3c76b2018-11-07 09:54:28 +0100176 : dir_path_(AddTrailingPathDelimiterIfNeeded(dir_path)),
tkchin93411912015-07-22 12:12:17 -0700177 file_prefix_(file_prefix),
tkchin93411912015-07-22 12:12:17 -0700178 max_file_size_(max_file_size),
179 current_file_index_(0),
180 rotation_index_(0),
181 current_bytes_written_(0),
182 disable_buffering_(false) {
Niels Möller6ffe62a2019-01-08 13:22:57 +0100183 RTC_DCHECK_GT(max_file_size, 0);
184 RTC_DCHECK_GT(num_files, 1);
Niels Möller7b3c76b2018-11-07 09:54:28 +0100185 RTC_DCHECK(IsFolder(dir_path));
Niels Möller6ffe62a2019-01-08 13:22:57 +0100186 file_names_.clear();
187 for (size_t i = 0; i < num_files; ++i) {
188 file_names_.push_back(GetFilePath(i, num_files));
tkchin93411912015-07-22 12:12:17 -0700189 }
Niels Möller6ffe62a2019-01-08 13:22:57 +0100190 rotation_index_ = num_files - 1;
tkchin93411912015-07-22 12:12:17 -0700191}
192
Yves Gerey665174f2018-06-19 15:03:05 +0200193FileRotatingStream::~FileRotatingStream() {}
tkchin93411912015-07-22 12:12:17 -0700194
195StreamState FileRotatingStream::GetState() const {
Niels Möller23213d92019-01-22 11:01:24 +0100196 return (file_.is_open() ? SS_OPEN : SS_CLOSED);
tkchin93411912015-07-22 12:12:17 -0700197}
198
199StreamResult FileRotatingStream::Read(void* buffer,
200 size_t buffer_len,
201 size_t* read,
202 int* error) {
henrikg91d6ede2015-09-17 00:24:34 -0700203 RTC_DCHECK(buffer);
Niels Möller6ffe62a2019-01-08 13:22:57 +0100204 RTC_NOTREACHED();
205 return SR_EOS;
tkchin93411912015-07-22 12:12:17 -0700206}
207
208StreamResult FileRotatingStream::Write(const void* data,
209 size_t data_len,
210 size_t* written,
211 int* error) {
Niels Möller23213d92019-01-22 11:01:24 +0100212 if (!file_.is_open()) {
Jonas Olsson55378f42018-05-25 10:23:10 +0200213 std::fprintf(stderr, "Open() must be called before Write.\n");
tkchin93411912015-07-22 12:12:17 -0700214 return SR_ERROR;
215 }
216 // Write as much as will fit in to the current file.
henrikg91d6ede2015-09-17 00:24:34 -0700217 RTC_DCHECK_LT(current_bytes_written_, max_file_size_);
tkchin93411912015-07-22 12:12:17 -0700218 size_t remaining_bytes = max_file_size_ - current_bytes_written_;
219 size_t write_length = std::min(data_len, remaining_bytes);
tkchin93411912015-07-22 12:12:17 -0700220
Niels Möller23213d92019-01-22 11:01:24 +0100221 if (!file_.Write(data, write_length)) {
222 return SR_ERROR;
223 }
224 if (disable_buffering_ && !file_.Flush()) {
225 return SR_ERROR;
226 }
227
228 current_bytes_written_ += write_length;
229 if (written) {
230 *written = write_length;
231 }
tkchin93411912015-07-22 12:12:17 -0700232 // If we're done with this file, rotate it out.
233 if (current_bytes_written_ >= max_file_size_) {
henrikg91d6ede2015-09-17 00:24:34 -0700234 RTC_DCHECK_EQ(current_bytes_written_, max_file_size_);
tkchin93411912015-07-22 12:12:17 -0700235 RotateFiles();
236 }
Niels Möller23213d92019-01-22 11:01:24 +0100237 return SR_SUCCESS;
tkchin93411912015-07-22 12:12:17 -0700238}
239
240bool FileRotatingStream::Flush() {
Niels Möller23213d92019-01-22 11:01:24 +0100241 if (!file_.is_open()) {
tkchin93411912015-07-22 12:12:17 -0700242 return false;
243 }
Niels Möller23213d92019-01-22 11:01:24 +0100244 return file_.Flush();
tkchin93411912015-07-22 12:12:17 -0700245}
246
tkchin93411912015-07-22 12:12:17 -0700247void FileRotatingStream::Close() {
248 CloseCurrentFile();
249}
250
251bool FileRotatingStream::Open() {
Niels Möller6ffe62a2019-01-08 13:22:57 +0100252 // Delete existing files when opening for write.
253 std::vector<std::string> matching_files =
254 GetFilesWithPrefix(dir_path_, file_prefix_);
255 for (const auto& matching_file : matching_files) {
256 if (!DeleteFile(matching_file)) {
257 std::fprintf(stderr, "Failed to delete: %s\n", matching_file.c_str());
tkchin93411912015-07-22 12:12:17 -0700258 }
259 }
Niels Möller6ffe62a2019-01-08 13:22:57 +0100260 return OpenCurrentFile();
tkchin93411912015-07-22 12:12:17 -0700261}
262
263bool FileRotatingStream::DisableBuffering() {
264 disable_buffering_ = true;
Niels Möller23213d92019-01-22 11:01:24 +0100265 return true;
tkchin93411912015-07-22 12:12:17 -0700266}
267
268std::string FileRotatingStream::GetFilePath(size_t index) const {
henrikg91d6ede2015-09-17 00:24:34 -0700269 RTC_DCHECK_LT(index, file_names_.size());
tkchin93411912015-07-22 12:12:17 -0700270 return file_names_[index];
271}
272
273bool FileRotatingStream::OpenCurrentFile() {
274 CloseCurrentFile();
275
276 // Opens the appropriate file in the appropriate mode.
henrikg91d6ede2015-09-17 00:24:34 -0700277 RTC_DCHECK_LT(current_file_index_, file_names_.size());
tkchin93411912015-07-22 12:12:17 -0700278 std::string file_path = file_names_[current_file_index_];
Niels Möller6ffe62a2019-01-08 13:22:57 +0100279
280 // We should always be writing to the zero-th file.
281 RTC_DCHECK_EQ(current_file_index_, 0);
Niels Möller23213d92019-01-22 11:01:24 +0100282 int error;
283 file_ = webrtc::FileWrapper::OpenWriteOnly(file_path, &error);
284 if (!file_.is_open()) {
285 std::fprintf(stderr, "Failed to open: %s Error: %d\n", file_path.c_str(),
Jonas Olsson55378f42018-05-25 10:23:10 +0200286 error);
tkchin93411912015-07-22 12:12:17 -0700287 return false;
288 }
tkchin93411912015-07-22 12:12:17 -0700289 return true;
290}
291
292void FileRotatingStream::CloseCurrentFile() {
Niels Möller23213d92019-01-22 11:01:24 +0100293 if (!file_.is_open()) {
tkchin93411912015-07-22 12:12:17 -0700294 return;
295 }
296 current_bytes_written_ = 0;
Niels Möller23213d92019-01-22 11:01:24 +0100297 file_.Close();
tkchin93411912015-07-22 12:12:17 -0700298}
299
300void FileRotatingStream::RotateFiles() {
tkchin93411912015-07-22 12:12:17 -0700301 CloseCurrentFile();
302 // Rotates the files by deleting the file at |rotation_index_|, which is the
303 // oldest file and then renaming the newer files to have an incremented index.
304 // See header file comments for example.
hayscd02b0fa2015-12-08 13:59:05 -0800305 RTC_DCHECK_LT(rotation_index_, file_names_.size());
tkchin93411912015-07-22 12:12:17 -0700306 std::string file_to_delete = file_names_[rotation_index_];
Niels Möller260770c2018-11-07 15:08:18 +0100307 if (IsFile(file_to_delete)) {
308 if (!DeleteFile(file_to_delete)) {
Jonas Olsson55378f42018-05-25 10:23:10 +0200309 std::fprintf(stderr, "Failed to delete: %s\n", file_to_delete.c_str());
tkchin93411912015-07-22 12:12:17 -0700310 }
311 }
312 for (auto i = rotation_index_; i > 0; --i) {
313 std::string rotated_name = file_names_[i];
314 std::string unrotated_name = file_names_[i - 1];
Niels Möller260770c2018-11-07 15:08:18 +0100315 if (IsFile(unrotated_name)) {
316 if (!MoveFile(unrotated_name, rotated_name)) {
Jonas Olsson55378f42018-05-25 10:23:10 +0200317 std::fprintf(stderr, "Failed to move: %s to %s\n",
318 unrotated_name.c_str(), rotated_name.c_str());
tkchin93411912015-07-22 12:12:17 -0700319 }
320 }
321 }
322 // Create a new file for 0th index.
323 OpenCurrentFile();
324 OnRotation();
325}
326
tkchin93411912015-07-22 12:12:17 -0700327std::string FileRotatingStream::GetFilePath(size_t index,
328 size_t num_files) const {
henrikg91d6ede2015-09-17 00:24:34 -0700329 RTC_DCHECK_LT(index, num_files);
tkchin93411912015-07-22 12:12:17 -0700330
Jonas Olsson671cae22018-06-14 09:57:39 +0200331 const size_t buffer_size = 32;
332 char file_postfix[buffer_size];
333 // We want to zero pad the index so that it will sort nicely.
334 const int max_digits = std::snprintf(nullptr, 0, "%zu", num_files - 1);
335 RTC_DCHECK_LT(1 + max_digits, buffer_size);
336 std::snprintf(file_postfix, buffer_size, "_%0*zu", max_digits, index);
tkchin93411912015-07-22 12:12:17 -0700337
Niels Möller7b3c76b2018-11-07 09:54:28 +0100338 return dir_path_ + file_prefix_ + file_postfix;
tkchin93411912015-07-22 12:12:17 -0700339}
340
341CallSessionFileRotatingStream::CallSessionFileRotatingStream(
tkchin93411912015-07-22 12:12:17 -0700342 const std::string& dir_path,
343 size_t max_total_log_size)
344 : FileRotatingStream(dir_path,
Niels Möllerd9ac0582019-01-03 14:21:38 +0100345 kCallSessionLogPrefix,
tkchin93411912015-07-22 12:12:17 -0700346 max_total_log_size / 2,
347 GetNumRotatingLogFiles(max_total_log_size) + 1),
348 max_total_log_size_(max_total_log_size),
349 num_rotations_(0) {
kwibergaf476c72016-11-28 15:21:39 -0800350 RTC_DCHECK_GE(max_total_log_size, 4);
tkchin93411912015-07-22 12:12:17 -0700351}
352
tkchin93411912015-07-22 12:12:17 -0700353const size_t CallSessionFileRotatingStream::kRotatingLogFileDefaultSize =
354 1024 * 1024;
355
356void CallSessionFileRotatingStream::OnRotation() {
357 ++num_rotations_;
358 if (num_rotations_ == 1) {
359 // On the first rotation adjust the max file size so subsequent files after
360 // the first are smaller.
361 SetMaxFileSize(GetRotatingLogSize(max_total_log_size_));
362 } else if (num_rotations_ == (GetNumFiles() - 1)) {
363 // On the next rotation the very first file is going to be deleted. Change
364 // the rotation index so this doesn't happen.
365 SetRotationIndex(GetRotationIndex() - 1);
366 }
367}
368
369size_t CallSessionFileRotatingStream::GetRotatingLogSize(
370 size_t max_total_log_size) {
371 size_t num_rotating_log_files = GetNumRotatingLogFiles(max_total_log_size);
372 size_t rotating_log_size = num_rotating_log_files > 2
373 ? kRotatingLogFileDefaultSize
374 : max_total_log_size / 4;
375 return rotating_log_size;
376}
377
378size_t CallSessionFileRotatingStream::GetNumRotatingLogFiles(
379 size_t max_total_log_size) {
380 // At minimum have two rotating files. Otherwise split the available log size
381 // evenly across 1MB files.
382 return std::max((size_t)2,
383 (max_total_log_size / 2) / kRotatingLogFileDefaultSize);
384}
385
Niels Möllerd9ac0582019-01-03 14:21:38 +0100386FileRotatingStreamReader::FileRotatingStreamReader(
387 const std::string& dir_path,
388 const std::string& file_prefix) {
389 file_names_ = GetFilesWithPrefix(AddTrailingPathDelimiterIfNeeded(dir_path),
390 file_prefix);
391
392 // Plain sort of the file names would sort by age, i.e., oldest last. Using
393 // std::greater gives us the desired chronological older, oldest first.
Steve Anton2acd1632019-03-25 13:48:30 -0700394 absl::c_sort(file_names_, std::greater<std::string>());
Niels Möllerd9ac0582019-01-03 14:21:38 +0100395}
396
397FileRotatingStreamReader::~FileRotatingStreamReader() = default;
398
399size_t FileRotatingStreamReader::GetSize() const {
400 size_t total_size = 0;
401 for (const auto& file_name : file_names_) {
402 total_size += GetFileSize(file_name).value_or(0);
403 }
404 return total_size;
405}
406
407size_t FileRotatingStreamReader::ReadAll(void* buffer, size_t size) const {
408 size_t done = 0;
409 for (const auto& file_name : file_names_) {
410 if (done < size) {
Niels Möller23213d92019-01-22 11:01:24 +0100411 webrtc::FileWrapper f = webrtc::FileWrapper::OpenReadOnly(file_name);
412 if (!f.is_open()) {
Niels Möllerd9ac0582019-01-03 14:21:38 +0100413 break;
414 }
Niels Möller23213d92019-01-22 11:01:24 +0100415 done += f.Read(static_cast<char*>(buffer) + done, size - done);
Niels Möllerd9ac0582019-01-03 14:21:38 +0100416 } else {
417 break;
418 }
419 }
420 return done;
421}
422
423CallSessionFileRotatingStreamReader::CallSessionFileRotatingStreamReader(
424 const std::string& dir_path)
425 : FileRotatingStreamReader(dir_path, kCallSessionLogPrefix) {}
426
tkchin93411912015-07-22 12:12:17 -0700427} // namespace rtc