Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-workhorse
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
iv
gitlab-workhorse
Commits
524fda2f
Commit
524fda2f
authored
Jan 25, 2016
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use Go `map` instead of iterating entries multiple times
parent
eebe7a29
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
19 deletions
+20
-19
internal/zipartifacts/metadata.go
internal/zipartifacts/metadata.go
+20
-19
No files found.
internal/zipartifacts/metadata.go
View file @
524fda2f
...
...
@@ -7,8 +7,8 @@ import (
"encoding/json"
"io"
"os"
"path"
"strconv"
"strings"
"time"
)
...
...
@@ -21,6 +21,8 @@ type metadata struct {
Comment
string
`json:"comment,omitempty"`
}
type
zipFileMap
map
[
string
]
*
zip
.
File
const
MetadataHeaderPrefix
=
"
\x00\x00\x00
&"
// length of string below, encoded properly
const
MetadataHeader
=
"GitLab Build Artifacts Metadata 0.0.2
\n
"
...
...
@@ -57,28 +59,26 @@ func writeZipEntryMetadata(output io.Writer, entry *zip.File) error {
return
nil
}
func
handleZipEntryMetadata
(
output
io
.
Writer
,
entry
*
zip
.
File
,
entries
[]
*
zip
.
File
)
error
{
func
handleZipEntryMetadata
(
output
io
.
Writer
,
entry
*
zip
.
File
,
fileMap
zipFileMap
)
error
{
var
dirNodes
[]
string
entryPath
:=
entry
.
Name
var
calculateEntryNodes
func
(
string
)
calculateEntryNodes
=
func
(
str
string
)
{
idx
:=
strings
.
LastIndex
(
str
,
"/"
)
if
idx
<
0
{
return
for
{
entryPath
=
path
.
Dir
(
entryPath
)
if
entryPath
==
"."
||
entryPath
==
"/"
{
break
}
dir
:=
str
[
:
idx
]
dirNodes
=
append
([]
string
{
dir
+
"/"
},
dirNodes
...
)
calculateEntryNodes
(
dir
)
dirNodes
=
append
([]
string
{
entryPath
+
"/"
},
dirNodes
...
)
}
calculateEntryNodes
(
entry
.
Name
)
for
_
,
d
:=
range
dirNodes
{
if
!
hasZipPathEntry
(
d
,
entries
)
{
if
_
,
ok
:=
fileMap
[
d
];
!
ok
{
var
missingHeader
zip
.
FileHeader
missingHeader
.
Name
=
d
missingHeader
.
SetModTime
(
time
.
Now
())
missingHeader
.
SetMode
(
os
.
FileMode
(
uint32
(
0755
)))
missingEntry
:=
&
zip
.
File
{
FileHeader
:
missingHeader
}
fileMap
[
d
]
=
missingEntry
writeZipEntryMetadata
(
output
,
missingEntry
)
}
...
...
@@ -88,14 +88,14 @@ func handleZipEntryMetadata(output io.Writer, entry *zip.File, entries []*zip.Fi
return
err
}
func
hasZipPathEntry
(
path
string
,
entries
[]
*
zip
.
File
)
bool
{
f
or
_
,
e
:=
range
entries
{
if
e
.
Name
==
path
{
return
true
}
func
generateZipFileMap
(
zipEntries
[]
*
zip
.
File
)
zipFileMap
{
f
ileMap
:=
make
(
map
[
string
]
*
zip
.
File
)
for
_
,
entry
:=
range
zipEntries
{
fileMap
[
entry
.
Name
]
=
entry
}
return
f
alse
return
f
ileMap
}
func
generateZipMetadata
(
output
io
.
Writer
,
archive
*
zip
.
Reader
)
error
{
...
...
@@ -108,9 +108,10 @@ func generateZipMetadata(output io.Writer, archive *zip.Reader) error {
return
err
}
fileMap
:=
generateZipFileMap
(
archive
.
File
)
// Write all files
for
_
,
entry
:=
range
archive
.
File
{
if
err
:=
handleZipEntryMetadata
(
output
,
entry
,
archive
.
File
);
err
!=
nil
{
if
err
:=
handleZipEntryMetadata
(
output
,
entry
,
fileMap
);
err
!=
nil
{
return
err
}
}
...
...
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