| 1 |
// Copyright 2024 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package hugofs |
| 15 |
|
| 16 |
import ( |
| 17 |
"io/fs" |
| 18 |
|
| 19 |
"github.com/bep/overlayfs" |
| 20 |
) |
| 21 |
|
| 22 |
// AppendDirsMerger merges two directories keeping all regular files |
| 23 |
// with the first slice as the base. |
| 24 |
// Duplicate directories in the second slice will be ignored. |
| 25 |
var AppendDirsMerger overlayfs.DirsMerger = func(lofi, bofi []fs.DirEntry) []fs.DirEntry { |
| 26 |
for _, fi1 := range bofi { |
| 27 |
var found bool |
| 28 |
// Remove duplicate directories. |
| 29 |
if fi1.IsDir() { |
| 30 |
for _, fi2 := range lofi { |
| 31 |
if fi2.IsDir() && fi2.Name() == fi1.Name() { |
| 32 |
found = true |
| 33 |
break |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
if !found { |
| 38 |
lofi = append(lofi, fi1) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
return lofi |
| 43 |
} |