KinoSearch

 view release on metacpan or  search on metacpan

core/KinoSearch/Store/RAMFolder.c  view on Meta::CPAN

        inner_to_folder = (RAMFolder*)to_folder;
    }
    if (!RAMFolder_Is_A(inner_from_folder, RAMFOLDER)) {
        Err_set_error(Err_new(CB_newf("Not a RAMFolder, but a '%o'",
            Obj_Get_Class_Name((Obj*)inner_from_folder))));
        return false;
    }
    if (!RAMFolder_Is_A(inner_to_folder, RAMFOLDER)) {
        Err_set_error(Err_new(CB_newf("Not a RAMFolder, but a '%o'",
            Obj_Get_Class_Name((Obj*)inner_to_folder))));
        return false;
    }

    // Find the original element. 
    elem = Hash_Fetch(inner_from_folder->entries, (Obj*)from_name);
    if (!elem) {
        if (   Folder_Is_A(from_folder, COMPOUNDFILEREADER)
            && Folder_Local_Exists(from_folder, (CharBuf*)from_name)
        ) {
            Err_set_error(Err_new(CB_newf("Source file '%o' is virtual", 
                from)));
        }
        else {
            Err_set_error(Err_new(CB_newf("File not found: '%o'", from)));
        }
        return false;
    }

    // Execute the rename/hard-link. 
    if (op == OP_RENAME) {
        Obj *existing = Hash_Fetch(inner_to_folder->entries, (Obj*)to_name);
        if (existing) {
            bool_t conflict = false;

            // Return success fast if file is copied on top of itself. 
            if (   inner_from_folder == inner_to_folder 
                && ZCB_Equals(from_name, (Obj*)to_name)
            ) {
                return true;
            }

            // Don't allow clobbering of different entry type. 
            if (Obj_Is_A(elem, RAMFILE)) {
                if (!Obj_Is_A(existing, RAMFILE)) {
                    conflict = true;
                }
            }
            else if (Obj_Is_A(elem, FOLDER)) {
                if (!Obj_Is_A(existing, FOLDER)) {
                    conflict = true;
                }
            }
            if (conflict) {
                Err_set_error(Err_new(CB_newf("Can't clobber a %o with a %o",
                    Obj_Get_Class_Name(existing), Obj_Get_Class_Name(elem))));
                return false;
            }
        }

        // Perform the store first, then the delete. Inform Folder objects
        // about the relocation.
        Hash_Store(inner_to_folder->entries, (Obj*)to_name, INCREF(elem));
        DECREF(Hash_Delete(inner_from_folder->entries, (Obj*)from_name));
        if (Obj_Is_A(elem, FOLDER)) {
            CharBuf *newpath = S_fullpath(inner_to_folder, (CharBuf*)to_name);
            Folder_Set_Path((Folder*)elem, newpath);
            DECREF(newpath);
        }
    }
    else if (op == OP_HARD_LINK) {
        if (!Obj_Is_A(elem, RAMFILE)) {
            Err_set_error(Err_new(CB_newf("'%o' isn't a file, it's a %o",
                from, Obj_Get_Class_Name(elem))));
            return false;
        }
        else {
            Obj *existing 
                = Hash_Fetch(inner_to_folder->entries, (Obj*)to_name);
            if (existing) {
                Err_set_error(Err_new(CB_newf("'%o' already exists", to)));
                return false;
            }
            else {
                Hash_Store(inner_to_folder->entries, (Obj*)to_name, 
                    INCREF(elem));
            }
        }
    }
    else {
        THROW(ERR, "Unexpected op: %i32", (int32_t)op);
    }

    return true;
}

bool_t
RAMFolder_rename(RAMFolder *self, const CharBuf* from, const CharBuf *to)
{
    Folder        *from_folder = RAMFolder_Enclosing_Folder(self, from);
    Folder        *to_folder   = RAMFolder_Enclosing_Folder(self, to);
    ZombieCharBuf *from_name   = IxFileNames_local_part(from, ZCB_BLANK());
    ZombieCharBuf *to_name     = IxFileNames_local_part(to, ZCB_BLANK());
    bool_t         result      = S_rename_or_hard_link(self, from, to, 
        from_folder, to_folder, from_name, to_name, OP_RENAME);
    if (!result) { ERR_ADD_FRAME(Err_get_error()); }
    return result;
}

bool_t
RAMFolder_hard_link(RAMFolder *self, const CharBuf *from, const CharBuf *to)
{
    Folder        *from_folder = RAMFolder_Enclosing_Folder(self, from);
    Folder        *to_folder   = RAMFolder_Enclosing_Folder(self, to);
    ZombieCharBuf *from_name   = IxFileNames_local_part(from, ZCB_BLANK());
    ZombieCharBuf *to_name     = IxFileNames_local_part(to, ZCB_BLANK());
    bool_t         result      = S_rename_or_hard_link(self, from, to, 
        from_folder, to_folder, from_name, to_name, OP_HARD_LINK);
    if (!result) { ERR_ADD_FRAME(Err_get_error()); }
    return result;
}



( run in 0.529 second using v1.01-cache-2.11-cpan-5511b514fd6 )