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

File can't be found if "/" is in file name. #286

Open
Mikowmer opened this issue Dec 30, 2022 · 4 comments
Open

File can't be found if "/" is in file name. #286

Mikowmer opened this issue Dec 30, 2022 · 4 comments

Comments

@Mikowmer
Copy link

Mikowmer commented Dec 30, 2022

As in the title. I have numerous files that have the forward slash character in the title. It shows up when I use ls, however attempting to use stat or get on those files gives an error saying that the file doesn't exist.

I have tried escaping the forward slash, but even that hasn't worked.

EDIT: Anything with a ' also encounters an error, specifically "Error: EOF found when expecting closing quote". Escaping this one does work, but it'll need fixing in the autocomplete.

@ddvk
Copy link
Collaborator

ddvk commented Jan 1, 2023

forward slash is problematic, it is used as a folder separator and cannot be escaped.

@Mikowmer
Copy link
Author

Mikowmer commented Jan 2, 2023

I... see what you mean. When it splits the path to navigate, it does a naive split (see rmapi/path.go)

The only solution I can see that would fix this would be to add an argument that would tell stat, rm, etc to ignore the forward slash. This would work for current folder operations, but not really anything deeper

An extension of this idea to allow for accessing deeper into the file structure is that spaces get used instead of forward slashes to delineate folders, and so each folder before the final folder/file gets entered as a separate argument into the shell command rather than splitting later on. Spaces already need to be escaped anyway if they are in a file/folder name to avoid creating a new argument, so this would solve the problem rather neatly.

This doesn't need to be the default behavior, so as mentioned before an argument could be passed that would use this alternative method.

@Mikowmer
Copy link
Author

Mikowmer commented Jan 5, 2023

Proof of concept for using arguments to separate folders and files rather than "/":

Rewriting statCmd in shell/stat.go

func statCmd(ctx *ShellCtxt) *ishell.Cmd {
	return &ishell.Cmd{
		Name:      "stat",
		Help:      "fetch entry metadata",
		Completer: createEntryCompleter(ctx),
		Func: func(c *ishell.Context) {
			if len(c.Args) == 0 {
				c.Err(errors.New("missing source file"))
				return
			}

			var node *model.Node
			var err error

			if (c.Args[0] == "-ap") && (len(c.Args) != 1) {
				node, err = ctx.api.Filetree().NodeByArgPath(c.Args[1:], ctx.node)
			} else {
				srcName := c.Args[0]

				node, err = ctx.api.Filetree().NodeByPath(srcName, ctx.node)
			}

			if err != nil {
				c.Err(errors.New("file doesn't exist"))
				return
			}

			jsn, err := json.MarshalIndent(node.Document, "", "  ")

			if err != nil {
				c.Err(errors.New("can't serialize to json"))
				return
			}

			c.Println(string(jsn))
		},
	}
}

Adding function to filetree/filetree.go

func (ctx *FileTreeCtx) NodeByArgPath(entries []string, current *model.Node) (*model.Node, error) {
	if current == nil {
		current = ctx.Root()
	}

	if len(entries) == 0 {
		return current, nil
	}

	i := 0
	if entries[i] == "" {
		current = ctx.Root()
		i++
	}

	for i < len(entries) {
		if entries[i] == "" || entries[i] == "." {
			i++
			continue
		}

		if entries[i] == ".." {
			if current.Parent == nil {
				current = ctx.Root()
			} else {
				current = current.Parent
			}

			i++
			continue
		}

		var err error
		current, err = current.FindByName(entries[i])

		if err != nil {
			return nil, err
		}

		i++
	}

	return current, nil
}

This works and gave me the stats for a file with "/" in its name. It also works when working multiple folders deep.

@HackerDaGreat57
Copy link

Maybe we could use a combination of grave (`) and ampersand (&) symbols to indicate escapes?

Frankly this would reduce the number of incompatible files by a LOT. who uses & and ` in a file name anyway??

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

No branches or pull requests

3 participants