tasks a-s

This commit is contained in:
Ingy döt Net 2013-04-10 23:57:08 -07:00
parent 47bf37c096
commit b83f433714
12433 changed files with 156208 additions and 123 deletions

View file

@ -0,0 +1 @@
Create a temporary file, '''securely and exclusively''' (opening it such that there are no possible [[race condition|race conditions]]). It's fine assuming local filesystem semantics (NFS or other networking filesystems can have signficantly more complicated semantics for satisfying the "no race conditions" criteria). The function should automatically resolve name collisions and should only fail in cases where permission is denied, the filesystem is read-only or full, or similar conditions exist (returning an error or raising an exception as appropriate to the language/environment).

View file

@ -0,0 +1,2 @@
---
note: Programming environment operations

View file

@ -0,0 +1,14 @@
with Ada.Text_Io; use Ada.Text_Io;
procedure Temp_File is
Temp : File_Type;
Contents : String(1..80);
Length : Natural;
begin
-- Create a temporary file
Create(File => Temp);
Put_Line(File => Temp, Item => "Hello World");
Reset(File => Temp, Mode => In_File);
Get_Line(File => Temp, Item => Contents, Last => Length);
Put_Line(Contents(1..Length));
end Temp_File;

View file

@ -0,0 +1,27 @@
file% = FNopentempfile
IF file% = 0 ERROR 100, "Failed to open temp file"
PRINT #file%, "Hello world!"
PTR#file% = 0
INPUT #file%, message$
CLOSE #file%
PRINT message$
END
DEF FNopentempfile
LOCAL pname%, hfile%, chan%
OPEN_EXISTING = 3
FILE_FLAG_DELETE_ON_CLOSE = &4000000
GENERIC_READ = &80000000
GENERIC_WRITE = &40000000
INVALID_HANDLE_VALUE = -1
DIM pname% LOCAL 260
FOR chan% = 5 TO 12
IF @hfile%(chan%) = 0 EXIT FOR
NEXT
IF chan% > 12 THEN = 0
SYS "GetTempFileName", @tmp$, "BBC", 0, pname%
SYS "CreateFile", $$pname%, GENERIC_READ OR GENERIC_WRITE, 0, 0, \
\ OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE, 0 TO hfile%
IF hfile% = INVALID_HANDLE_VALUE THEN = 0
@hfile%(chan%) = hfile%
= chan%

View file

@ -0,0 +1,14 @@
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
FILE *fh = tmpfile(); /* file is automatically deleted when program exits */
/* do stuff with stream "fh" */
fclose(fh);
/* The C standard library also has a tmpnam() function to create a file
for you to open later. But you should not use it because someone else might
be able to open the file from the time it is created by this function to the
time you open it. */
return 0;
}

View file

@ -0,0 +1,12 @@
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char filename[] = "/tmp/prefixXXXXXX";
int fd = mkstemp(filename);
puts(filename);
/* do stuff with file descriptor "fd" */
close(fd);
return 0;
}

View file

@ -0,0 +1,2 @@
user=> (doto (java.io.File/createTempFile "pre" ".suff") .deleteOnExit)
#<File /tmp/pre8116759964152254766.suff>

View file

@ -0,0 +1,15 @@
module tempfile ;
import tango.io.TempFile, tango.io.Stdout ;
void main(char[][] args) {
// create a temporary file that will be deleted automatically when out of scope
auto tempTransient = new TempFile(TempFile.Transient) ;
Stdout(tempTransient.path()).newline ;
// create a temporary file, still persist after the TempFile object has been destroyed
auto tempPermanent = new TempFile(TempFile.Permanent) ;
Stdout(tempPermanent.path()).newline ;
// both can only be accessed by the current user (the program?).
}

View file

@ -0,0 +1,3 @@
import "io/ioutil"
file_obj, err := ioutil.TempFile("", "foo")

View file

@ -0,0 +1,3 @@
def file = File.createTempFile( "xxx", ".txt" )
file.deleteOnExit()
println file

View file

@ -0,0 +1,5 @@
import System.IO
main = do (pathOfTempFile, h) <- openTempFile "." "prefix.suffix" -- first argument is path to directory where you want to put it
-- do stuff with it here; "h" is the Handle to the opened file
return ()

View file

@ -0,0 +1,12 @@
! The "scratch" option opens a file exclusively for the current process.
! A scratch file is automatically deleted upon process termination.
OPEN( FIle='TemporaryAndExclusive', SCRatch, IOStat=ErrNr)
WRITE(FIle='TemporaryAndExclusive') "something"
WRITE(FIle='TemporaryAndExclusive', CLoSe=1) ! explicit "close" deletes file
! Without "scratch" access can be controlled by "denyread", "denywrite", "denyreadwrite" options.
OPEN( FIle='DenyForOthers', DenyREAdWRIte, IOStat=ErrNr)
WRITE(FIle='DenyForOthers') "something"
WRITE(FIle='DenyForOthers', DELETE=1)

View file

@ -0,0 +1,13 @@
import java.io.File;
try {
// Create temp file
File filename = File.createTempFile("prefix", ".suffix");
// Delete temp file when program exits
filename.deleteOnExit();
System.out.println(filename);
} catch (IOException e) {
}

