ParseFileMode converts a git-style file mode (e.g. "100644") into a human-readable string like "rw-r--r--".
(modeStr string)
| 9 | // ParseFileMode converts a git-style file mode (e.g. "100644") |
| 10 | // into a human-readable string like "rw-r--r--". |
| 11 | func ParseFileMode(modeStr string) (string, error) { |
| 12 | // Git modes are typically 6 digits. The last 3 represent permissions. |
| 13 | // e.g. 100644 → 644 |
| 14 | if len(modeStr) < 3 { |
| 15 | return "", fmt.Errorf("invalid mode: %s", modeStr) |
| 16 | } |
| 17 | |
| 18 | permStr := modeStr[len(modeStr)-3:] |
| 19 | permVal, err := strconv.Atoi(permStr) |
| 20 | if err != nil { |
| 21 | return "", err |
| 22 | } |
| 23 | |
| 24 | return numericPermToLetters(permVal), nil |
| 25 | } |
| 26 | |
| 27 | func numericPermToLetters(perm int) string { |
| 28 | // Map each octal digit to rwx letters |