securetemp

package module
v2.0.1+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 1, 2019 License: AGPL-3.0 Imports: 8 Imported by: 1

README

Secure temporary directories and files in Go

Build Status

This CLI app and library can be used to create temporary directories and files inside RAM. This is useful if you temporarily need a file to store sensitive data in.

tl;dr

# Install library
go get -u github.com/leonklingele/securetemp

# Install library + CLI app ("securetemp")
go get -u github.com/leonklingele/securetemp/...
securetemp -h
// Create a temporary file inside a RAM disk which is securetemp.DefaultSize big
tmpFile, cleanupFunc, err := securetemp.TempFile(securetemp.DefaultSize)
if err != nil {
	// TODO: Properly handle error
	log.Fatal(err)
}
// `tmpFile` is an *os.File
if _, err := tmpFile.WriteString("Hello, World!"); err != nil {
	// TODO: Properly handle error
	log.Fatal(err)
}
// Call the cleanup func as soon as you no longer need the file.
// The file is deleted and the RAM is freed again.
// The cleanup function also calls `tmpFile.Close()` for you.
cleanupFunc()
// Create a temporary directory inside a RAM disk which is 20MB big
tmpDir, cleanupFunc, err := securetemp.TempDir(20 * securetemp.SizeMB)
if err != nil {
	// TODO: Properly handle error
	log.Fatal(err)
}
// Create one / multiple file/s inside `tmpDir`
file, err := os.Create(path.Join(tmpDir, "myfile.txt"))
if err != nil {
	// TODO: Properly handle error
	log.Fatal(err)
}
defer file.Close()
// Do something with `file`
if _, err := file.WriteString("Hello, World!"); err != nil {
	// TODO: Properly handle error
	log.Fatal(err)
}
file.Close()
// Call the cleanup func as soon as you no longer need the directory.
// The directory is deleted and the RAM is freed again.
// Make sure to close every file inside `tmpDir` before.
cleanupFunc()

Idea

This project was inspired by pass

Documentation

Index

Constants

View Source
const (
	// SizeKB represents one Kibibyte
	SizeKB = 1 << (10 * iota)
	// SizeMB represents one Mebibyte (1024 KiB)
	SizeMB
	// SizeGB represents one Gibibyte (1024 MiB)
	SizeGB
)

TODO(leon): It's {K,M,G}iB, not {K,M,G}B (with an "i")

View Source
const (
	// DefaultSize specifies the default size for a RAM disk in MB
	DefaultSize = 4 * SizeMB
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CleanupFunc

type CleanupFunc func()

func TempDir

func TempDir(size int) (string, CleanupFunc, error)

TempDir creates a new RAM disk with size 'size' (in bytes) and returns the path to it. Use this function only if you intend to create multiple files inside your RAM disk, else prefer to use 'TempFile'.

func TempFile

func TempFile(size int) (*os.File, CleanupFunc, error)

TempFile creates a new RAM disk with size 'size' (in bytes), creates a temp file in it and returns a pointer to that file. Use this function only if you intend to create a single file inside your RAM disk, else prefer to use 'TempDir'.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL