Skip to content
This repository has been archived by the owner on Mar 29, 2023. It is now read-only.

initial support for Mode and ModTime on Node interface #31

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"io"
"os"
"time"
)

var (
Expand All @@ -17,6 +18,13 @@ var (
type Node interface {
io.Closer

// Mode returns the file's mode
Mode() os.FileMode

// ModTime returns the files last modification time.
// If the last modification time is unknown/unspecified `mtime` is zero.
ModTime() (mtime time.Time)

// Size returns size of this file (if this file is a directory, total size of
// all files stored in the tree should be returned). Some implementations may
// choose not to implement this
Expand Down
67 changes: 58 additions & 9 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package files
import (
"io"
"mime/multipart"
"os"
"strings"
"testing"
"time"
)

func TestSliceFiles(t *testing.T) {
Expand Down Expand Up @@ -48,36 +50,62 @@ func TestReaderFiles(t *testing.T) {
t.Fatal("Expected EOF when reading after close")
}
}

func TestReaderFileStat(t *testing.T) {
reader := strings.NewReader("beep boop")
mode := os.FileMode(0754)
mtime := time.Date(2020, 11, 2, 12, 27, 35, 55555, time.UTC)
stat := &mockFileInfo{name: "test", mode: mode, mtime: mtime}

rf := NewReaderStatFile(reader, stat)
if rf.Mode() != mode {
t.Fatalf("Expected file mode to be [%v] but got [%v]", mode, rf.Mode())
}
if rf.ModTime() != mtime {
t.Fatalf("Expected file modified time to be [%v] but got [%v]", mtime, rf.ModTime())
}
}

func TestMultipartFiles(t *testing.T) {
data := `
--Boundary!
Content-Type: text/plain
Content-Disposition: file; filename="name"
Content-Disposition: form-data; name="file-0?mode=0754&mtime=1604320500&mtime-nsecs=55555"; filename="file1"
Some-Header: beep

beep
--Boundary!
Content-Type: application/x-directory
Content-Disposition: file; filename="dir"
Content-Disposition: form-data; name="dir-0?mode=755&mtime=1604320500"; ans=42; filename="dir1"

--Boundary!
Content-Type: text/plain
Content-Disposition: file; filename="dir/nested"
Content-Disposition: form-data; name="file"; filename="dir1/nested"

some content
--Boundary!
Content-Type: text/plain
Content-Disposition: form-data; name="file?mode=600"; filename="dir1/nested2"; ans=42

some content
--Boundary!
Content-Type: application/symlink
Content-Disposition: file; filename="dir/simlynk"
Content-Disposition: form-data; name="file-5"; filename="dir1/simlynk"

anotherfile
--Boundary!
Content-Type: application/symlink
Content-Disposition: form-data; name="file?mtime=1604320500"; filename="dir1/simlynk2"

anotherfile
--Boundary!
Content-Type: text/plain
Content-Disposition: file; filename="implicit1/implicit2/deep_implicit"
Content-Disposition: form-data; name="dir?mode=0644"; filename="implicit1/implicit2/deep_implicit"

implicit file1
--Boundary!
Content-Type: text/plain
Content-Disposition: file; filename="implicit1/shallow_implicit"
Content-Disposition: form-data; name="dir?mode=755&mtime=1604320500"; filename="implicit1/shallow_implicit"

implicit file2
--Boundary!--
Expand All @@ -94,22 +122,40 @@ implicit file2
CheckDir(t, dir, []Event{
{
kind: TFile,
name: "name",
name: "file1",
value: "beep",
mode: 0754,
mtime: time.Unix(1604320500, 55555),
},
{
kind: TDirStart,
name: "dir",
kind: TDirStart,
name: "dir1",
mode: 0755,
mtime: time.Unix(1604320500, 0),
},
{
kind: TFile,
name: "nested",
value: "some content",
},
{
kind: TFile,
name: "nested2",
value: "some content",
mode: 0600,
},
{
kind: TSymlink,
name: "simlynk",
value: "anotherfile",
mode: 0777,
},
{
kind: TSymlink,
name: "simlynk2",
value: "anotherfile",
mode: 0777,
mtime: time.Unix(1604320500, 0),
},
{
kind: TDirEnd,
Expand All @@ -126,6 +172,7 @@ implicit file2
kind: TFile,
name: "deep_implicit",
value: "implicit file1",
mode: 0644,
},
{
kind: TDirEnd,
Expand All @@ -134,6 +181,8 @@ implicit file2
kind: TFile,
name: "shallow_implicit",
value: "implicit file2",
mode: 0755,
mtime: time.Unix(1604320500, 0),
},
{
kind: TDirEnd,
Expand Down
18 changes: 17 additions & 1 deletion filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@ import (
"os"
"path/filepath"
"testing"
"time"
)

type mockFileInfo struct {
os.FileInfo
name string
name string
mode os.FileMode
mtime time.Time
size int64
}

func (m *mockFileInfo) Name() string {
return m.name
}

func (m *mockFileInfo) Mode() os.FileMode {
return m.mode
}

func (m *mockFileInfo) ModTime() time.Time {
return m.mtime
}

func (m *mockFileInfo) Size() int64 {
return m.size
}

func (m *mockFileInfo) Sys() interface{} {
return nil
}
Expand Down
12 changes: 12 additions & 0 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package files

import (
"io"
"os"
"testing"
"time"
)

type Kind int
Expand All @@ -18,6 +20,8 @@ type Event struct {
kind Kind
name string
value string
mode os.FileMode
mtime time.Time
}

func CheckDir(t *testing.T, dir Directory, expected []Event) {
Expand Down Expand Up @@ -50,6 +54,14 @@ func CheckDir(t *testing.T, dir Directory, expected []Event) {
t.Fatalf("[%d] expected filename to be %q", i, next.name)
}

if it.Node().Mode()&os.ModePerm != next.mode {
t.Fatalf("[%d] expected mode for '%s' to be %O, got %O", i, it.Name(), next.mode, it.Node().Mode())
}

if !next.mtime.IsZero() && !it.Node().ModTime().Equal(next.mtime) {
t.Fatalf("[%d] expected modification time for '%s' to be %q", i, it.Name(), next.mtime)
}

switch next.kind {
case TFile:
mf, ok := it.Node().(File)
Expand Down
22 changes: 19 additions & 3 deletions linkfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,37 @@ package files
import (
"os"
"strings"
"time"
)

type Symlink struct {
Target string

stat os.FileInfo
mtime time.Time
reader strings.Reader
}

func NewLinkFile(target string, stat os.FileInfo) File {
lf := &Symlink{Target: target, stat: stat}
mtime := time.Time{}
if stat != nil {
mtime = stat.ModTime()
}
return NewSymlinkFile(target, mtime)
}

func NewSymlinkFile(target string, mtime time.Time) File {
lf := &Symlink{Target: target, mtime: mtime}
lf.reader.Reset(lf.Target)
return lf
}

func (lf *Symlink) Mode() os.FileMode {
return os.ModeSymlink | os.ModePerm
}

func (lf *Symlink) ModTime() time.Time {
return lf.mtime
}

func (lf *Symlink) Close() error {
return nil
}
Expand Down
41 changes: 34 additions & 7 deletions multifilereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"net/textproto"
"net/url"
"path"
"strconv"
"strings"
"sync"
)

Expand Down Expand Up @@ -88,12 +90,7 @@ func (mfr *MultiFileReader) Read(buf []byte) (written int, err error) {
// write the boundary and headers
header := make(textproto.MIMEHeader)
filename := url.QueryEscape(path.Join(path.Join(mfr.path...), entry.Name()))
dispositionPrefix := "attachment"
if mfr.form {
dispositionPrefix = "form-data; name=\"file\""
}

header.Set("Content-Disposition", fmt.Sprintf("%s; filename=\"%s\"", dispositionPrefix, filename))
mfr.addContentDisposition(header, filename)

var contentType string

Expand All @@ -112,7 +109,7 @@ func (mfr *MultiFileReader) Read(buf []byte) (written int, err error) {
return 0, ErrNotSupported
}

header.Set("Content-Type", contentType)
header.Set(contentTypeHeader, contentType)
if rf, ok := entry.Node().(FileInfo); ok {
header.Set("abspath", rf.AbsPath())
}
Expand Down Expand Up @@ -146,6 +143,36 @@ func (mfr *MultiFileReader) Read(buf []byte) (written int, err error) {
return written, nil
}

func (mfr *MultiFileReader) addContentDisposition(header textproto.MIMEHeader, filename string) {
sb := &strings.Builder{}
params := url.Values{}

if mode := mfr.currentFile.Mode(); mode != 0 {
params.Add("mode", "0"+strconv.FormatUint(uint64(mode), 8))
}
if mtime := mfr.currentFile.ModTime(); !mtime.IsZero() {
params.Add("mtime", strconv.FormatInt(mtime.Unix(), 10))
if n := mtime.Nanosecond(); n > 0 {
params.Add("mtime-nsecs", strconv.FormatInt(int64(n), 10))
}
}

sb.Grow(120)
if mfr.form {
sb.WriteString("form-data; name=\"file")
if len(params) > 0 {
fmt.Fprintf(sb, "?%s", params.Encode())
}
sb.WriteString("\"")
} else {
sb.WriteString("attachment")
}

fmt.Fprintf(sb, "; filename=\"%s\"", filename)

header.Set(contentDispositionHeader, sb.String())
}

// Boundary returns the boundary string to be used to separate files in the multipart data
func (mfr *MultiFileReader) Boundary() string {
return mfr.mpWriter.Boundary()
Expand Down
26 changes: 22 additions & 4 deletions multifilereader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,29 @@ package files
import (
"io"
"mime/multipart"
"strings"
"testing"
"time"
)

var text = "Some text! :)"

func getTestMultiFileReader(t *testing.T) *MultiFileReader {
sf := NewMapDirectory(map[string]Node{
"file.txt": NewBytesFile([]byte(text)),
"file.txt": NewReaderStatFile(
strings.NewReader(text),
&mockFileInfo{name: "file.txt", mode: 0, mtime: time.Time{}}),
"boop": NewMapDirectory(map[string]Node{
"a.txt": NewBytesFile([]byte("bleep")),
"b.txt": NewBytesFile([]byte("bloop")),
"a.txt": NewReaderStatFile(
strings.NewReader("bleep"),
&mockFileInfo{name: "a.txt", mode: 0744, mtime: time.Time{}}),
"b.txt": NewReaderStatFile(
strings.NewReader("bloop"),
&mockFileInfo{name: "b.txt", mode: 0666, mtime: time.Unix(1604320500, 0)}),
}),
"beep.txt": NewBytesFile([]byte("beep")),
"beep.txt": NewReaderStatFile(
strings.NewReader("beep"),
&mockFileInfo{name: "beep.txt", mode: 0754, mtime: time.Unix(1604320500, 55555)}),
})

// testing output by reading it with the go stdlib "mime/multipart" Reader
Expand All @@ -36,6 +46,14 @@ func TestMultiFileReaderToMultiFile(t *testing.T) {
t.Fatal("iterator didn't work as expected")
}

n := it.Node()
if n.Mode() != 0754 {
t.Fatal("unexpected file mode")
}
if n.ModTime() != time.Unix(1604320500, 55555) {
t.Fatal("unexpected last modification time")
}

if !it.Next() || it.Name() != "boop" || DirFromEntry(it) == nil {
t.Fatal("iterator didn't work as expected")
}
Expand Down
Loading