-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
File\leaky_temporary_file()
(#181)
Summary: This is a temporary file that is not automatically deleted. Name is following the "if it's sometimes necessary but you don't want people to use it, name it something scary" principle :p Adding this based on looking into why some open source Facebook projects (e.g. fbshipit) use PHP IO instead of HSL IO. `File\temporary_file()` should be strongly preferred over this, but sometimes it's needed. X-link: hhvm/hsl#181 Reviewed By: bigfootjon Differential Revision: D34314199 fbshipit-source-id: 8513311c431861b2dfde9b8242702ef7f7585f5a
- Loading branch information
1 parent
180f52c
commit e5b344d
Showing
4 changed files
with
99 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?hh | ||
/* | ||
* Copyright (c) 2004-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the hphp/hsl/ subdirectory of this source tree. | ||
* | ||
*/ | ||
|
||
namespace HH\Lib\_Private\_File; | ||
|
||
use namespace HH\Lib\{OS, Str}; | ||
|
||
function open_temporary_fd( | ||
string $prefix, | ||
string $suffix, | ||
): (OS\FileDescriptor, string) { | ||
if ( | ||
!( | ||
Str\starts_with($prefix, '/') || | ||
Str\starts_with($prefix, './') || | ||
Str\starts_with($prefix, '../') | ||
) | ||
) { | ||
$prefix = Str\trim_right(\sys_get_temp_dir(), '/').'/'.$prefix; | ||
} | ||
$pattern = $prefix.'XXXXXX'.$suffix; | ||
return OS\mkstemps($pattern, Str\length($suffix)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?hh | ||
/* | ||
* Copyright (c) 2004-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the hphp/hsl/ subdirectory of this source tree. | ||
* | ||
*/ | ||
|
||
namespace HH\Lib\File; | ||
|
||
use namespace HH\Lib\File; | ||
use namespace HH\Lib\_Private\_File; | ||
|
||
/** Creates a new temporary file, without automatic cleanup. | ||
* | ||
* `File\temporary_file()` is **strongly** recommended instead. | ||
* | ||
* - If the prefix starts with `.`, it is interpreted relative to the current | ||
* working directory. | ||
* - If the prefix statis with `/`, it is treated as an absolute path. | ||
* - Otherwise, it is created in the system temporary directory. | ||
* | ||
* Regardless of the kind of prefix, the parent directory must exist. | ||
* | ||
* A suffix can optionally be provided; this is useful when you need a | ||
* particular filename extension; for example, | ||
* `File\temporary_file('foo', '.txt')` may create `/tmp/foo123456.txt`. | ||
* | ||
* The temporary file: | ||
* - will be a new file (i.e. `O_CREAT | O_EXCL`) | ||
* - be owned by the current user | ||
* - be created with mode 0600 | ||
* - **will not** be automatically deleted | ||
*/ | ||
function leaky_temporary_file( | ||
string $prefix = 'hack-leakytmp-', | ||
string $suffix = '', | ||
): File\CloseableReadWriteHandle { | ||
list($fd, $path) = _File\open_temporary_fd($prefix, $suffix); | ||
return new _File\CloseableReadWriteHandle($fd, $path); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters