Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
可能な限り確実にmkdirを成功させるには / Make mkdir
Search
Yuichi Sugiyama
PRO
March 25, 2023
Technology
0
430
可能な限り確実にmkdirを成功させるには / Make mkdir
PHPerKaigi 2023 のLTの資料です
Yuichi Sugiyama
PRO
March 25, 2023
Tweet
Share
More Decks by Yuichi Sugiyama
See All by Yuichi Sugiyama
サイボウズ と Garoon と The PHP Foundation と 私 / Cybozu and Garoon and The PHP Foundation and me
oogfranz
PRO
1
430
サイボウズ #Garoon 開発チームの 「 完成度低いの歓迎LT大会 」 PHPerKaigi出張版 / Low quality LT in PHPerKaigi 2023
oogfranz
PRO
0
490
20年ものの巨大プロダクトをKubernetesに移行している話 後日談/Garoon on Kubernetes after talk
oogfranz
PRO
0
490
20年ものの巨大プロダクトをKubernetesに移行している話/Garoon on Kubernetes
oogfranz
PRO
0
360
PHPアプリケーションだってモニタリングしたい / Monitoring PHP application
oogfranz
PRO
1
550
効果的な静的解析の CI導入パターンを求めて / Great static analysis with CI
oogfranz
PRO
3
3.4k
Dev-meets-Ops
oogfranz
PRO
1
900
GitHub力の低い僕でも、 OSSコントリビュートできたワケ / GitHub Power
oogfranz
PRO
1
490
静的解析の育て方 / How to make your static analysis strong
oogfranz
PRO
3
2.7k
Other Decks in Technology
See All in Technology
疎通2024
sadnessojisan
5
1k
やってやろうじゃないかメカアジャイル! / Let's do it, mechanical agile!
psj59129
1
600
ロリポップ! for Gamersを支えるインフラ/lolipop for gamers infrastructure
takumakume
0
130
アプリをリリースできる状態に保ったまま 段階的にリファクタリングするための 戦略と戦術 / Strategies and tactics for incremental refactoring
yanzm
6
1.3k
『GRANBLUE FANTASY Relink』ソフトウェアラスタライザによる実践的なオクルージョンカリング
cygames
0
140
Javaにおける関数型プログラミンへの取り組み
skrb
7
320
AWS SAW を広めたい @四国クラウドお遍路
kazzpapa3
0
230
Technical Writing Meetup vol.35
soracom
PRO
2
110
eBPFのこれまでとこれから
yutarohayakawa
9
3.1k
技術的負債解消の取り組みと専門チームのお話
bengo4com
0
330
ロボットアームを遠隔制御の話 & LLMをつかったIoTの話もしたい
soracom
PRO
1
380
四国のあのイベントの〇〇システムを45日間で構築した話 / cloudohenro2024_tachibana
biatunky
0
330
Featured
See All Featured
Designing for Performance
lara
604
68k
Large-scale JavaScript Application Architecture
addyosmani
508
110k
Web Components: a chance to create the future
zenorocha
308
42k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2k
Music & Morning Musume
bryan
46
6k
Practical Orchestrator
shlominoach
185
10k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
248
20k
KATA
mclloyd
27
13k
The Pragmatic Product Professional
lauravandoore
31
6.2k
Building Your Own Lightsaber
phodgson
101
6k
10 Git Anti Patterns You Should be Aware of
lemiorhan
653
58k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
190
16k
Transcript
ՄೳͳݶΓ࣮֬ʹ mkdirΛޭͤ͞Δʹ PHPerKaigi 2023 ͗͢·@MASHݭָஂ
Whoami • ਿࢁ ༞Ұ @oogFranz • αΠϘζͰେاۀ͚άϧʔϓΣΞ Garoon (Ψϧʔϯ)
࡞ͬͯΔ • ෳۀϛϡʔδγϟϯ • MASHݭָஂɺFried Banana CabinɺϒϧʔάϥεˑϙϦε
σΟϨΫτϦΛPHP͔Β࡞Ε·͔͢ʁ function makeDirectory(string $path): bool { //??? }
mkdir() Ͱ༨༟ͩͥʂ function makeDirectory(string $path): bool { return mkdir($path); }
mkdir() Ͱ༨༟ͩͥʂ ͜ͷ࣮ͷΛग़དྷΔݶΓ্͛ͯΈ·͠ΐ͏ʂ function makeDirectory(string $path): bool { return mkdir($path);
}
σΟϨΫτϦύεΛࢦఆ͢ΔͱWarning makeDirectory('test/foo/bar'); //Warning: mkdir(): No such file or directory in
/usr/ local/code/mkdir.php on line 9 • testσΟϨΫτϦɺfoo σΟϨΫτϦ͕ଘࡏ͠ͳ͍߹ Warning͕ൃੜ͢Δ • σΟϨΫτϦ࡞͞Εͳ͍
ϚχϡΞϧΛͪΌΜͱಡΉ • recursiveΦϓγϣϯmkdir ͷ —parents, -p Φϓγϣϯͱಉ͡ • σΟϨΫτϦΛඞཁʹԠͯ͡࡞ͯ͘͠ΕΔ mkdir(
string $directory, int $permissions = 0777, bool $recursive = false, ?resource $context = null ): bool
recursive = trueΛ͏ function makeDirectory(string $path): bool { return mkdir($path,
recursive: true); }
σΟϨΫτϦ͕طʹଘࡏ͢ΔͱWarning • ࡞લʹis_dir(), fi le_exists()ͰνΣοΫ͕ඞཁ • ͜ΕϚχϡΞϧʹॻ͍ͯ͋Δ makeDirectory('foo'); makeDirectory('foo'); //
Warning: mkdir(): File exists in /usr/local/ code/mkdir.php on line 9
is_dir, fi le_existsΛ͏ function makeDirectory(string $path): bool { if (is_dir($path))
{ return true; } if (file_exists($path)) { return false; } return mkdir($path, recursive: true); } ✴ fi le_exists() ͕ trueͷͱ͖݁ՌతʹσΟϨΫτϦ͕࡞Ͱ͖ͳ͍ͷͰҙ
σΟϨΫτϦ͕ͳ͍ͷʹis_dir͕trueΛฦ͢ makeDirectory('foo/bar'); makeDirectory('foo/bar'); sleep(10); //͜͜Ͱผϓϩηε͔Βfoo/barσΟϨΫτϦΛআ͢Δ makeDirectory('foo/bar');
σΟϨΫτϦ͕ͳ͍ͷʹis_dir͕trueΛฦ͢ makeDirectory('foo/bar'); makeDirectory('foo/bar'); sleep(10); //͜͜Ͱผϓϩηε͔Βfoo/barσΟϨΫτϦΛআ͢Δ makeDirectory('foo/bar'); σΟϨΫτϦ͕࡞͞Εͳ͍ʂ
is_dir()Ωϟογϡ͞Ε͍ͯΔʂ • is_dir()ͳͲͷ݁ՌύϑΥʔϚϯεͷͨΊʹΩϟογϡ͞ΕΔ • Ωϟογϡϓϩηε͝ͱʹ࡞͞ΕΔɻ • ଞͷϓϩηεʹΑͬͯϑΝΠϧͷεςʔλε͕มΘΔ߹ ΩϟογϡͷΫϦΞ͕ඞཁ
clearstatcache()Λ͏ function makeDirectory(string $path): bool { clearstatcache(); if (is_dir($path)) {
return true; } if (file_exists($path)) { return false; } return mkdir($path, recursive: true); }
clearstatcache()Λ͏ function makeDirectory(string $path): bool { clearstatcache(); if (is_dir($path)) {
return true; } if (file_exists($path)) { return false; } return mkdir($path, recursive: true); } JT@EJS͕ਖ਼͘͠ಈ࡞͢Δ
࣮ߦλΠϛϯάʹΑͬͯWarning is_dir() is_dir() fi le_exits() fi le_exits() ϓϩηε1 ϓϩηε2 ·ͩσΟϨΫτϦ͕
ଘࡏ͠ͳ͍ͷͰGBMTF
࣮ߦλΠϛϯάʹΑͬͯWarning is_dir() is_dir() fi le_exits() fi le_exits() mkdir() ϓϩηε1 ϓϩηε2
σΟϨΫτϦ࡞ޭ
࣮ߦλΠϛϯάʹΑͬͯWarning is_dir() is_dir() fi le_exits() fi le_exits() mkdir() mkdir() ϓϩηε1
ϓϩηε2 σΟϨΫτϦ࡞ࣦഊʂ
Τϥʔͷ͑ํ • Τϥʔ੍ޚԋࢉࢠ `@` Λ͏ खܰɻͳΜ͔ෛ͚ͨؾ͕͢Δɻ • error_reporting Ͱ
E_WARNINGΛ͑Δ ଞͷE_WARNING͑ͯ͠·͏ͷͰ͓קΊ͠ͳ͍ • set_error_handler ͰWarningΛΩϟον͢Δ ਅ໘ʹΔͳΒ͜Εɻ • ഉଞ੍ޚʁʁ ΦʔόʔΩϧͰʁ͜ͷϝιουҎ֎͔Βͷݺͼग़͠ʹউͯͳ͍ɺɺɺ
Τϥʔ੍ޚԋࢉࢠΛͬͨ߹ function makeDirectory(string $path): bool { if (@mkdir($path, recursive: true)
=== false) { clearstatcache(); return is_dir($path); } return true; } • Τϥʔ੍ޚʹͤͯࣄલͷσΟϨΫτϦνΣοΫෆཁʢ͔ʣ
Permission • ͦͦ࡞ݖݶ͕ͳ͍߹E_WARNING͕ൃੜ • is_writableͰ֬ೝՄೳ • σΟϨΫτϦ͕͋Δͱ͖ɺͳ͍ͱ͖͕͋ΔͷͰ࠶ؼ͕ඞཁ • ͏࣮ߦ࣮ͯ͠ߦͰ͖Δ͔ࢼͨ͠ํ͕ૣ͘ͳ͍ʁʁ
→Τϥʔ੍ޚඞਢ
·ͱΊ • mkdirͳͲϑΝΠϧγεςϜʹ৮Δ߹ߟྀϙΠϯτଟ͍ • ϚχϡΞϧͪΌΜͱಡ͏ • ϑΝΠϧγεςϜͷΩϟογϡϋϚΔͱͭΒ͍ͷͰɺ clearstatcacheΛ͍·͠ΐ͏ •
ύʔϛογϣϯΉ͔͍ͣ͠ • Ͳ͏ؤுͬͯWarningಀΕΒΕͳ͍ͷͰɺ Τϥʔ੍ޚ͕ΜΓ·͠ΐ͏
αΠϘζPHPerKaigi ޙஊΠϕϯτʂ https://cybozu.connpass.com/event/277790/ 4/5ʢਫʣ 19:00~ @αΠϘζຊڮΦϑΟε