archive/zip: read and write unix file modes

R=golang-dev, rsc, adg
CC=golang-dev
https://golang.org/cl/5124044
This commit is contained in:
Gustavo Niemeyer 2011-09-25 20:48:03 -03:00
parent d53afb8d83
commit ecda69e667
5 changed files with 86 additions and 15 deletions

View file

@ -28,6 +28,9 @@ const (
directoryHeaderLen = 46 // + filename + extra + comment
directoryEndLen = 22 // + comment
dataDescriptorLen = 12
// Constants for the first byte in CreatorVersion
creatorUnix = 3
)
type FileHeader struct {
@ -42,6 +45,7 @@ type FileHeader struct {
CompressedSize uint32
UncompressedSize uint32
Extra []byte
ExternalAttrs uint32 // Meaning depends on CreatorVersion
Comment string
}
@ -89,3 +93,18 @@ func (h *FileHeader) Mtime_ns() int64 {
t := msDosTimeToTime(h.ModifiedDate, h.ModifiedTime)
return t.Seconds() * 1e9
}
// Mode returns the permission and mode bits for the FileHeader.
// An error is returned in case the information is not available.
func (h *FileHeader) Mode() (mode uint32, err os.Error) {
if h.CreatorVersion>>8 == creatorUnix {
return h.ExternalAttrs >> 16, nil
}
return 0, os.NewError("file mode not available")
}
// SetMode changes the permission and mode bits for the FileHeader.
func (h *FileHeader) SetMode(mode uint32) {
h.CreatorVersion = h.CreatorVersion&0xff | creatorUnix<<8
h.ExternalAttrs = mode << 16
}