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

support file mode and last modified time for ipfs add #66

Closed
wants to merge 4 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 55 additions & 0 deletions options/unixfs.go
Expand Up @@ -3,6 +3,8 @@ package options
import (
"errors"
"fmt"
"os"
"time"

cid "github.com/ipfs/go-cid"
dag "github.com/ipfs/go-merkledag"
Expand Down Expand Up @@ -36,6 +38,11 @@ type UnixfsAddSettings struct {
Events chan<- interface{}
Silent bool
Progress bool

PreserveMode bool
PreserveMtime bool
Mode os.FileMode
Mtime time.Time
}

type UnixfsLsSettings struct {
Expand Down Expand Up @@ -66,6 +73,11 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix,
Events: nil,
Silent: false,
Progress: false,

PreserveMode: false,
PreserveMtime: false,
Mode: 0,
Mtime: time.Time{},
}

for _, opt := range opts {
Expand Down Expand Up @@ -103,6 +115,14 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix,
}
}

if !options.Mtime.IsZero() && options.PreserveMtime {
options.PreserveMtime = false
}

if options.Mode != 0 && options.PreserveMode {
options.PreserveMode = false
}

// cidV1 -> raw blocks (by default)
if options.CidVersion > 0 && !options.RawLeavesSet {
options.RawLeaves = true
Expand Down Expand Up @@ -283,3 +303,38 @@ func (unixfsOpts) ResolveChildren(resolve bool) UnixfsLsOption {
return nil
}
}

// PreserveMode tells the adder to store the file permissions
func (unixfsOpts) PreserveMode(enable bool) UnixfsAddOption {
return func(settings *UnixfsAddSettings) error {
settings.PreserveMode = enable
return nil
}
}

// PreserveMtime tells the adder to store the file modification time
func (unixfsOpts) PreserveMtime(enable bool) UnixfsAddOption {
return func(settings *UnixfsAddSettings) error {
settings.PreserveMtime = enable
return nil
}
}

// Mode represents a unix file mode
func (unixfsOpts) Mode(mode os.FileMode) UnixfsAddOption {
return func(settings *UnixfsAddSettings) error {
settings.Mode = mode
return nil
}
}

// Mtime represents a unix file mtime
func (unixfsOpts) Mtime(seconds int64, nsecs uint32) UnixfsAddOption {
return func(settings *UnixfsAddSettings) error {
if nsecs > 999999999 {
return errors.New("mtime nanoseconds must be in range [1, 999999999]")
}
settings.Mtime = time.Unix(seconds, int64(nsecs))
return nil
}
}
12 changes: 8 additions & 4 deletions unixfs.go
Expand Up @@ -8,13 +8,17 @@ import (

"github.com/ipfs/go-cid"
files "github.com/ipfs/go-ipfs-files"
"os"
)

type AddEvent struct {
Name string
Path path.Resolved `json:",omitempty"`
Bytes int64 `json:",omitempty"`
Size string `json:",omitempty"`
Name string
Path path.Resolved `json:",omitempty"`
Bytes int64 `json:",omitempty"`
Size string `json:",omitempty"`
Mode os.FileMode `json:",omitempty"`
Mtime int64 `json:",omitempty"`
MtimeNsecs int `json:",omitempty"`
}

// FileType is an enum of possible UnixFS file types.
Expand Down