Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
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-ce
Commits
f1d320ed
Commit
f1d320ed
authored
Jan 26, 2016
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor function that generates artifacts metadata
parent
3353d2e7
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
46 deletions
+42
-46
internal/zipartifacts/metadata.go
internal/zipartifacts/metadata.go
+42
-46
No files found.
internal/zipartifacts/metadata.go
View file @
f1d320ed
...
...
@@ -9,6 +9,7 @@ import (
"os"
"path"
"strconv"
"strings"
"time"
)
...
...
@@ -21,7 +22,7 @@ type metadata struct {
Comment
string
`json:"comment,omitempty"`
}
type
zip
File
Map
map
[
string
]
*
zip
.
File
type
zip
Dir
Map
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
"
...
...
@@ -47,71 +48,66 @@ func (m metadata) writeEncoded(output io.Writer) error {
}
func
writeZipEntryMetadata
(
output
io
.
Writer
,
entry
*
zip
.
File
)
error
{
err
:=
writeString
(
output
,
entry
.
Name
)
if
err
!=
nil
{
if
err
:=
writeString
(
output
,
entry
.
Name
);
err
!=
nil
{
return
err
}
err
=
newMetadata
(
entry
)
.
writeEncoded
(
output
)
if
err
!=
nil
{
if
err
:=
newMetadata
(
entry
)
.
writeEncoded
(
output
);
err
!=
nil
{
return
err
}
return
nil
}
func
handleZipEntryMetadata
(
output
io
.
Writer
,
entry
*
zip
.
File
,
fileMap
zipFileMap
)
error
{
var
dirNodes
[]
string
entryPath
:=
entry
.
Name
for
{
entryPath
=
path
.
Dir
(
entryPath
)
if
entryPath
==
"."
||
entryPath
==
"/"
{
break
}
dirNodes
=
append
([]
string
{
entryPath
+
"/"
},
dirNodes
...
)
func
generateZipMetadata
(
output
io
.
Writer
,
archive
*
zip
.
Reader
)
error
{
// Write metadata header
if
err
:=
writeString
(
output
,
MetadataHeader
);
err
!=
nil
{
return
err
}
for
_
,
d
:=
range
dirNodes
{
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
)
}
// Write empty error header
if
err
:=
writeString
(
output
,
"{}"
);
err
!=
nil
{
return
err
}
err
:=
writeZipEntryMetadata
(
output
,
entry
)
return
err
}
func
generateZipFileMap
(
zipEntries
[]
*
zip
.
File
)
zipFileMap
{
fileMap
:=
make
(
zipFileMap
,
len
(
zipEntries
))
for
_
,
entry
:=
range
zipEntries
{
fileMap
[
entry
.
Name
]
=
entry
// Create map od directories in zip archive
dirMap
:=
make
(
zipDirMap
,
len
(
archive
.
File
))
for
_
,
entry
:=
range
archive
.
File
{
if
strings
.
HasSuffix
(
entry
.
Name
,
"/"
)
{
dirMap
[
entry
.
Name
]
=
entry
}
}
return
fileMap
}
// Add missing entries
var
missingEntries
[]
*
zip
.
File
func
generateZipMetadata
(
output
io
.
Writer
,
archive
*
zip
.
Reader
)
error
{
if
err
:=
writeString
(
output
,
MetadataHeader
);
err
!=
nil
{
return
err
for
_
,
entry
:=
range
archive
.
File
{
entryPath
:=
entry
.
Name
for
{
entryPath
=
path
.
Dir
(
entryPath
)
if
entryPath
==
"."
||
entryPath
==
"/"
{
break
}
if
_
,
ok
:=
dirMap
[
entryPath
];
!
ok
{
var
missingHeader
zip
.
FileHeader
missingHeader
.
Name
=
entryPath
missingHeader
.
SetModTime
(
time
.
Now
())
missingHeader
.
SetMode
(
os
.
FileMode
(
uint32
(
0755
)))
missingEntry
:=
&
zip
.
File
{
FileHeader
:
missingHeader
}
dirMap
[
entryPath
]
=
missingEntry
missingEntries
=
append
(
missingEntries
,
missingEntry
)
}
}
}
// Write empty error string
if
err
:=
writeString
(
output
,
"{}"
);
err
!=
nil
{
return
err
}
archive
.
File
=
append
(
archive
.
File
,
missingEntries
...
)
fileMap
:=
generateZipFileMap
(
archive
.
File
)
// Write all files
for
_
,
entry
:=
range
archive
.
File
{
if
err
:=
handleZipEntryMetadata
(
output
,
entry
,
fileMap
);
err
!=
nil
{
if
err
:=
writeZipEntryMetadata
(
output
,
entry
);
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