Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ls: print "size":0 for empty files in JSON #3257

Merged
merged 1 commit into from
May 23, 2021

Conversation

greatroar
Copy link
Contributor

@greatroar greatroar commented Feb 1, 2021

What does this PR change? What problem does it solve?

It causes restic ls --json to print empty files as having "size":0 instead of omitting the size, as was done previously.

It also removes unconditional panics introduced in 200f095, which broke JSON printing entirely.

Was the change discussed in an issue or in the forum before?

Fixes #3247.

Checklist

  • I have read the Contribution Guidelines
  • I have enabled maintainer edits for this PR
  • I have added tests for all changes in this PR
  • I have added documentation for the changes (in the manual)
  • There's a new file in changelog/unreleased/ that describes the changes for our users (template here)
  • I have run gofmt on the code in all commits
  • All commit messages are formatted in the same style as the other commits in the repo
  • I'm done, this Pull Request is ready for review

fd0
fd0 previously requested changes May 17, 2021
Copy link
Member

@fd0 fd0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a better way (IMHO): Make Size a pointer to uint64, and set it to a non-nil value when the size should be printed. The code below misses error handling, but passes the tests you added:

type lsNode struct {
	Name       string      `json:"name"`
	Type       string      `json:"type"`
	Path       string      `json:"path"`
	UID        uint32      `json:"uid"`
	GID        uint32      `json:"gid"`
	Size       *uint64     `json:"size,omitempty"`
	Mode       os.FileMode `json:"mode,omitempty"`
	ModTime    time.Time   `json:"mtime,omitempty"`
	AccessTime time.Time   `json:"atime,omitempty"`
	ChangeTime time.Time   `json:"ctime,omitempty"`
	StructType string      `json:"struct_type"` // "node"
}

// Print node in our custom JSON format, followed by a newline.
func lsNodeJSON(w io.Writer, path string, node *restic.Node) {
	var size uint64

	n := lsNode{
		Name:       node.Name,
		Type:       node.Type,
		Path:       path,
		UID:        node.UID,
		GID:        node.GID,
		Mode:       node.Mode,
		ModTime:    node.ModTime,
		AccessTime: node.AccessTime,
		ChangeTime: node.ChangeTime,
		StructType: "node",
	}

	if node.Type == "file" {
		size = node.Size
		n.Size = &size
	}

	json.NewEncoder(w).Encode(n)
}

What do you think?

@greatroar
Copy link
Contributor Author

Good idea, implemented that.

@greatroar greatroar force-pushed the ls-json-empty branch 2 times, most recently from eb23c6c to bf641cb Compare May 21, 2021 18:55
Copy link
Member

@MichaelEischer MichaelEischer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@MichaelEischer MichaelEischer dismissed fd0’s stale review May 23, 2021 11:49

Feedback has been addressed

@MichaelEischer MichaelEischer merged commit 7eb6372 into restic:master May 23, 2021
@greatroar greatroar deleted the ls-json-empty branch May 24, 2021 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"ls --json " does not output a size for all Windows files
3 participants