mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-25 18:07:07 +09:00
util: Improve and rename EscapePath() to DetermineEscapePath()
If the new URL encoded path is found then it has precedence over the '%' escaped path. In case none of both is found the new URL approach is used.
This commit is contained in:
@@ -79,7 +79,7 @@ func (b *Buffer) Backup() error {
|
|||||||
os.Mkdir(backupdir, os.ModePerm)
|
os.Mkdir(backupdir, os.ModePerm)
|
||||||
}
|
}
|
||||||
|
|
||||||
name := filepath.Join(backupdir, util.EscapePath(b.AbsPath))
|
name := util.DetermineEscapePath(backupdir, b.AbsPath)
|
||||||
|
|
||||||
err = overwriteFile(name, encoding.Nop, func(file io.Writer) (e error) {
|
err = overwriteFile(name, encoding.Nop, func(file io.Writer) (e error) {
|
||||||
b.Lock()
|
b.Lock()
|
||||||
@@ -123,7 +123,7 @@ func (b *Buffer) RemoveBackup() {
|
|||||||
if !b.Settings["backup"].(bool) || b.Settings["permbackup"].(bool) || b.Path == "" || b.Type != BTDefault {
|
if !b.Settings["backup"].(bool) || b.Settings["permbackup"].(bool) || b.Path == "" || b.Type != BTDefault {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
f := filepath.Join(config.ConfigDir, "backups", util.EscapePath(b.AbsPath))
|
f := util.DetermineEscapePath(filepath.Join(config.ConfigDir, "backups"), b.AbsPath)
|
||||||
os.Remove(f)
|
os.Remove(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,13 +131,13 @@ func (b *Buffer) RemoveBackup() {
|
|||||||
// Returns true if a backup was applied
|
// Returns true if a backup was applied
|
||||||
func (b *Buffer) ApplyBackup(fsize int64) (bool, bool) {
|
func (b *Buffer) ApplyBackup(fsize int64) (bool, bool) {
|
||||||
if b.Settings["backup"].(bool) && !b.Settings["permbackup"].(bool) && len(b.Path) > 0 && b.Type == BTDefault {
|
if b.Settings["backup"].(bool) && !b.Settings["permbackup"].(bool) && len(b.Path) > 0 && b.Type == BTDefault {
|
||||||
backupfile := filepath.Join(config.ConfigDir, "backups", util.EscapePath(b.AbsPath))
|
backupfile := util.DetermineEscapePath(filepath.Join(config.ConfigDir, "backups"), b.AbsPath)
|
||||||
if info, err := os.Stat(backupfile); err == nil {
|
if info, err := os.Stat(backupfile); err == nil {
|
||||||
backup, err := os.Open(backupfile)
|
backup, err := os.Open(backupfile)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
defer backup.Close()
|
defer backup.Close()
|
||||||
t := info.ModTime()
|
t := info.ModTime()
|
||||||
msg := fmt.Sprintf(backupMsg, t.Format("Mon Jan _2 at 15:04, 2006"), util.EscapePath(b.AbsPath))
|
msg := fmt.Sprintf(backupMsg, t.Format("Mon Jan _2 at 15:04, 2006"), backupfile)
|
||||||
choice := screen.TermPrompt(msg, []string{"r", "i", "a", "recover", "ignore", "abort"}, true)
|
choice := screen.TermPrompt(msg, []string{"r", "i", "a", "recover", "ignore", "abort"}, true)
|
||||||
|
|
||||||
if choice%3 == 0 {
|
if choice%3 == 0 {
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ func (b *Buffer) Serialize() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
name := filepath.Join(config.ConfigDir, "buffers", util.EscapePath(b.AbsPath))
|
name := util.DetermineEscapePath(filepath.Join(config.ConfigDir, "buffers"), b.AbsPath)
|
||||||
|
|
||||||
return overwriteFile(name, encoding.Nop, func(file io.Writer) error {
|
return overwriteFile(name, encoding.Nop, func(file io.Writer) error {
|
||||||
err := gob.NewEncoder(file).Encode(SerializedBuffer{
|
err := gob.NewEncoder(file).Encode(SerializedBuffer{
|
||||||
@@ -50,7 +50,7 @@ func (b *Buffer) Unserialize() error {
|
|||||||
if b.Path == "" {
|
if b.Path == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
file, err := os.Open(filepath.Join(config.ConfigDir, "buffers", util.EscapePath(b.AbsPath)))
|
file, err := os.Open(util.DetermineEscapePath(filepath.Join(config.ConfigDir, "buffers"), b.AbsPath))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
var buffer SerializedBuffer
|
var buffer SerializedBuffer
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -408,8 +409,13 @@ func GetModTime(path string) (time.Time, error) {
|
|||||||
return info.ModTime(), nil
|
return info.ModTime(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// EscapePath replaces every path separator in a given path with a %
|
// EscapePathUrl encodes the path in URL query form
|
||||||
func EscapePath(path string) string {
|
func EscapePathUrl(path string) string {
|
||||||
|
return url.QueryEscape(filepath.ToSlash(path))
|
||||||
|
}
|
||||||
|
|
||||||
|
// EscapePathLegacy replaces every path separator in a given path with a %
|
||||||
|
func EscapePathLegacy(path string) string {
|
||||||
path = filepath.ToSlash(path)
|
path = filepath.ToSlash(path)
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
// ':' is not valid in a path name on Windows but is ok on Unix
|
// ':' is not valid in a path name on Windows but is ok on Unix
|
||||||
@@ -418,6 +424,24 @@ func EscapePath(path string) string {
|
|||||||
return strings.ReplaceAll(path, "/", "%")
|
return strings.ReplaceAll(path, "/", "%")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DetermineEscapePath escapes a path, determining whether it should be escaped
|
||||||
|
// using URL encoding (preferred, since it encodes unambiguously) or
|
||||||
|
// legacy encoding with '%' (for backward compatibility, if the legacy-escaped
|
||||||
|
// path exists in the given directory).
|
||||||
|
func DetermineEscapePath(dir string, path string) string {
|
||||||
|
url := filepath.Join(dir, EscapePathUrl(path))
|
||||||
|
if _, err := os.Stat(url); err == nil {
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
|
||||||
|
legacy := filepath.Join(dir, EscapePathLegacy(path))
|
||||||
|
if _, err := os.Stat(legacy); err == nil {
|
||||||
|
return legacy
|
||||||
|
}
|
||||||
|
|
||||||
|
return url
|
||||||
|
}
|
||||||
|
|
||||||
// GetLeadingWhitespace returns the leading whitespace of the given byte array
|
// GetLeadingWhitespace returns the leading whitespace of the given byte array
|
||||||
func GetLeadingWhitespace(b []byte) []byte {
|
func GetLeadingWhitespace(b []byte) []byte {
|
||||||
ws := []byte{}
|
ws := []byte{}
|
||||||
|
|||||||
Reference in New Issue
Block a user