View file

@ -0,0 +1,5 @@
fp = io.tmpfile()
-- do some file operations
fp:close()

View file

@ -0,0 +1,2 @@
tmp = OpenWrite[]
Close[tmp]

View file

@ -0,0 +1,2 @@
# Filename.temp_file "prefix." ".suffix" ;;
- : string = "/home/blue_prawn/tmp/prefix.301f82.suffix"

View file

@ -0,0 +1,3 @@
[FID, MSG] = tmpfile(); % Return the file ID corresponding to a new temporary
filename = tmpnam (...); % generates temporary file name, but does not open file
[FID, NAME, MSG] = mkstemp (TEMPLATE, DELETE); % Return the file ID corresponding to a new temporary file with a unique name created from TEMPLATE.

View file

@ -0,0 +1,9 @@
$fh = tmpfile();
// do stuff with $fh
fclose($fh);
// file removed when closed
// or:
$filename = tempnam('/tmp', 'prefix');
echo "$filename\n";
// open $filename and do stuff with it

View file

@ -0,0 +1,14 @@
Program TempFileDemo;
uses
SysUtils;
var
tempFile: text;
begin
assign (Tempfile, GetTempFileName);
rewrite (tempFile);
writeln (tempFile, 5);
close (tempFile);
end.

View file

@ -0,0 +1,6 @@
use File::Temp qw(tempfile);
$fh = tempfile();
($fh2, $filename) = tempfile(); # this file stays around by default
print "$filename\n";
close $fh;
close $fh2;

View file

@ -0,0 +1,4 @@
use File::Temp;
$fh = new File::Temp;
print $fh->filename, "\n";
close $fh;

View file

@ -0,0 +1,11 @@
: (out (tmp "foo") (println 123)) # Write tempfile
-> 123
: (in (tmp "foo") (read)) # Read tempfile
-> 123
: (let F (tmp "foo")
(ctl F # Get exclusive lock
(let N (in F (read)) # Atomic increment
(out F (println (inc N))) ) ) )
-> 124

View file

@ -0,0 +1,25 @@
Procedure.s TempFile()
Protected a, Result$
For a = 0 To 9999
Result$ = GetTemporaryDirectory() + StringField(GetFilePart(ProgramFilename()),1,".")
Result$ + "_" + Str(ElapsedMilliseconds()) + "_(" + RSet(Str(a),4,"0") + ").tmp"
If FileSize(Result$) = -1 ; -1 = File not found
ProcedureReturn Result$
EndIf
Next
ProcedureReturn ""
EndProcedure
Define File, File$
File$ = TempFile()
If File$ <> ""
File = CreateFile(#PB_Any, File$)
If File <> 0
WriteString(File, "Some temporary data here...")
CloseFile(File)
EndIf
EndIf

View file

@ -0,0 +1,9 @@
>>> import tempfile
>>> invisible = tempfile.TemporaryFile()
>>> invisible.name
'<fdopen>'
>>> visible = tempfile.NamedTemporaryFile()
>>> visible.name
'/tmp/tmpZNfc_s'
>>> visible.close()
>>> invisible.close()

View file

@ -0,0 +1,5 @@
fd, path = tempfile.mkstemp()
try:
# use the path or the file descriptor
finally:
os.close(fd)

View file

@ -0,0 +1,2 @@
#lang racket
(make-temporary-file)

View file

@ -0,0 +1,8 @@
irb(main):001:0> require 'tempfile'
=> true
irb(main):002:0> f = Tempfile.new('foo')
=> #<File:/tmp/foo20081226-307-10p746n-0>
irb(main):003:0> f.path
=> "/tmp/foo20081226-307-10p746n-0"
irb(main):004:0> f.close
=> nil

View file

@ -0,0 +1,14 @@
import java.io.File
try {
// Create temp file
val filename = File.createTempFile("prefix", ".suffix")
// Delete temp file when program exits
filename.deleteOnExit
System.out.println(filename)
} catch {
case _: java.io.IOException =>
}

View file

@ -0,0 +1 @@
val filename = OS.FileSys.tmpName ();

View file

@ -0,0 +1,6 @@
$$ MODE TUSCRIPT
tmpfile="test.txt"
ERROR/STOP CREATE (tmpfile,seq-E,-std-)
text="hello world"
FILE $tmpfile = text
- tmpfile "test.txt" can only be accessed by one user an will be deleted upon programm termination

View file

@ -0,0 +1 @@
set chan [file tempfile filenameVar]

View file

@ -0,0 +1,13 @@
RESTOREUMASK=$(umask)
TRY=0
while :; do
TRY=$(( TRY + 1 ))
umask 0077
MYTMP=${TMPDIR:-/tmp}/$(basename $0).$$.$(date +%s).$TRY
trap "rm -fr $MYTMP" EXIT
mkdir "$MYTMP" 2>/dev/null && break
done
umask "$RESTOREUMASK"
cd "$MYTMP" || {
echo "Temporary directory failure on $MYTMP" >&2
exit 1; }