mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-09-12 05:47:23 +00:00
fix: do better parsing of file modes (#9161)
- No longer hardcode the file modes we expect, parse them as numbers and do bitmask tricks that Git does so we allow a more variety of _weird_ file modes that can happen in the wild. - Ref: https://codeberg.org/forgejo/forgejo/pulls/8900#issuecomment-6429175 - Resolves Codeberg/Community#2111 Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9161 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
This commit is contained in:
parent
cee204b5a5
commit
b8906423df
2 changed files with 69 additions and 31 deletions
|
@ -10,8 +10,6 @@ import (
|
|||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"forgejo.org/modules/log"
|
||||
)
|
||||
|
||||
// ParseTreeEntries parses the output of a `git ls-tree -l` command.
|
||||
|
@ -55,19 +53,9 @@ func parseTreeEntries(data []byte, ptree *Tree) ([]*TreeEntry, error) {
|
|||
entry.sized = true
|
||||
}
|
||||
|
||||
switch string(entryMode) {
|
||||
case "100644":
|
||||
entry.entryMode = EntryModeBlob
|
||||
case "100755":
|
||||
entry.entryMode = EntryModeExec
|
||||
case "120000":
|
||||
entry.entryMode = EntryModeSymlink
|
||||
case "160000":
|
||||
entry.entryMode = EntryModeCommit
|
||||
case "040000", "040755", "040775": // git uses 040000 for tree object, but some users may get 040755 or 040775 for unknown reasons
|
||||
entry.entryMode = EntryModeTree
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown type: %v", string(entryMode))
|
||||
entry.entryMode, err = parseMode(string(entryMode))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
entry.ID, err = NewIDFromString(string(entryObjectID))
|
||||
|
@ -108,23 +96,10 @@ loop:
|
|||
sz -= int64(count)
|
||||
entry := new(TreeEntry)
|
||||
entry.ptree = ptree
|
||||
|
||||
switch string(mode) {
|
||||
case "100644":
|
||||
entry.entryMode = EntryModeBlob
|
||||
case "100755":
|
||||
entry.entryMode = EntryModeExec
|
||||
case "120000":
|
||||
entry.entryMode = EntryModeSymlink
|
||||
case "160000":
|
||||
entry.entryMode = EntryModeCommit
|
||||
case "40000", "40755", "40775": // git uses 40000 for tree object, but some users may get 40755 or 40775 for unknown reasons
|
||||
entry.entryMode = EntryModeTree
|
||||
default:
|
||||
log.Debug("Unknown mode: %v", string(mode))
|
||||
return nil, fmt.Errorf("unknown mode: %v", string(mode))
|
||||
entry.entryMode, err = parseMode(string(mode))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
entry.ID = objectFormat.MustID(sha)
|
||||
entry.name = string(fname)
|
||||
entries = append(entries, entry)
|
||||
|
@ -135,3 +110,31 @@ loop:
|
|||
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
// Parse the file mode, we cannot hardcode the modes that we expect for
|
||||
// a variety of reasons (that is not known to us) the permissions bits
|
||||
// of files can vary, usually the result because of tooling that uses Git in
|
||||
// a funny way. So we have to parse the mode as a integer and do bit tricks.
|
||||
func parseMode(modeStr string) (EntryMode, error) {
|
||||
mode, err := strconv.ParseUint(modeStr, 8, 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("cannot parse mode: %v", err)
|
||||
}
|
||||
|
||||
switch mode & 0o170000 {
|
||||
case 0o040000:
|
||||
return EntryModeTree, nil
|
||||
case 0o120000:
|
||||
return EntryModeSymlink, nil
|
||||
case 0o160000:
|
||||
return EntryModeCommit, nil
|
||||
case 0o100000:
|
||||
// Check for the permission bit on the owner.
|
||||
if mode&0o100 == 0o100 {
|
||||
return EntryModeExec, nil
|
||||
}
|
||||
return EntryModeBlob, nil
|
||||
}
|
||||
|
||||
return 0, fmt.Errorf("unknown mode: %o", mode)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue