blob: 325c6bbab0e378d09d67117d70b78c60038c5ccf [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/filerotatingstream.h"
tkchin93411912015-07-22 12:12:17 -070012
13#include <algorithm>
Jonas Olsson55378f42018-05-25 10:23:10 +020014#include <cstdio>
tkchin93411912015-07-22 12:12:17 -070015#include <string>
Yves Gerey988cc082018-10-23 12:03:01 +020016#include <utility>
tkchin93411912015-07-22 12:12:17 -070017
Niels Möller7b3c76b2018-11-07 09:54:28 +010018#if defined(WEBRTC_WIN)
19#include <windows.h>
20#include "rtc_base/stringutils.h"
21#else
Niels Möller260770c2018-11-07 15:08:18 +010022#include <dirent.h>
Niels Möller7b3c76b2018-11-07 09:54:28 +010023#include <sys/stat.h>
Niels Möllerb7396662018-11-13 11:55:19 +010024#include <unistd.h>
Niels Möller7b3c76b2018-11-07 09:54:28 +010025#endif // WEBRTC_WIN
26
27#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
39std::string AddTrailingPathDelimiterIfNeeded(std::string directory);
Niels Möller260770c2018-11-07 15:08:18 +010040
41// |dir| must have a trailing delimiter. |prefix| must not include wild card
42// characters.
43std::vector<std::string> GetFilesWithPrefix(const std::string& directory,
44 const std::string& prefix);
45bool DeleteFile(const std::string& file);
46bool MoveFile(const std::string& old_file, const std::string& new_file);
47bool IsFile(const std::string& file);
Niels Möller7b3c76b2018-11-07 09:54:28 +010048bool IsFolder(const std::string& file);
Niels Möller260770c2018-11-07 15:08:18 +010049absl::optional<size_t> GetFileSize(const std::string& file);
Niels Möller7b3c76b2018-11-07 09:54:28 +010050
51#if defined(WEBRTC_WIN)
52
53std::string AddTrailingPathDelimiterIfNeeded(std::string directory) {
54 if (absl::EndsWith(directory, "\\")) {
55 return directory;
56 }
57 return directory + "\\";
58}
59
Niels Möller260770c2018-11-07 15:08:18 +010060std::vector<std::string> GetFilesWithPrefix(const std::string& directory,
61 const std::string& prefix) {
62 RTC_DCHECK(absl::EndsWith(directory, "\\"));
63 WIN32_FIND_DATA data;
64 HANDLE handle;
65 handle = ::FindFirstFile(ToUtf16(directory + prefix + '*').c_str(), &data);
66 if (handle == INVALID_HANDLE_VALUE)
67 return {};
68
69 std::vector<std::string> file_list;
70 do {
71 file_list.emplace_back(directory + ToUtf8(data.cFileName));
72 } while (::FindNextFile(handle, &data) == TRUE);
73
74 ::FindClose(handle);
75 return file_list;
76}
77
78bool DeleteFile(const std::string& file) {
79 return ::DeleteFile(ToUtf16(file).c_str()) != 0;
80}
81
82bool MoveFile(const std::string& old_file, const std::string& new_file) {
83 return ::MoveFile(ToUtf16(old_file).c_str(), ToUtf16(new_file).c_str()) != 0;
84}
85
86bool IsFile(const std::string& file) {
87 WIN32_FILE_ATTRIBUTE_DATA data = {0};
88 if (0 == ::GetFileAttributesEx(ToUtf16(file).c_str(), GetFileExInfoStandard,
89 &data))
90 return false;
91 return (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0;
92}
93
Niels Möller7b3c76b2018-11-07 09:54:28 +010094bool IsFolder(const std::string& file) {
95 WIN32_FILE_ATTRIBUTE_DATA data = {0};
96 if (0 == ::GetFileAttributesEx(ToUtf16(file).c_str(), GetFileExInfoStandard,
97 &data))
98 return false;
99 return (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ==
100 FILE_ATTRIBUTE_DIRECTORY;
101}
102
Niels Möller260770c2018-11-07 15:08:18 +0100103absl::optional<size_t> GetFileSize(const std::string& file) {
104 WIN32_FILE_ATTRIBUTE_DATA data = {0};
105 if (::GetFileAttributesEx(ToUtf16(file).c_str(), GetFileExInfoStandard,
106 &data) == 0)
107 return absl::nullopt;
108 return data.nFileSizeLow;
109}
110
Niels Möller7b3c76b2018-11-07 09:54:28 +0100111#else // defined(WEBRTC_WIN)
112
113std::string AddTrailingPathDelimiterIfNeeded(std::string directory) {
114 if (absl::EndsWith(directory, "/")) {
115 return directory;
116 }
117 return directory + "/";
118}
119
Niels Möller260770c2018-11-07 15:08:18 +0100120std::vector<std::string> GetFilesWithPrefix(const std::string& directory,
121 const std::string& prefix) {
122 RTC_DCHECK(absl::EndsWith(directory, "/"));
123 DIR* dir = ::opendir(directory.c_str());
124 if (dir == nullptr)
125 return {};
126 std::vector<std::string> file_list;
127 for (struct dirent* dirent = ::readdir(dir); dirent;
128 dirent = ::readdir(dir)) {
129 std::string name = dirent->d_name;
130 if (name.compare(0, prefix.size(), prefix) == 0) {
131 file_list.emplace_back(directory + name);
132 }
133 }
134 ::closedir(dir);
135 return file_list;
136}
137
138bool DeleteFile(const std::string& file) {
139 return ::unlink(file.c_str()) == 0;
140}
141
142bool MoveFile(const std::string& old_file, const std::string& new_file) {
143 return ::rename(old_file.c_str(), new_file.c_str()) == 0;
144}
145
146bool IsFile(const std::string& file) {
147 struct stat st;
148 int res = ::stat(file.c_str(), &st);
149 // Treat symlinks, named pipes, etc. all as files.
150 return res == 0 && !S_ISDIR(st.st_mode);
151}
152
Niels Möller7b3c76b2018-11-07 09:54:28 +0100153bool IsFolder(const std::string& file) {
154 struct stat st;
155 int res = ::stat(file.c_str(), &st);
156 return res == 0 && S_ISDIR(st.st_mode);
157}
158
Niels Möller260770c2018-11-07 15:08:18 +0100159absl::optional<size_t> GetFileSize(const std::string& file) {
160 struct stat st;
161 if (::stat(file.c_str(), &st) != 0)
162 return absl::nullopt;
163 return st.st_size;
164}
165
Niels Möller7b3c76b2018-11-07 09:54:28 +0100166#endif
167
168} // namespace
169
tkchin93411912015-07-22 12:12:17 -0700170FileRotatingStream::FileRotatingStream(const std::string& dir_path,
171 const std::string& file_prefix)
Yves Gerey665174f2018-06-19 15:03:05 +0200172 : FileRotatingStream(dir_path, file_prefix, 0, 0, kRead) {}
tkchin93411912015-07-22 12:12:17 -0700173
174FileRotatingStream::FileRotatingStream(const std::string& dir_path,
175 const std::string& file_prefix,
176 size_t max_file_size,
177 size_t num_files)
178 : FileRotatingStream(dir_path,
179 file_prefix,
180 max_file_size,
181 num_files,
182 kWrite) {
kwibergaf476c72016-11-28 15:21:39 -0800183 RTC_DCHECK_GT(max_file_size, 0);
184 RTC_DCHECK_GT(num_files, 1);
tkchin93411912015-07-22 12:12:17 -0700185}
186
187FileRotatingStream::FileRotatingStream(const std::string& dir_path,
188 const std::string& file_prefix,
189 size_t max_file_size,
190 size_t num_files,
191 Mode mode)
Niels Möller7b3c76b2018-11-07 09:54:28 +0100192 : dir_path_(AddTrailingPathDelimiterIfNeeded(dir_path)),
tkchin93411912015-07-22 12:12:17 -0700193 file_prefix_(file_prefix),
194 mode_(mode),
195 file_stream_(nullptr),
196 max_file_size_(max_file_size),
197 current_file_index_(0),
198 rotation_index_(0),
199 current_bytes_written_(0),
200 disable_buffering_(false) {
Niels Möller7b3c76b2018-11-07 09:54:28 +0100201 RTC_DCHECK(IsFolder(dir_path));
tkchin93411912015-07-22 12:12:17 -0700202 switch (mode) {
203 case kWrite: {
204 file_names_.clear();
205 for (size_t i = 0; i < num_files; ++i) {
206 file_names_.push_back(GetFilePath(i, num_files));
207 }
208 rotation_index_ = num_files - 1;
209 break;
210 }
211 case kRead: {
Niels Möller260770c2018-11-07 15:08:18 +0100212 file_names_ = GetFilesWithPrefix(dir_path_, file_prefix_);
tkchin93411912015-07-22 12:12:17 -0700213 std::sort(file_names_.begin(), file_names_.end());
214 if (file_names_.size() > 0) {
215 // |file_names_| is sorted newest first, so read from the end.
216 current_file_index_ = file_names_.size() - 1;
217 }
218 break;
219 }
220 }
221}
222
Yves Gerey665174f2018-06-19 15:03:05 +0200223FileRotatingStream::~FileRotatingStream() {}
tkchin93411912015-07-22 12:12:17 -0700224
225StreamState FileRotatingStream::GetState() const {
226 if (mode_ == kRead && current_file_index_ < file_names_.size()) {
227 return SS_OPEN;
228 }
229 if (!file_stream_) {
230 return SS_CLOSED;
231 }
232 return file_stream_->GetState();
233}
234
235StreamResult FileRotatingStream::Read(void* buffer,
236 size_t buffer_len,
237 size_t* read,
238 int* error) {
henrikg91d6ede2015-09-17 00:24:34 -0700239 RTC_DCHECK(buffer);
tkchin93411912015-07-22 12:12:17 -0700240 if (mode_ != kRead) {
241 return SR_EOS;
242 }
243 if (current_file_index_ >= file_names_.size()) {
244 return SR_EOS;
245 }
246 // We will have no file stream initially, and when we are finished with the
247 // previous file.
248 if (!file_stream_) {
249 if (!OpenCurrentFile()) {
250 return SR_ERROR;
251 }
252 }
253 int local_error = 0;
254 if (!error) {
255 error = &local_error;
256 }
257 StreamResult result = file_stream_->Read(buffer, buffer_len, read, error);
258 if (result == SR_EOS || result == SR_ERROR) {
259 if (result == SR_ERROR) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100260 RTC_LOG(LS_ERROR) << "Failed to read from: "
261 << file_names_[current_file_index_]
262 << "Error: " << error;
tkchin93411912015-07-22 12:12:17 -0700263 }
264 // Reached the end of the file, read next file. If there is an error return
265 // the error status but allow for a next read by reading next file.
266 CloseCurrentFile();
267 if (current_file_index_ == 0) {
268 // Just finished reading the last file, signal EOS by setting index.
269 current_file_index_ = file_names_.size();
270 } else {
271 --current_file_index_;
272 }
273 if (read) {
274 *read = 0;
275 }
276 return result == SR_EOS ? SR_SUCCESS : result;
277 } else if (result == SR_SUCCESS) {
278 // Succeeded, continue reading from this file.
279 return SR_SUCCESS;
280 } else {
281 RTC_NOTREACHED();
282 }
283 return result;
284}
285
286StreamResult FileRotatingStream::Write(const void* data,
287 size_t data_len,
288 size_t* written,
289 int* error) {
290 if (mode_ != kWrite) {
291 return SR_EOS;
292 }
293 if (!file_stream_) {
Jonas Olsson55378f42018-05-25 10:23:10 +0200294 std::fprintf(stderr, "Open() must be called before Write.\n");
tkchin93411912015-07-22 12:12:17 -0700295 return SR_ERROR;
296 }
297 // Write as much as will fit in to the current file.
henrikg91d6ede2015-09-17 00:24:34 -0700298 RTC_DCHECK_LT(current_bytes_written_, max_file_size_);
tkchin93411912015-07-22 12:12:17 -0700299 size_t remaining_bytes = max_file_size_ - current_bytes_written_;
300 size_t write_length = std::min(data_len, remaining_bytes);
301 size_t local_written = 0;
302 if (!written) {
303 written = &local_written;
304 }
305 StreamResult result = file_stream_->Write(data, write_length, written, error);
306 current_bytes_written_ += *written;
307
308 // If we're done with this file, rotate it out.
309 if (current_bytes_written_ >= max_file_size_) {
henrikg91d6ede2015-09-17 00:24:34 -0700310 RTC_DCHECK_EQ(current_bytes_written_, max_file_size_);
tkchin93411912015-07-22 12:12:17 -0700311 RotateFiles();
312 }
313 return result;
314}
315
316bool FileRotatingStream::Flush() {
317 if (!file_stream_) {
318 return false;
319 }
320 return file_stream_->Flush();
321}
322
tkchin28bae022015-07-23 12:27:02 -0700323bool FileRotatingStream::GetSize(size_t* size) const {
324 if (mode_ != kRead) {
325 // Not possible to get accurate size on disk when writing because of
326 // potential buffering.
327 return false;
328 }
henrikg91d6ede2015-09-17 00:24:34 -0700329 RTC_DCHECK(size);
tkchin28bae022015-07-23 12:27:02 -0700330 *size = 0;
331 size_t total_size = 0;
332 for (auto file_name : file_names_) {
Niels Möller260770c2018-11-07 15:08:18 +0100333 total_size += GetFileSize(file_name).value_or(0);
tkchin28bae022015-07-23 12:27:02 -0700334 }
335 *size = total_size;
336 return true;
337}
338
tkchin93411912015-07-22 12:12:17 -0700339void FileRotatingStream::Close() {
340 CloseCurrentFile();
341}
342
343bool FileRotatingStream::Open() {
344 switch (mode_) {
345 case kRead:
346 // Defer opening to when we first read since we want to return read error
347 // if we fail to open next file.
348 return true;
349 case kWrite: {
350 // Delete existing files when opening for write.
Niels Möller260770c2018-11-07 15:08:18 +0100351 std::vector<std::string> matching_files =
352 GetFilesWithPrefix(dir_path_, file_prefix_);
353 for (const auto& matching_file : matching_files) {
354 if (!DeleteFile(matching_file)) {
Jonas Olsson55378f42018-05-25 10:23:10 +0200355 std::fprintf(stderr, "Failed to delete: %s\n", matching_file.c_str());
tkchin93411912015-07-22 12:12:17 -0700356 }
357 }
358 return OpenCurrentFile();
359 }
360 }
361 return false;
362}
363
364bool FileRotatingStream::DisableBuffering() {
365 disable_buffering_ = true;
366 if (!file_stream_) {
Jonas Olsson55378f42018-05-25 10:23:10 +0200367 std::fprintf(stderr, "Open() must be called before DisableBuffering().\n");
tkchin93411912015-07-22 12:12:17 -0700368 return false;
369 }
370 return file_stream_->DisableBuffering();
371}
372
373std::string FileRotatingStream::GetFilePath(size_t index) const {
henrikg91d6ede2015-09-17 00:24:34 -0700374 RTC_DCHECK_LT(index, file_names_.size());
tkchin93411912015-07-22 12:12:17 -0700375 return file_names_[index];
376}
377
378bool FileRotatingStream::OpenCurrentFile() {
379 CloseCurrentFile();
380
381 // Opens the appropriate file in the appropriate mode.
henrikg91d6ede2015-09-17 00:24:34 -0700382 RTC_DCHECK_LT(current_file_index_, file_names_.size());
tkchin93411912015-07-22 12:12:17 -0700383 std::string file_path = file_names_[current_file_index_];
384 file_stream_.reset(new FileStream());
385 const char* mode = nullptr;
386 switch (mode_) {
387 case kWrite:
388 mode = "w+";
389 // We should always we writing to the zero-th file.
kwibergaf476c72016-11-28 15:21:39 -0800390 RTC_DCHECK_EQ(current_file_index_, 0);
tkchin93411912015-07-22 12:12:17 -0700391 break;
392 case kRead:
393 mode = "r";
394 break;
395 }
396 int error = 0;
397 if (!file_stream_->Open(file_path, mode, &error)) {
Jonas Olsson55378f42018-05-25 10:23:10 +0200398 std::fprintf(stderr, "Failed to open: %s Error: %i\n", file_path.c_str(),
399 error);
tkchin93411912015-07-22 12:12:17 -0700400 file_stream_.reset();
401 return false;
402 }
403 if (disable_buffering_) {
404 file_stream_->DisableBuffering();
405 }
406 return true;
407}
408
409void FileRotatingStream::CloseCurrentFile() {
410 if (!file_stream_) {
411 return;
412 }
413 current_bytes_written_ = 0;
414 file_stream_.reset();
415}
416
417void FileRotatingStream::RotateFiles() {
henrikg91d6ede2015-09-17 00:24:34 -0700418 RTC_DCHECK_EQ(mode_, kWrite);
tkchin93411912015-07-22 12:12:17 -0700419 CloseCurrentFile();
420 // Rotates the files by deleting the file at |rotation_index_|, which is the
421 // oldest file and then renaming the newer files to have an incremented index.
422 // See header file comments for example.
hayscd02b0fa2015-12-08 13:59:05 -0800423 RTC_DCHECK_LT(rotation_index_, file_names_.size());
tkchin93411912015-07-22 12:12:17 -0700424 std::string file_to_delete = file_names_[rotation_index_];
Niels Möller260770c2018-11-07 15:08:18 +0100425 if (IsFile(file_to_delete)) {
426 if (!DeleteFile(file_to_delete)) {
Jonas Olsson55378f42018-05-25 10:23:10 +0200427 std::fprintf(stderr, "Failed to delete: %s\n", file_to_delete.c_str());
tkchin93411912015-07-22 12:12:17 -0700428 }
429 }
430 for (auto i = rotation_index_; i > 0; --i) {
431 std::string rotated_name = file_names_[i];
432 std::string unrotated_name = file_names_[i - 1];
Niels Möller260770c2018-11-07 15:08:18 +0100433 if (IsFile(unrotated_name)) {
434 if (!MoveFile(unrotated_name, rotated_name)) {
Jonas Olsson55378f42018-05-25 10:23:10 +0200435 std::fprintf(stderr, "Failed to move: %s to %s\n",
436 unrotated_name.c_str(), rotated_name.c_str());
tkchin93411912015-07-22 12:12:17 -0700437 }
438 }
439 }
440 // Create a new file for 0th index.
441 OpenCurrentFile();
442 OnRotation();
443}
444
tkchin93411912015-07-22 12:12:17 -0700445std::string FileRotatingStream::GetFilePath(size_t index,
446 size_t num_files) const {
henrikg91d6ede2015-09-17 00:24:34 -0700447 RTC_DCHECK_LT(index, num_files);
tkchin93411912015-07-22 12:12:17 -0700448
Jonas Olsson671cae22018-06-14 09:57:39 +0200449 const size_t buffer_size = 32;
450 char file_postfix[buffer_size];
451 // We want to zero pad the index so that it will sort nicely.
452 const int max_digits = std::snprintf(nullptr, 0, "%zu", num_files - 1);
453 RTC_DCHECK_LT(1 + max_digits, buffer_size);
454 std::snprintf(file_postfix, buffer_size, "_%0*zu", max_digits, index);
tkchin93411912015-07-22 12:12:17 -0700455
Niels Möller7b3c76b2018-11-07 09:54:28 +0100456 return dir_path_ + file_prefix_ + file_postfix;
tkchin93411912015-07-22 12:12:17 -0700457}
458
459CallSessionFileRotatingStream::CallSessionFileRotatingStream(
460 const std::string& dir_path)
461 : FileRotatingStream(dir_path, kLogPrefix),
462 max_total_log_size_(0),
Yves Gerey665174f2018-06-19 15:03:05 +0200463 num_rotations_(0) {}
tkchin93411912015-07-22 12:12:17 -0700464
465CallSessionFileRotatingStream::CallSessionFileRotatingStream(
466 const std::string& dir_path,
467 size_t max_total_log_size)
468 : FileRotatingStream(dir_path,
469 kLogPrefix,
470 max_total_log_size / 2,
471 GetNumRotatingLogFiles(max_total_log_size) + 1),
472 max_total_log_size_(max_total_log_size),
473 num_rotations_(0) {
kwibergaf476c72016-11-28 15:21:39 -0800474 RTC_DCHECK_GE(max_total_log_size, 4);
tkchin93411912015-07-22 12:12:17 -0700475}
476
477const char* CallSessionFileRotatingStream::kLogPrefix = "webrtc_log";
478const size_t CallSessionFileRotatingStream::kRotatingLogFileDefaultSize =
479 1024 * 1024;
480
481void CallSessionFileRotatingStream::OnRotation() {
482 ++num_rotations_;
483 if (num_rotations_ == 1) {
484 // On the first rotation adjust the max file size so subsequent files after
485 // the first are smaller.
486 SetMaxFileSize(GetRotatingLogSize(max_total_log_size_));
487 } else if (num_rotations_ == (GetNumFiles() - 1)) {
488 // On the next rotation the very first file is going to be deleted. Change
489 // the rotation index so this doesn't happen.
490 SetRotationIndex(GetRotationIndex() - 1);
491 }
492}
493
494size_t CallSessionFileRotatingStream::GetRotatingLogSize(
495 size_t max_total_log_size) {
496 size_t num_rotating_log_files = GetNumRotatingLogFiles(max_total_log_size);
497 size_t rotating_log_size = num_rotating_log_files > 2
498 ? kRotatingLogFileDefaultSize
499 : max_total_log_size / 4;
500 return rotating_log_size;
501}
502
503size_t CallSessionFileRotatingStream::GetNumRotatingLogFiles(
504 size_t max_total_log_size) {
505 // At minimum have two rotating files. Otherwise split the available log size
506 // evenly across 1MB files.
507 return std::max((size_t)2,
508 (max_total_log_size / 2) / kRotatingLogFileDefaultSize);
509}
510
511} // namespace rtc