/// Apply new node (upper layer) to node tree (lower layer),
/// support overlay defined in OCI image layer spec (https://github.com/opencontainers/image-spec/blob/master/layer.md),
/// include change types Additions, Modifications, Removals and Opaques, return true if applied
Apply new node (upper layer) to node tree (lower layer),
support overlay defined in OCI image layer spec ,
include change types Additions, Modifications, Removals and Opaques, return true if applied
Apply new node ( upper layer ) to node tree ( lower layer ) support overlay defined in OCI image layer spec include change types Additions Modifications Removals and Opaques return true if applied
pub fn apply(&mut self, target: &Node, handle_whiteout: bool) -> Result<bool> {
        // Handle whiteout file
        if handle_whiteout && target.whiteout_type().is_some() {
            return self.remove(target);
        }
        let target_paths = target.path_vec();
        let target_paths_len = target_paths.len();
        let depth = self.node.path_vec().len();
        // Handle root node modification
        if target.path == PathBuf::from("/") {
            let mut node = target.clone();
            node.overlay = Overlay::UpperModification;
            self.node = node;
            return Ok(true);
        }
        // Don't search if path recursive depth out of target path
        if depth < target_paths_len {
            // TODO: Search child by binary search
            for child in self.children.iter_mut() {
                // Skip if path component name not match
                if target_paths[depth] != child.node.name() {
                    continue;
                }
                // Modifications: Replace the node
                if depth == target_paths_len - 1 {
                    let mut node = target.clone();
                    node.overlay = Overlay::UpperModification;
                    *child = Tree {
                        node,
                        children: child.children.clone(),
                    };
                    return Ok(true);
                }
                if child.node.is_dir() {
                    // Search the node recursively
                    let found = child.apply(target, handle_whiteout)?;
                    if found {
                        return Ok(true);
                    }
                }
            }
        }
        // Additions: Add new node to children
        if depth == target_paths_len - 1 && target_paths[depth - 1] == self.node.name() {
            let mut node = target.clone();
            node.overlay = Overlay::UpperAddition;
            self.children.push(Tree {
                node,
                children: Vec::new(),
            });
            return Ok(true);
        }
        Ok(false)
    }
pub fn apply ( & mut self , target : & Node , handle_whiteout : bool ) -> Result < bool > { // Handle whiteout file if handle_whiteout && target . whiteout_type ( ) . is_some ( ) { return self . remove ( target ) ; } let target_paths = target . path_vec ( ) ; let target_paths_len = target_paths . len ( ) ; let depth = self . node . path_vec ( ) . len ( ) ; // Handle root node modification if target . path == PathBuf :: from ( " " ) { let mut node = target . clone ( ) ; node . overlay = Overlay :: UpperModification ; self . node = node ; return Ok ( true ) ; } // Don't search if path recursive depth out of target path if depth < target_paths_len { // TODO: Search child by binary search for child in self . children . iter_mut ( ) { // Skip if path component name not match if target_paths [ depth ] != child . node . name ( ) { continue ; } // Modifications: Replace the node if depth == target_paths_len - 1 { let mut node = target . clone ( ) ; node . overlay = Overlay :: UpperModification ; * child = Tree { node , children : child . children . clone ( ) , } ; return Ok ( true ) ; } if child . node . is_dir ( ) { // Search the node recursively let found = child . apply ( target , handle_whiteout ) ? ; if found { return Ok ( true ) ; } } } } // Additions: Add new node to children if depth == target_paths_len - 1 && target_paths [ depth - 1 ] == self . node . name ( ) { let mut node = target . clone ( ) ; node . overlay = Overlay :: UpperAddition ; self . children . push ( Tree { node , children : Vec :: new ( ) , } ) ; return Ok ( true ) ; } Ok ( false ) }
Apply new node (upper layer) to node tree (lower layer),
support overlay defined in OCI image layer spec (https://github.com/opencontainers/image-spec/blob/master/layer.md),
include change types Additions, Modifications, Removals and Opaques, return true if applied
Apply new node ( upper layer ) to node tree ( lower layer ) support overlay defined in OCI image layer spec ( https : // github . com / opencontainers / image - spec / blob / master / layer . md ) include change types Additions Modifications Removals and Opaques return true if applied
&mut self
target
&Node
handle_whiteout
bool