blob: a6ee2aa99c513982c208fd78275cb1e4a7e4b334 [file] [log] [blame]
Viktor Palmkvist4ec6a0c2016-09-02 11:38:321/*
2 * Copyright (c) 2016 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 04:47:3111#include "rtc_base/file.h"
Viktor Palmkvist4ec6a0c2016-09-02 11:38:3212
zijiehedd87d582016-12-06 23:04:0213#include <utility>
14
Viktor Palmkvist4ec6a0c2016-09-02 11:38:3215namespace rtc {
16
zijiehedd87d582016-12-06 23:04:0217namespace {
18
19std::string NormalizePathname(Pathname&& path) {
20 path.Normalize();
21 return path.pathname();
22}
23
24} // namespace
25
Viktor Palmkvist4ec6a0c2016-09-02 11:38:3226File::File(PlatformFile file) : file_(file) {}
27
Viktor Palmkvist1d477ea2016-10-03 11:16:0228File::File() : file_(kInvalidPlatformFileValue) {}
29
Viktor Palmkvist4ec6a0c2016-09-02 11:38:3230File::~File() {
31 Close();
32}
33
zijiehedd87d582016-12-06 23:04:0234// static
Viktor Palmkvist971eb272016-09-16 08:19:2335File File::Open(const std::string& path) {
36 return File(OpenPlatformFile(path));
37}
38
zijiehedd87d582016-12-06 23:04:0239// static
40File File::Open(Pathname&& path) {
41 return Open(NormalizePathname(std::move(path)));
42}
43
44// static
45File File::Open(const Pathname& path) {
46 return Open(Pathname(path));
47}
48
49// static
Viktor Palmkvist971eb272016-09-16 08:19:2350File File::Create(const std::string& path) {
51 return File(CreatePlatformFile(path));
52}
53
zijiehedd87d582016-12-06 23:04:0254// static
55File File::Create(Pathname&& path) {
56 return Create(NormalizePathname(std::move(path)));
57}
58
59// static
60File File::Create(const Pathname& path) {
61 return Create(Pathname(path));
62}
63
zijiehe2769ec62016-12-14 23:03:0364// static
65bool File::Remove(const std::string& path) {
66 return RemoveFile(path);
67}
68
69// static
70bool File::Remove(Pathname&& path) {
71 return Remove(NormalizePathname(std::move(path)));
72}
73
74// static
75bool File::Remove(const Pathname& path) {
76 return Remove(Pathname(path));
77}
78
Viktor Palmkvist4ec6a0c2016-09-02 11:38:3279File::File(File&& other) : file_(other.file_) {
80 other.file_ = kInvalidPlatformFileValue;
81}
82
83File& File::operator=(File&& other) {
84 Close();
85 file_ = other.file_;
86 other.file_ = kInvalidPlatformFileValue;
87 return *this;
88}
89
90bool File::IsOpen() {
91 return file_ != kInvalidPlatformFileValue;
92}
93
94} // namespace rtc