Ver Fonte

updates

Toby Chui há 5 anos atrás
pai
commit
0a7b3602c0
2 ficheiros alterados com 144 adições e 15 exclusões
  1. BIN
      fsconv.exe
  2. 144 15
      main.go

BIN
fsconv.exe


+ 144 - 15
main.go

@@ -22,10 +22,9 @@ import (
 	"path"
 	"path/filepath"
 	"reflect"
-	"strconv"
 	"sort"
-	"regexp"
 	"strings"
+	"bytes"
 	"encoding/hex"
 )
 
@@ -119,17 +118,15 @@ func scan_recursive(dir_path string, ignore []string) ([]string, []string) {
 	return folders, files
 }
 
-func hex2bin(s string) []byte {
-	ret, _ := hex.DecodeString(s)
-	return ret
+func hex2bin(s string) ([]byte, error) {
+	ret, err := hex.DecodeString(s)
+	return ret, err
 }
 
-func Bin2hex(str string) (string, error) {
-	i, err := strconv.ParseInt(str, 2, 0)
-	if err != nil {
-		return "", err
-	}
-	return strconv.FormatInt(i, 16), nil
+func bin2hex(s string) (string) {
+	src := []byte(s)
+	encodedStr := hex.EncodeToString(src)
+	return encodedStr;
 }
 
 type ByLen []string
@@ -160,9 +157,8 @@ func main() {
 			//Check if it has the inith prefix
 			if len(name) > 5 && name[0:5] == "inith"{
 				//This is hex filename. Translate its name to normal filename
-				originalName := string(hex2bin(name[5:]))
-				var re = regexp.MustCompile(`/[/\\?%*:|"<>]/g`)
-				safeName := re.ReplaceAllString(originalName, `-`)
+				originalName,_ := hex2bin(name[5:])
+				safeName := getSafeFilename(string(originalName))
 				fmt.Println(filename + " -> " + safeName + extension)
 				err := os.Rename(files[i], path.Dir(thisFilepath) + "/" + safeName + extension)
 				if (err != nil){
@@ -174,9 +170,20 @@ func main() {
 		
 		//Sort the array of folders by string length, hence deeper folder will be renamed first
 		sort.Sort(ByLen(folders))
+		applicationStartupPath, _ := Realpath(path.Dir(os.Args[0]))
 		for j := 0; j < len(folders); j++{
 			foldername := path.Base(folders[j])
-			fmt.Println(foldername)
+			if (foldername != applicationStartupPath){
+				//All subdirectories except the root
+				if (isHex(filepath.Base(foldername))){
+					//This folder use hex foldername. Convert it to UTF8 filename
+					convFoldername, _ := hex2bin(filepath.Base(foldername));
+					safeName := getSafeFilename(string(convFoldername))
+					fmt.Println(filepath.Base(foldername) + " -> " + string(safeName))
+					os.Rename(foldername,filepath.Dir(foldername) + "/" + string(safeName))
+				}
+			}
+			
 		}
 
 
@@ -191,3 +198,125 @@ func main() {
 		//Given target directory
 	}
 }
+
+
+
+func getSafeFilename(s string) string{
+	replacer := strings.NewReplacer("/", "", "\\","", "@","-", "&","-", "*","-", "<","-", ">","-", "|","-", "?","-", ":","-")
+	safeName := replacer.Replace(string(s))
+	return safeName
+}
+
+func isHex(s string) bool{
+	_, err := hex2bin(s)
+	if err != nil {
+		return false
+	}
+	return true
+}
+
+
+//Realpath from https://github.com/yookoala/realpath/blob/master/realpath.go
+// Realpath returns the real path of a given file in the os
+func Realpath(fpath string) (string, error) {
+
+	if len(fpath) == 0 {
+		return "", os.ErrInvalid
+	}
+
+	if !filepath.IsAbs(fpath) {
+		pwd, err := os.Getwd()
+		if err != nil {
+			return "", err
+		}
+		fpath = filepath.Join(pwd, fpath)
+	}
+
+	path := []byte(fpath)
+	nlinks := 0
+	start := 1
+	prev := 1
+	for start < len(path) {
+		c := nextComponent(path, start)
+		cur := c[start:]
+
+		switch {
+
+		case len(cur) == 0:
+			copy(path[start:], path[start+1:])
+			path = path[0 : len(path)-1]
+
+		case len(cur) == 1 && cur[0] == '.':
+			if start+2 < len(path) {
+				copy(path[start:], path[start+2:])
+			}
+			path = path[0 : len(path)-2]
+
+		case len(cur) == 2 && cur[0] == '.' && cur[1] == '.':
+			copy(path[prev:], path[start+2:])
+			path = path[0 : len(path)+prev-(start+2)]
+			prev = 1
+			start = 1
+
+		default:
+
+			fi, err := os.Lstat(string(c))
+			if err != nil {
+				return "", err
+			}
+			if isSymlink(fi) {
+
+				nlinks++
+				if nlinks > 16 {
+					return "", os.ErrInvalid
+				}
+
+				var link string
+				link, err = os.Readlink(string(c))
+				after := string(path[len(c):])
+
+				// switch symlink component with its real path
+				path = switchSymlinkCom(path, start, link, after)
+
+				prev = 1
+				start = 1
+			} else {
+				// Directories
+				prev = start
+				start = len(c) + 1
+			}
+		}
+	}
+
+	for len(path) > 1 && path[len(path)-1] == os.PathSeparator {
+		path = path[0 : len(path)-1]
+	}
+	return string(path), nil
+
+}
+
+// test if a link is symbolic link
+func isSymlink(fi os.FileInfo) bool {
+	return fi.Mode()&os.ModeSymlink == os.ModeSymlink
+}
+
+// switch a symbolic link component to its real path
+func switchSymlinkCom(path []byte, start int, link, after string) []byte {
+
+	if link[0] == os.PathSeparator {
+		// Absolute links
+		return []byte(filepath.Join(link, after))
+	}
+
+	// Relative links
+	return []byte(filepath.Join(string(path[0:start]), link, after))
+}
+
+// get the next component
+func nextComponent(path []byte, start int) []byte {
+	v := bytes.IndexByte(path[start:], os.PathSeparator)
+	if v < 0 {
+		return path
+	}
+	return path[0 : start+v]
+}