tempdir
forked from lugia-kun/tempdirtempdir.cr
Creates a temporary directory with atomic mkdtemp/mkstemp support.
Features
- Atomic creation: Uses
mkdtempon Unix andCreateFileAon Windows for race-free temp directory/file creation - Secure permissions: Directories created with
0o700, files with0o600(owner-only) - Cross-platform: Works on Unix/Linux/macOS and Windows
- Convenience methods:
create_tempfilefor atomic tempfile creation inside temp directories - Automatic cleanup: Tempdir removes contents on
#close, block form auto-cleans
Installation
-
Add the dependency to your
shard.yml:dependencies: tempdir: github: kritoke/tempdir -
Run
shards install
Usage
require "tempdir"
Dir.mktmpdir
dir = Dir.mktmpdir(*args)
Creates a temporary directory. Arguments are passed to File.tempname as-is. See File.tempname.
The returning object is Tempdir. It removes all entries when #close-d.
With block, the created directory will be removed after block is left.
Dir.mktmpdir do |dir|
# work with dir
end
Tempdir
The temporary directory class based on Dir.
This class only rewrites the #close method to remove entries in the directory.
create_tempfile
Create a secure tempfile inside the tempdir using atomic mkstemp:
Dir.mktmpdir do |dir|
path = dir.create_tempfile("myfile_", data: Slice(UInt8).new([1, 2, 3]))
# path is the created file path with 0o600 permissions
end
The data parameter is optional. If provided, writes the bytes atomically. The file is created with owner-only permissions (0o600) where supported.
Security
- Directories are created with
0o700permissions (owner-only). - Files created via
create_tempfileusemkstempfor atomic creation and are set to0o600(owner read/write only). - On Unix, uses
mkdtemp/mkstempfor atomic creation avoiding TOCTOU races. - On Windows, falls back to
CreateFileAwithCREATE_NEWflag for atomic semantics.
Development
crystal spec
Contributing
- Fork it (https://github.com/kritoke/tempdir/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
License
MIT
tempdir
- 0
- 0
- 0
- 1
- 0
- 10 days ago
- February 23, 2026
MIT License
Mon, 23 Feb 2026 03:27:17 GMT