# base::Fileのコンストラクタ [base::File](https://source.chromium.org/chromium/chromium/src/+/main:base/files/file.h)はOSレベルのファイルのラッパー。 base::Fileはいくつかのコンストラクタを持っていて、それぞれが何をしているのか、特に誰がblocking callなのかを確認したい。 ## base::File() まずは引数のないコンストラクタ[base::File()](https://source.chromium.org/chromium/chromium/src/+/main:base/files/file.cc;l=29;drc=5bf1a4a403490afa7294232e383a1750aefcc881)。 これは空のファイルオブジェクトを作るときに使われる。 default実装になっており、何もしない。 [SetPlatformFile](https://source.chromium.org/chromium/chromium/src/+/main:base/files/file_posix.cc;l=668;drc=5bf1a4a403490afa7294232e383a1750aefcc881)でdesctiptorをセットすることができるオブジェクトにはなっている。 ## base::File(file_path) ファイルパスとその開き方のflagを取る[base::File](https://source.chromium.org/chromium/chromium/src/+/main:base/files/file.cc;l=32;drc=5bf1a4a403490afa7294232e383a1750aefcc881)コンストラクタ。 これは実際にファイルをオープンする。 Poxisの場合は[File::DoInitialize](https://source.chromium.org/chromium/chromium/src/+/main:base/files/file_posix.cc;l=528;drc=5bf1a4a403490afa7294232e383a1750aefcc881)につながるが、この中では[ScopedBlockingCall](https://source.chromium.org/chromium/chromium/src/+/main:base/threading/scoped_blocking_call.h;l=92;drc=5bf1a4a403490afa7294232e383a1750aefcc881)が作られている。 ```cpp= ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); ``` これはdaily note頻出だが、Blocking callを呼ぶときにつくっておくことで、blockしていいスレッド上でなければクラッシュするようになっている。 つまりこれはBlocking callを含む。 中では、open_flagsをいろいろ計算した後、[`open`](https://source.chromium.org/chromium/chromium/src/+/main:base/files/file_posix.cc;l=584;drc=5bf1a4a403490afa7294232e383a1750aefcc881)を呼んでdescriptorを取る。 もしdescriptorがvalidなら、このタイミングで[`created_`](https://source.chromium.org/chromium/chromium/src/+/main:base/files/file.h;l=427;drc=5bf1a4a403490afa7294232e383a1750aefcc881)をフリップし、[`file_`](https://source.chromium.org/chromium/chromium/src/+/main:base/files/file.h;l=417;drc=5bf1a4a403490afa7294232e383a1750aefcc881)をそのdescriptorでリセットする。 この`file_`はScopedPlatformFile型。 ## base::FilePath(platform_file) ScopedPlatformFileを受け取ってbase::Fileを作る[コンストラクタ](https://source.chromium.org/chromium/chromium/src/+/main:base/files/file.cc;l=37;drc=5bf1a4a403490afa7294232e383a1750aefcc881)もある。 これは、すでにdescriptorがあるのでopenする必要がなく、`file_`に受け取ったdescriptorをセットして即終了。 ## File::~File [デストラクタ](https://source.chromium.org/chromium/chromium/src/+/main:base/files/file.cc;l=67;drc=5bf1a4a403490afa7294232e383a1750aefcc881)が何をするかも確認しておく。 いかなるコンストラクタを経由していようと、dtorでは[Close](https://source.chromium.org/chromium/chromium/src/+/main:base/files/file.cc;l=69;drc=5bf1a4a403490afa7294232e383a1750aefcc881)を呼んでいる。 Posixの[File::Close](https://source.chromium.org/chromium/chromium/src/+/main:base/files/file_posix.cc;l=239;drc=5bf1a4a403490afa7294232e383a1750aefcc881)では、またScopedBlockingCallが作られ、`file_.reset()`が呼ばれる。 ScopedPlatformFileの[reset](https://source.chromium.org/chromium/chromium/src/+/main:base/scoped_generic.h;l=138;drc=5bf1a4a403490afa7294232e383a1750aefcc881)では[FreeIfNecessary](https://source.chromium.org/chromium/chromium/src/+/main:base/scoped_generic.h;l=256;drc=5bf1a4a403490afa7294232e383a1750aefcc881)でFreeする。