@@ -319,29 +319,61 @@ func stringInSlice(str string, list []string) bool {
319319 return false
320320}
321321
322- func findBaseDir (dirList []string ) string {
323- if len (dirList ) == 1 {
324- return filepath .Dir (dirList [0 ]) + "/"
325- }
326- baseDir := ""
327- // https://github.com/backdrop-ops/contrib/issues/55#issuecomment-73814500
328- dontdiff := []string {"pax_global_header" }
329- for index , _ := range dirList {
330- if stringInSlice (dirList [index ], dontdiff ) {
331- continue
322+ func CommonPrefix (sep byte , paths []string ) string {
323+ // Handle special cases.
324+ switch len (paths ) {
325+ case 0 :
326+ return ""
327+ case 1 :
328+ return path .Clean (paths [0 ])
329+ }
330+
331+ c := []byte (path .Clean (paths [0 ]))
332+
333+ // We add a trailing sep to handle: common prefix directory is included in the path list
334+ // (e.g. /home/user1, /home/user1/foo, /home/user1/bar).
335+ // path.Clean will have cleaned off trailing / separators with
336+ // the exception of the root directory, "/" making it "//"
337+ // but this will get fixed up to "/" below).
338+ c = append (c , sep )
339+
340+ // Ignore the first path since it's already in c
341+ for _ , v := range paths [1 :] {
342+ // Clean up each path before testing it
343+ v = path .Clean (v ) + string (sep )
344+
345+ // Find the first non-common byte and truncate c
346+ if len (v ) < len (c ) {
347+ c = c [:len (v )]
332348 }
333- candidateBaseDir := dirList [ index ]
334- for i := index ; i < len ( dirList ); i ++ {
335- if ! strings . Contains ( dirList [ i ], candidateBaseDir ) {
336- return baseDir
349+ for i := 0 ; i < len ( c ); i ++ {
350+ if v [ i ] != c [ i ] {
351+ c = c [: i ]
352+ break
337353 }
338354 }
339- // avoid setting the candidate if it is the last file
340- if dirList [len (dirList )- 1 ] != candidateBaseDir {
341- baseDir = candidateBaseDir
355+ }
356+
357+ // Remove trailing non-separator characters and the final separator
358+ for i := len (c ) - 1 ; i >= 0 ; i -- {
359+ if c [i ] == sep {
360+ c = c [:i ]
361+ break
342362 }
343363 }
344- return baseDir
364+
365+ return string (c )
366+ }
367+
368+ func findBaseDir (dirList []string ) string {
369+ if len (dirList ) == 1 {
370+ return filepath .Dir (dirList [0 ]) + "/"
371+ }
372+ commonBaseDir := CommonPrefix (os .PathSeparator , dirList )
373+ if commonBaseDir != "" {
374+ commonBaseDir = commonBaseDir + "/"
375+ }
376+ return commonBaseDir
345377}
346378
347379func extractZip (body []byte , location string ) (string , error ) {
0 commit comments