Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jacobsa-fuse
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
jacobsa-fuse
Commits
12b184c0
Commit
12b184c0
authored
Mar 24, 2015
by
Aaron Jacobs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Clean up after the tool binary.
parent
6cc47f10
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
68 additions
and
19 deletions
+68
-19
samples/subprocess.go
samples/subprocess.go
+68
-19
No files found.
samples/subprocess.go
View file @
12b184c0
...
@@ -75,28 +75,36 @@ func (t *SubprocessTest) SetUp(ti *ogletest.TestInfo) {
...
@@ -75,28 +75,36 @@ func (t *SubprocessTest) SetUp(ti *ogletest.TestInfo) {
}
}
// Private state for getToolPath.
// Private state for getToolPath.
var
getTool
Path_Path
string
var
getTool
Contents_Contents
[]
byte
var
getTool
Path
_Err
error
var
getTool
Contents
_Err
error
var
getTool
Path
_Once
sync
.
Once
var
getTool
Contents
_Once
sync
.
Once
// Implementation detail of getToolPath.
// Implementation detail of getToolPath.
func
getTool
PathImpl
()
(
toolPath
string
,
err
error
)
{
func
getTool
ContentsImpl
()
(
contents
[]
byte
,
err
error
)
{
// Fast path: has the user set the flag?
// Fast path: has the user set the flag?
if
*
fToolPath
!=
""
{
if
*
fToolPath
!=
""
{
toolPath
=
*
fToolPath
contents
,
err
=
ioutil
.
ReadFile
(
*
fToolPath
)
if
err
!=
nil
{
err
=
fmt
.
Errorf
(
"Reading mount_sample contents: %v"
,
err
)
return
}
return
return
}
}
// Create a temporary directory.
// Create a temporary directory
into which we will compile the tool
.
tempDir
,
err
:=
ioutil
.
TempDir
(
""
,
""
)
tempDir
,
err
:=
ioutil
.
TempDir
(
""
,
""
)
if
err
!=
nil
{
if
err
!=
nil
{
err
=
fmt
.
Errorf
(
"TempDir: %v"
,
err
)
err
=
fmt
.
Errorf
(
"TempDir: %v"
,
err
)
return
return
}
}
toolPath
=
path
.
Join
(
tempDir
,
"mount_sample"
)
toolPath
:=
path
.
Join
(
tempDir
,
"mount_sample"
)
// Ensure that we kill the temporary directory when we're finished here.
defer
os
.
RemoveAll
(
tempDir
)
//
Build the command
.
//
Run "go build"
.
cmd
:=
exec
.
Command
(
cmd
:=
exec
.
Command
(
"go"
,
"go"
,
"build"
,
"build"
,
...
@@ -114,18 +122,25 @@ func getToolPathImpl() (toolPath string, err error) {
...
@@ -114,18 +122,25 @@ func getToolPathImpl() (toolPath string, err error) {
return
return
}
}
// Slurp the tool contents.
contents
,
err
=
ioutil
.
ReadFile
(
toolPath
)
if
err
!=
nil
{
err
=
fmt
.
Errorf
(
"ReadFile: %v"
,
err
)
return
}
return
return
}
}
// Build the mount_sample tool if it has not yet been built for this process.
// Build the mount_sample tool if it has not yet been built for this process.
// Return
a path to the binary
.
// Return
its contents
.
func
getTool
Path
()
(
toolPath
string
,
err
error
)
{
func
getTool
Contents
()
(
contents
[]
byte
,
err
error
)
{
//
Build
if we haven't yet.
//
Get hold of the binary contents,
if we haven't yet.
getTool
Path
_Once
.
Do
(
func
()
{
getTool
Contents
_Once
.
Do
(
func
()
{
getTool
Path_Path
,
getToolPath_Err
=
getToolPath
Impl
()
getTool
Contents_Contents
,
getToolContents_Err
=
getToolContents
Impl
()
})
})
toolPath
,
err
=
getToolPath_Path
,
getToolPath
_Err
contents
,
err
=
getToolContents_Contents
,
getToolContents
_Err
return
return
}
}
...
@@ -180,10 +195,42 @@ func (t *SubprocessTest) initialize() (err error) {
...
@@ -180,10 +195,42 @@ func (t *SubprocessTest) initialize() (err error) {
return
return
}
}
// Build the mount_sample tool.
// Build/read the mount_sample tool.
toolPath
,
err
:=
getToolPath
()
toolContents
,
err
:=
getToolContents
()
if
err
!=
nil
{
err
=
fmt
.
Errorf
(
"getTooltoolContents: %v"
,
err
)
return
}
// Create a temporary file to hold the contents of the tool.
toolFile
,
err
:=
ioutil
.
TempFile
(
""
,
"subprocess_test"
)
if
err
!=
nil
{
if
err
!=
nil
{
err
=
fmt
.
Errorf
(
"getToolPath: %v"
,
err
)
err
=
fmt
.
Errorf
(
"TempFile: %v"
,
err
)
return
}
defer
toolFile
.
Close
()
// Ensure that it is deleted when we leave.
toolPath
:=
toolFile
.
Name
()
defer
os
.
Remove
(
toolPath
)
// Write out the tool contents and make them executable.
if
_
,
err
=
toolFile
.
Write
(
toolContents
);
err
!=
nil
{
err
=
fmt
.
Errorf
(
"toolFile.Write: %v"
,
err
)
return
}
if
err
=
toolFile
.
Chmod
(
0500
);
err
!=
nil
{
err
=
fmt
.
Errorf
(
"toolFile.Chmod: %v"
,
err
)
return
}
// Close the tool file to prevent "text file busy" errors below.
err
=
toolFile
.
Close
()
toolFile
=
nil
if
err
!=
nil
{
err
=
fmt
.
Errorf
(
"toolFile.Close: %v"
,
err
)
return
return
}
}
...
@@ -263,6 +310,8 @@ func (t *SubprocessTest) TearDown() {
...
@@ -263,6 +310,8 @@ func (t *SubprocessTest) TearDown() {
// Like TearDown, but doesn't panic.
// Like TearDown, but doesn't panic.
func
(
t
*
SubprocessTest
)
destroy
()
(
err
error
)
{
func
(
t
*
SubprocessTest
)
destroy
()
(
err
error
)
{
// Make sure we clean up after ourselves after everything else below.
// Close what is necessary.
// Close what is necessary.
for
_
,
c
:=
range
t
.
ToClose
{
for
_
,
c
:=
range
t
.
ToClose
{
if
c
==
nil
{
if
c
==
nil
{
...
@@ -298,8 +347,8 @@ func (t *SubprocessTest) destroy() (err error) {
...
@@ -298,8 +347,8 @@ func (t *SubprocessTest) destroy() (err error) {
return
return
}
}
//
Attempt to unlink the mount point
.
//
Clean up
.
o
s
.
Remove
(
t
.
Dir
)
o
gletest
.
ExpectEq
(
nil
,
os
.
Remove
(
t
.
Dir
)
)
}()
}()
// Wait for the subprocess.
// Wait for the subprocess.
...
...
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