/// <summary> /// Add a new entry for file. File must be saved so as to be /// appeared under its parent directory. /// </summary> private SqlFile __addFile(string fileName) { if ((fileName = checkInvalidChars(fileName)) == null) { SqlFsErrCode.CurrentError = FsErr.InvalidChars; return(null); } if (__isAlreadyExist(fileName)) { SqlFsErrCode.CurrentError = FsErr.NameAlreadyExists; return(null); } FsID newID = SqlFile.addFile(db, fileName, this.ID); if (newID.compare(SqlFsConst.INVALIDID) <= 0) { SqlFsErrCode.CurrentError = FsErr.NoNewIDForNewFsNode; return(null); } if (!updateChildList(SqlFsConst.FSOP.ADD, newID, FsID.toFsID(0))) { // delete entry just created SqlFs.deleteEntryByID(db, SqlFs.DBNAMES.FsBlock.ToString(), SqlFs.FSBLOCK.fsID.ToString(), newID); return(null); } SqlFile f = SqlFile.getFile(db, fsLocker, newID); f.setDataBlockID(SqlFsConst.NOFILEDATAID); return(f); }
/// <summary> /// Add an empty dir /// </summary> /// <returns> the newly created dir </returns> private SqlDir __addDir(string dirName) { if ((dirName = checkInvalidChars(dirName)) == null) { SqlFsErrCode.CurrentError = FsErr.InvalidChars; return(null); } if (__isAlreadyExist(dirName)) { SqlFsErrCode.CurrentError = FsErr.NameAlreadyExists; return(null); } FsID newID = SqlDir.addDir(db, dirName, this.ID); if (newID.compare(SqlFsConst.INVALIDID) <= 0) { SqlFsErrCode.CurrentError = FsErr.NoNewIDForNewFsNode; return(null); } if (!updateChildList(SqlFsConst.FSOP.ADD, newID, FsID.toFsID(0))) { // delete entry just created SqlFs.deleteEntryByID(db, SqlFs.DBNAMES.FsBlock.ToString(), SqlFs.FSBLOCK.fsID.ToString(), newID); return(null); } return(SqlDir.getDir(db, fsLocker, newID)); }
/// <summary> /// Use a function to get ID since length of ID may be changed in future /// </summary> internal static FsID getID(Cursor cur, int index) { if (FsID.LongID) { return(FsID.toFsID(cur.getLong(index))); } return(FsID.toFsID(cur.getInt(index))); }
/// <summary> /// Move itself to a destination dir /// </summary> private bool __move(SqlDir destDir) { do { if (this.ID.Equals(SqlFsConst.ROOTDIRID)) // can't move root { SqlFsErrCode.CurrentError = FsErr.CannotMoveRoot; break; } if (this.ID.Equals(destDir.ID)) // can't move to itself { SqlFsErrCode.CurrentError = FsErr.CannotMoveToSelf; break; } if (this.Dir && destDir.isAncestor((SqlDir)this)) // if it is a DIR, can't move to its subdir { SqlFsErrCode.CurrentError = FsErr.CannotMoveToSubdir; break; } if (destDir.isAlreadyExist(this.Name)) // can't move if there is one with the same name { SqlFsErrCode.CurrentError = FsErr.NameAlreadyExists; break; } // unlink itself from parent SqlDir parent = this.Parent; if (parent == null) { SqlFsErrCode.CurrentError = FsErr.NoParent; break; } if (!parent.updateChildList(SqlFsConst.FSOP.DEL, this.ID, FsID.toFsID(0))) { break; } // update parent ID if (!setField(SqlFs.FSBLOCK.fsParent, destDir.ID)) { break; } // add itself to dest dir if (!destDir.updateChildList(SqlFsConst.FSOP.ADD, this.ID, FsID.toFsID(0))) { break; } return(true); } while (false); return(false); }
/// <summary> /// delete all dirs and files recursively /// </summary> private bool __delete() { bool isOK = false; do { // delete itself from parent if (!this.ID.Equals(SqlFsConst.ROOTDIRID)) // root has no parent { SqlDir parent = this.Parent; if (parent == null) { SqlFsErrCode.CurrentError = FsErr.NoParent; break; } if (!parent.updateChildList(SqlFsConst.FSOP.DEL, this.ID, FsID.toFsID(0))) { break; } } // delete underlying subdirs and files List <SqlFsNode> childList = this.ChildList; if (childList != null) { // delete children one by one foreach (SqlFsNode fsNode in childList) { fsNode.delete(); } } // delete itself if (this.ID.Equals(SqlFsConst.ROOTDIRID)) { // for root, just clear all children this.setField(SqlFs.FSBLOCK.fsChild, null); } else { if (!SqlFs.deleteEntryByID(db, SqlFs.DBNAMES.FsBlock.ToString(), SqlFs.FSBLOCK.fsID.ToString(), this.ID)) { SqlFsErrCode.CurrentError = FsErr.CannotDeleteFsEntry; break; } } isOK = true; } while (false); return(isOK); }
private bool __delete() { bool isOK = false; do { // delete itself from parent SqlDir parent = this.Parent; if (parent == null) { SqlFsErrCode.CurrentError = FsErr.NoParent; break; } if (!parent.updateChildList(SqlFsConst.FSOP.DEL, this.ID, FsID.toFsID(0))) { break; } // delete entry in data block table FsID dataBlockID = this.getDataBlockID(); if (dataBlockID.compare(SqlFsConst.INVALIDID) <= 0) { //SqlFsLog.debug("+++ dataBlockID = " + dataBlockID.getVal()); SqlFsErrCode.CurrentError = FsErr.DataBlockIDNotValid; break; } if (!SqlFs.deleteEntryByID(db, IFileData.DTABLENAME, IFileData.IDCOL, dataBlockID)) { SqlFsErrCode.CurrentError = FsErr.CannotDeleteDataBlockEntry; break; } // delete its own entry if (!SqlFs.deleteEntryByID(db, SqlFs.DBNAMES.FsBlock.ToString(), SqlFs.FSBLOCK.fsID.ToString(), this.ID)) { SqlFsErrCode.CurrentError = FsErr.CannotDeleteFsEntry; break; } isOK = true; } while (false); return(isOK); }