AddPath adds a new node to the tree with the given payload
(filepath string, data FileInfo)
| 242 | |
| 243 | // AddPath adds a new node to the tree with the given payload |
| 244 | func (tree *FileTree) AddPath(filepath string, data FileInfo) (*FileNode, []*FileNode, error) { |
| 245 | filepath = path.Clean(filepath) |
| 246 | if filepath == "." { |
| 247 | return nil, nil, fmt.Errorf("cannot add relative path '%s'", filepath) |
| 248 | } |
| 249 | nodeNames := strings.Split(strings.Trim(filepath, "/"), "/") |
| 250 | node := tree.Root |
| 251 | addedNodes := make([]*FileNode, 0) |
| 252 | for idx, name := range nodeNames { |
| 253 | if name == "" { |
| 254 | continue |
| 255 | } |
| 256 | // find or create node |
| 257 | if node.Children[name] != nil { |
| 258 | node = node.Children[name] |
| 259 | } else { |
| 260 | // don't add paths that should be deleted |
| 261 | if strings.HasPrefix(name, doubleWhiteoutPrefix) { |
| 262 | return nil, addedNodes, nil |
| 263 | } |
| 264 | |
| 265 | // don't attach the payload. The payload is destined for the |
| 266 | // Path's end node, not any intermediary node. |
| 267 | node = node.AddChild(name, FileInfo{}) |
| 268 | addedNodes = append(addedNodes, node) |
| 269 | |
| 270 | if node == nil { |
| 271 | // the child could not be added |
| 272 | return node, addedNodes, fmt.Errorf("could not add child node: '%s' (path:'%s')", name, filepath) |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // attach payload to the last specified node |
| 277 | if idx == len(nodeNames)-1 { |
| 278 | node.Data.FileInfo = data |
| 279 | } |
| 280 | } |
| 281 | return node, addedNodes, nil |
| 282 | } |
| 283 | |
| 284 | // RemovePath removes a node from the tree given its path. |
| 285 | func (tree *FileTree) RemovePath(path string) error { |