/// <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> /// Save data to data block table /// </summary> private bool __saveFileData(IFileData fileData) { bool isOK = false; do { FsID dataBlockID = this.getDataBlockID(); if (dataBlockID.compare(SqlFsConst.INVALIDID) == 0) // new file without data will be -1 { SqlFsErrCode.CurrentError = FsErr.DataBlockIDNotValid; break; } FsID newDataBlockID = fileData.saveData(db, dataBlockID); if (newDataBlockID.compare(SqlFsConst.INVALIDID) <= 0) { SqlFsErrCode.CurrentError = FsErr.SaveFileDataErr; break; } // update file size this.setField(FSBLOCK.fsFileSize, fileData.DataSizeInByte); // update itself (data block table) so that last mod time can be updated this.setDataBlockID(newDataBlockID); isOK = true; } while (false); return(isOK); }
/// <summary> /// Retrieve data from data block table /// </summary> private bool __getFileData(IFileData fileData) { FsID dataBlockID = this.getDataBlockID(); if (dataBlockID.compare(SqlFsConst.INVALIDID) <= 0) { SqlFsErrCode.CurrentError = FsErr.DataBlockIDNotValid; return(false); } return(fileData.getData(db, dataBlockID)); }
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); }
/// /// <summary> /// Save data to data block table /// </summary> /// @param [in] dataBlockID -- the data block ID if exist. Pass 0 if not exist /// </param> /// <returns> dataBlockID if OK </returns> /// <returns> 0 if failed </returns> private FsID __saveData(SQLiteDatabase db, FsID dataBlockID) { ContentValues contValues = __saveData(); try { if (dataBlockID.compare(SqlFsConst.INVALIDID) <= 0) { // save new data if (db.insert(IFileData.DTABLENAME.ToString(), null, contValues) < 0) { dataBlockID = SqlFsConst.INVALIDID; } else { dataBlockID = getLastInsertID(db); } } else { // update data string @where = SqlStr.genWhere(new SqlStr.SqlSimpCond(IFileData.IDCOL, "=", dataBlockID)); if (db.update(IFileData.DTABLENAME.ToString(), contValues, @where, null) == 0) { dataBlockID = SqlFsConst.INVALIDID; } } } catch (Exception e) { Log.d("IFileData.__saveData", e.Message); dataBlockID = SqlFsConst.INVALIDID; } return(dataBlockID); }