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
1
Merge Requests
1
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
nexedi
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
Show 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 (
...
@@ -7,8 +7,8 @@ import (
"encoding/json"
"encoding/json"
"io"
"io"
"os"
"os"
"path"
"strconv"
"strconv"
"strings"
"time"
"time"
)
)
...
@@ -21,6 +21,8 @@ type metadata struct {
...
@@ -21,6 +21,8 @@ type metadata struct {
Comment
string
`json:"comment,omitempty"`
Comment
string
`json:"comment,omitempty"`
}
}
type
zipFileMap
map
[
string
]
*
zip
.
File
const
MetadataHeaderPrefix
=
"
\x00\x00\x00
&"
// length of string below, encoded properly
const
MetadataHeaderPrefix
=
"
\x00\x00\x00
&"
// length of string below, encoded properly
const
MetadataHeader
=
"GitLab Build Artifacts Metadata 0.0.2
\n
"
const
MetadataHeader
=
"GitLab Build Artifacts Metadata 0.0.2
\n
"
...
@@ -57,28 +59,26 @@ func writeZipEntryMetadata(output io.Writer, entry *zip.File) error {
...
@@ -57,28 +59,26 @@ func writeZipEntryMetadata(output io.Writer, entry *zip.File) error {
return
nil
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
var
dirNodes
[]
string
entryPath
:=
entry
.
Name
var
calculateEntryNodes
func
(
string
)
for
{
calculateEntryNodes
=
func
(
str
string
)
{
entryPath
=
path
.
Dir
(
entryPath
)
idx
:=
strings
.
LastIndex
(
str
,
"/"
)
if
entryPath
==
"."
||
entryPath
==
"/"
{
if
idx
<
0
{
break
return
}
}
dir
:=
str
[
:
idx
]
dirNodes
=
append
([]
string
{
entryPath
+
"/"
},
dirNodes
...
)
dirNodes
=
append
([]
string
{
dir
+
"/"
},
dirNodes
...
)
calculateEntryNodes
(
dir
)
}
}
calculateEntryNodes
(
entry
.
Name
)
for
_
,
d
:=
range
dirNodes
{
for
_
,
d
:=
range
dirNodes
{
if
!
hasZipPathEntry
(
d
,
entries
)
{
if
_
,
ok
:=
fileMap
[
d
];
!
ok
{
var
missingHeader
zip
.
FileHeader
var
missingHeader
zip
.
FileHeader
missingHeader
.
Name
=
d
missingHeader
.
Name
=
d
missingHeader
.
SetModTime
(
time
.
Now
())
missingHeader
.
SetModTime
(
time
.
Now
())
missingHeader
.
SetMode
(
os
.
FileMode
(
uint32
(
0755
)))
missingHeader
.
SetMode
(
os
.
FileMode
(
uint32
(
0755
)))
missingEntry
:=
&
zip
.
File
{
FileHeader
:
missingHeader
}
missingEntry
:=
&
zip
.
File
{
FileHeader
:
missingHeader
}
fileMap
[
d
]
=
missingEntry
writeZipEntryMetadata
(
output
,
missingEntry
)
writeZipEntryMetadata
(
output
,
missingEntry
)
}
}
...
@@ -88,14 +88,14 @@ func handleZipEntryMetadata(output io.Writer, entry *zip.File, entries []*zip.Fi
...
@@ -88,14 +88,14 @@ func handleZipEntryMetadata(output io.Writer, entry *zip.File, entries []*zip.Fi
return
err
return
err
}
}
func
hasZipPathEntry
(
path
string
,
entries
[]
*
zip
.
File
)
bool
{
func
generateZipFileMap
(
zipEntries
[]
*
zip
.
File
)
zipFileMap
{
f
or
_
,
e
:=
range
entries
{
f
ileMap
:=
make
(
map
[
string
]
*
zip
.
File
)
if
e
.
Name
==
path
{
return
true
for
_
,
entry
:=
range
zipEntries
{
}
fileMap
[
entry
.
Name
]
=
entry
}
}
return
f
alse
return
f
ileMap
}
}
func
generateZipMetadata
(
output
io
.
Writer
,
archive
*
zip
.
Reader
)
error
{
func
generateZipMetadata
(
output
io
.
Writer
,
archive
*
zip
.
Reader
)
error
{
...
@@ -108,9 +108,10 @@ func generateZipMetadata(output io.Writer, archive *zip.Reader) error {
...
@@ -108,9 +108,10 @@ func generateZipMetadata(output io.Writer, archive *zip.Reader) error {
return
err
return
err
}
}
fileMap
:=
generateZipFileMap
(
archive
.
File
)
// Write all files
// Write all files
for
_
,
entry
:=
range
archive
.
File
{
for
_
,
entry
:=
range
archive
.
File
{
if
err
:=
handleZipEntryMetadata
(
output
,
entry
,
archive
.
File
);
err
!=
nil
{
if
err
:=
handleZipEntryMetadata
(
output
,
entry
,
fileMap
);
err
!=
nil
{
return
err
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