Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
28852c15
Commit
28852c15
authored
Jun 03, 2010
by
Russ Cox
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
io/ioutil: add TempFile
R=r CC=golang-dev
https://golang.org/cl/1472042
parent
47bc1f2e
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
93 additions
and
0 deletions
+93
-0
Makefile
src/pkg/io/ioutil/Makefile
+1
-0
tempfile.go
src/pkg/io/ioutil/tempfile.go
+63
-0
tempfile_test.go
src/pkg/io/ioutil/tempfile_test.go
+29
-0
No files found.
src/pkg/io/ioutil/Makefile
View file @
28852c15
...
...
@@ -7,5 +7,6 @@ include ../../../Make.$(GOARCH)
TARG
=
io/ioutil
GOFILES
=
\
ioutil.go
\
tempfile.go
\
include
../../../Make.pkg
src/pkg/io/ioutil/tempfile.go
0 → 100644
View file @
28852c15
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package
ioutil
import
(
"os"
"strconv"
)
// Random number state, accessed without lock; racy but harmless.
// We generate random temporary file names so that there's a good
// chance the file doesn't exist yet - keeps the number of tries in
// TempFile to a minimum.
var
rand
uint32
func
reseed
()
uint32
{
sec
,
nsec
,
_
:=
os
.
Time
()
return
uint32
(
sec
*
1e9
+
nsec
+
int64
(
os
.
Getpid
()))
}
func
nextSuffix
()
string
{
r
:=
rand
if
r
==
0
{
r
=
reseed
()
}
r
=
r
*
1664525
+
1013904223
// constants from Numerical Recipes
rand
=
r
return
strconv
.
Itoa
(
int
(
1e9
+
r
%
1e9
))[
1
:
]
}
// TempFile creates a new temporary file in the directory dir
// with a name beginning with prefix, opens the file for reading
// and writing, and returns the resulting *os.File.
// If dir is the empty string, TempFile uses the value of the
// environment variable $TMPDIR or, if that is empty,/tmp.
// Multiple programs calling TempFile simultaneously
// will not choose the same file. The caller can use f.Name()
// to find the name of the file. It is the caller's responsibility to
// remove the file when no longer needed.
func
TempFile
(
dir
,
prefix
string
)
(
f
*
os
.
File
,
err
os
.
Error
)
{
if
dir
==
""
{
dir
=
os
.
Getenv
(
"TMPDIR"
)
if
dir
==
""
{
dir
=
"/tmp"
}
}
nconflict
:=
0
for
i
:=
0
;
i
<
10000
;
i
++
{
name
:=
dir
+
"/"
+
prefix
+
nextSuffix
()
f
,
err
=
os
.
Open
(
name
,
os
.
O_RDWR
|
os
.
O_CREATE
|
os
.
O_EXCL
,
0600
)
if
pe
,
ok
:=
err
.
(
*
os
.
PathError
);
ok
&&
pe
.
Error
==
os
.
EEXIST
{
if
nconflict
++
;
nconflict
>
10
{
rand
=
reseed
()
}
continue
}
break
}
return
}
src/pkg/io/ioutil/tempfile_test.go
0 → 100644
View file @
28852c15
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package
ioutil_test
import
(
.
"io/ioutil"
"os"
"testing"
)
func
TestTempFile
(
t
*
testing
.
T
)
{
f
,
err
:=
TempFile
(
"/_not_exists_"
,
"foo"
)
if
f
!=
nil
||
err
==
nil
{
t
.
Errorf
(
"TempFile(`/_not_exists_`, `foo`) = %v, %v"
,
f
,
err
)
}
f
,
err
=
TempFile
(
"/tmp"
,
"ioutil_test"
)
if
f
==
nil
||
err
!=
nil
{
t
.
Errorf
(
"TempFile(`/tmp`, `ioutil_test`) = %v, %v"
,
f
,
err
)
}
re
:=
testing
.
MustCompile
(
"^/tmp/ioutil_test[0-9]+$"
)
if
!
re
.
MatchString
(
f
.
Name
())
{
t
.
Fatalf
(
"TempFile(`/tmp`, `ioutil_test`) created bad name %s"
,
f
.
Name
())
}
os
.
Remove
(
f
.
Name
())
f
.
Close
()
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment