[archiver] Refactor filesystem structure walking logic
This commit is contained in:
parent
bb01349a9f
commit
f87a68f72b
1 changed files with 36 additions and 41 deletions
|
@ -363,16 +363,16 @@ func prepareData (_context *context, _pathResolved string, _pathInArchive string
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func walkPath (_context *context, _path string, _prefix string, _name string, _recursed map[string]uint, _recurse bool) (os.FileInfo, error) {
|
func walkPath (_context *context, _pathResolved string, _pathInArchive string, _name string, _recursed map[string]uint, _recurse bool) (os.FileInfo, error) {
|
||||||
|
|
||||||
if _recursed == nil {
|
if _recursed == nil {
|
||||||
_recursed = make (map[string]uint, 128)
|
_recursed = make (map[string]uint, 128)
|
||||||
}
|
}
|
||||||
|
|
||||||
_pathInArchive := filepath.Join (_prefix, _name)
|
_pathOriginal := _pathResolved
|
||||||
|
|
||||||
var _stat os.FileInfo
|
var _stat os.FileInfo
|
||||||
if _stat_0, _error := os.Lstat (_path); _error == nil {
|
if _stat_0, _error := os.Lstat (_pathResolved); _error == nil {
|
||||||
_stat = _stat_0
|
_stat = _stat_0
|
||||||
} else {
|
} else {
|
||||||
return nil, _error
|
return nil, _error
|
||||||
|
@ -382,7 +382,7 @@ func walkPath (_context *context, _path string, _prefix string, _name string, _r
|
||||||
_isSymlink := false
|
_isSymlink := false
|
||||||
if (_stat.Mode () & os.ModeSymlink) != 0 {
|
if (_stat.Mode () & os.ModeSymlink) != 0 {
|
||||||
_isSymlink = true
|
_isSymlink = true
|
||||||
if _stat_0, _error := os.Stat (_path); _error == nil {
|
if _stat_0, _error := os.Stat (_pathResolved); _error == nil {
|
||||||
_stat = _stat_0
|
_stat = _stat_0
|
||||||
} else {
|
} else {
|
||||||
return nil, _error
|
return nil, _error
|
||||||
|
@ -394,15 +394,12 @@ func walkPath (_context *context, _path string, _prefix string, _name string, _r
|
||||||
return _stat, nil
|
return _stat, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var _pathResolved string
|
|
||||||
if _isSymlink {
|
if _isSymlink {
|
||||||
if _resolved, _error := filepath.EvalSymlinks (_path); _error == nil {
|
if _pathResolved_0, _error := filepath.EvalSymlinks (_pathResolved); _error == nil {
|
||||||
_pathResolved = _resolved
|
_pathResolved = _pathResolved_0
|
||||||
} else {
|
} else {
|
||||||
return nil, _error
|
return nil, _error
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
_pathResolved = _path
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if _isSymlink && _context.debug {
|
if _isSymlink && _context.debug {
|
||||||
|
@ -424,34 +421,39 @@ func walkPath (_context *context, _path string, _prefix string, _name string, _r
|
||||||
|
|
||||||
_wasRecursed, _ := _recursed[_pathResolved]
|
_wasRecursed, _ := _recursed[_pathResolved]
|
||||||
if _wasRecursed > 0 {
|
if _wasRecursed > 0 {
|
||||||
log.Printf ("[ww] [2e1744c9] detected directory loop for `%s` resolving to `%s`; ignoring!\n", _path, _pathResolved)
|
log.Printf ("[ww] [2e1744c9] detected directory loop for `%s` resolving to `%s`; ignoring!\n", _pathOriginal, _pathResolved)
|
||||||
return _stat, nil
|
return _stat, nil
|
||||||
}
|
}
|
||||||
_recursed[_pathResolved] = _wasRecursed + 1
|
_recursed[_pathResolved] = _wasRecursed + 1
|
||||||
|
|
||||||
_names := make ([]string, 0, 16)
|
_childsName := make ([]string, 0, 16)
|
||||||
_stats := make (map[string]os.FileInfo, 16)
|
_childsPathResolved := make (map[string]string, 16)
|
||||||
|
_childsPathInArchive := make (map[string]string, 16)
|
||||||
|
_childsStat := make (map[string]os.FileInfo, 16)
|
||||||
|
|
||||||
if _context.debug {
|
if _context.debug {
|
||||||
log.Printf ("[ ] folder >> `%s`\n", _pathInArchive)
|
log.Printf ("[ ] folder >> `%s`\n", _pathInArchive)
|
||||||
}
|
}
|
||||||
if _stream, _error := os.Open (_path); _error == nil {
|
if _stream, _error := os.Open (_pathResolved); _error == nil {
|
||||||
defer _stream.Close ()
|
defer _stream.Close ()
|
||||||
_prefix = filepath.Join (_prefix, _name)
|
|
||||||
_loop : for {
|
_loop : for {
|
||||||
switch _buffer, _error := _stream.Readdir (128); _error {
|
switch _buffer, _error := _stream.Readdir (128); _error {
|
||||||
case nil :
|
case nil :
|
||||||
for _, _stat := range _buffer {
|
for _, _childStat := range _buffer {
|
||||||
_name := _stat.Name ()
|
_childName := _childStat.Name ()
|
||||||
if ShouldSkipName (_name) {
|
_childPathResolved := filepath.Join (_pathResolved, _childName)
|
||||||
|
_childPathInArchive := filepath.Join (_pathInArchive, _childName)
|
||||||
|
if ShouldSkipName (_childName) {
|
||||||
if _context.debug {
|
if _context.debug {
|
||||||
log.Printf ("[ ] skip !! `%s`\n", filepath.Join (_prefix, _name))
|
log.Printf ("[ ] skip !! `%s`\n", _childPathInArchive)
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
_names = append (_names, _name)
|
_childsName = append (_childsName, _childName)
|
||||||
if _stat, _error := walkPath (_context, filepath.Join (_path, _name), _prefix, _name, _recursed, false); _error == nil {
|
_childsPathResolved[_childName] = _childPathResolved
|
||||||
_stats[_name] = _stat
|
_childsPathInArchive[_childName] = _childPathInArchive
|
||||||
|
if _childStat, _error := walkPath (_context, _childPathResolved, _childPathInArchive, _childName, _recursed, false); _error == nil {
|
||||||
|
_childsStat[_childName] = _childStat
|
||||||
} else {
|
} else {
|
||||||
return nil, _error
|
return nil, _error
|
||||||
}
|
}
|
||||||
|
@ -467,42 +469,35 @@ func walkPath (_context *context, _path string, _prefix string, _name string, _r
|
||||||
log.Printf ("[ ] folder << `%s`\n", _pathInArchive)
|
log.Printf ("[ ] folder << `%s`\n", _pathInArchive)
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Strings (_names)
|
sort.Strings (_childsName)
|
||||||
|
|
||||||
if _context.debug {
|
if _context.debug {
|
||||||
log.Printf ("[ ] folder :: `%s`\n", _pathInArchive)
|
log.Printf ("[ ] folder :: `%s`\n", _pathInArchive)
|
||||||
}
|
}
|
||||||
if _error := archiveFolder (_context, _pathResolved, _pathInArchive, _names, _stats); _error != nil {
|
if _error := archiveFolder (_context, _pathResolved, _pathInArchive, _childsName, _childsStat); _error != nil {
|
||||||
return nil, _error
|
return nil, _error
|
||||||
}
|
}
|
||||||
|
|
||||||
if _context.debug {
|
if _context.debug {
|
||||||
log.Printf ("[ ] folder >> `%s`\n", _pathInArchive)
|
log.Printf ("[ ] folder >> `%s`\n", _pathInArchive)
|
||||||
}
|
}
|
||||||
|
for _, _childName := range _childsName {
|
||||||
for _, _name := range _names {
|
if _childsStat[_childName] .Mode () .IsRegular () {
|
||||||
_stat := _stats[_name]
|
if _, _error := walkPath (_context, _childsPathResolved[_childName], _childsPathInArchive[_childName], _childName, _recursed, true); _error != nil {
|
||||||
_statMode := _stat.Mode ()
|
|
||||||
if _statMode.IsRegular () {
|
|
||||||
if _, _error := walkPath (_context, filepath.Join (_path, _name), _prefix, _name, _recursed, true); _error != nil {
|
|
||||||
return _stat, _error
|
return _stat, _error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, _name := range _names {
|
for _, _childName := range _childsName {
|
||||||
_stat := _stats[_name]
|
if _childsStat[_childName] .Mode () .IsDir () {
|
||||||
_statMode := _stat.Mode ()
|
if _, _error := walkPath (_context, _childsPathResolved[_childName], _childsPathInArchive[_childName], _childName, _recursed, true); _error != nil {
|
||||||
if _statMode.IsDir () {
|
|
||||||
if _, _error := walkPath (_context, filepath.Join (_path, _name), _prefix, _name, _recursed, true); _error != nil {
|
|
||||||
return _stat, _error
|
return _stat, _error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, _name := range _names {
|
for _, _childName := range _childsName {
|
||||||
_stat := _stats[_name]
|
if (! _childsStat[_childName] .Mode () .IsRegular ()) && (! _childsStat[_childName] .Mode () .IsDir ()) {
|
||||||
_statMode := _stat.Mode ()
|
if _, _error := walkPath (_context, _childsPathResolved[_childName], _childsPathInArchive[_childName], _childName, _recursed, true); _error != nil {
|
||||||
if ! _statMode.IsRegular () && ! _statMode.IsDir () {
|
|
||||||
if _, _error := walkPath (_context, filepath.Join (_path, _name), _prefix, _name, _recursed, true); _error != nil {
|
|
||||||
return _stat, _error
|
return _stat, _error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -515,7 +510,7 @@ func walkPath (_context *context, _path string, _prefix string, _name string, _r
|
||||||
return _stat, nil
|
return _stat, nil
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return nil, fmt.Errorf ("[d9b836d7] unexpected file type for `%s`: `%s`!", _path, _statMode)
|
return nil, fmt.Errorf ("[d9b836d7] unexpected file type for `%s`: `%s`!", _pathResolved, _statMode)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -595,7 +590,7 @@ cdb-http-archiver
|
||||||
debug : _debug,
|
debug : _debug,
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, _error := walkPath (_context, _sourcesFolder, "/", "", nil, true); _error != nil {
|
if _, _error := walkPath (_context, _sourcesFolder, "/", filepath.Base (_sourcesFolder), nil, true); _error != nil {
|
||||||
AbortError (_error, "[b6a19ef4] failed walking folder!")
|
AbortError (_error, "[b6a19ef4] failed walking folder!")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue