/// <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> /// Create a new entry in FsBlock /// </summary> /// <returns> new ID for the inserted node </returns> internal static FsID addFsNode(SQLiteDatabase db, SqlFsConst.FSTYPE type, string dirName, FsID parentID) { long curTime = SqlFsFunc.calToFileTime(new DateTime()); List <object> colsAndValues = new List <object>(10); colsAndValues.Add(SqlFs.FSBLOCK.fsCreateTime.ToString()); colsAndValues.Add(curTime); colsAndValues.Add(SqlFs.FSBLOCK.fsLastModTime.ToString()); colsAndValues.Add(curTime); colsAndValues.Add(SqlFs.FSBLOCK.fsFileSize.ToString()); colsAndValues.Add(0); colsAndValues.Add(SqlFs.FSBLOCK.fsType.ToString()); colsAndValues.Add(type.v()); colsAndValues.Add(SqlFs.FSBLOCK.fsName.ToString()); colsAndValues.Add(dirName); colsAndValues.Add(SqlFs.FSBLOCK.fsParent.ToString()); colsAndValues.Add(parentID); ContentValues contValues = SqlStr.genContentValues(colsAndValues); try { db.insert(SqlFs.DBNAMES.FsBlock.ToString(), null, contValues); } catch (Exception e) { SqlFsLog.debug(e); SqlFsErrCode.CurrentError = FsErr.AddFsNodeError; return(SqlFsConst.INVALIDID); } // retrieve the ID of the new entry return(SqlFs.getLastInsertID(db)); }
/// <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)); }
public static SqlFs create(string dbPath, IFileData dummyInst, Context ctxt, AtomicBoolean isNewTableCreated) { SqlFsErrCode.CurrentError = FsErr.OK; SqlFs fs = null; try { if (dbPath != null) { fs = new SqlFs(ctxt, dbPath); if (fs != null) { fs.fsLocker.FsLock; try { fs.open(); fs.prepare(dummyInst, isNewTableCreated); } finally { fs.fsLocker.dispose(); } } } } catch (Exception e) { SqlFsLog.debug("ERROR: Cannot open database, " + e.Message); SqlFsErrCode.CurrentError = FsErr.CannotOpenDB; } return(fs); }
/// <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); }
/// <summary> /// Move itself to a destination path (absolute or relative) /// </summary> private bool __move(string destPath) { if (SqlFsFunc.isNullOrEmpty(destPath)) { SqlFsErrCode.CurrentError = FsErr.EmptyString; return(false); } // determine destination dir SqlDir destDir = null; if (destPath.StartsWith(SqlFsConst.STRPATHSEP)) { // absolute path SqlDir rootDir = (SqlDir)SqlFs.getFsNodeByID(db, fsLocker, SqlFsConst.ROOTDIRID); // get root destPath = SqlFsFunc.Trim(destPath, new char[] { SqlFsConst.PATHSEP }); // if empty after trim, it refers to root destDir = SqlFsFunc.isNullOrEmpty(destPath) ? rootDir : rootDir.getDir(destPath); } else { // relative path SqlDir parent = this.Parent; if (parent != null) { destDir = parent.getDir(destPath); } } if (destDir != null) { return(__move(destDir)); } SqlFsErrCode.CurrentError = FsErr.DestDirNotFound; return(false); }
internal virtual SqlFsNode getFsNodeByID(FsID id) { return(SqlFs.getFsNodeByID(db, fsLocker, id)); }
/// /// @param [in] dbPath -- absolute or relative path (inside phone memory) to the db file </param> /// @param [in] dummyInst -- a dummy instance of a class inherited from IFileData. </param> public static SqlFs create(string dbPath, IFileData dummyInst, Context ctxt) { AtomicBoolean isNewTableCreated = new AtomicBoolean(false); return(SqlFs.create(dbPath, dummyInst, ctxt, isNewTableCreated)); }
/// <summary> /// Create db using the default "SimpleFileData" /// </summary> /// @param [in] dbPath -- absolute or relative path (inside phone memory) to the db file </param> public static SqlFs create(string dbPath, Context ctxt) { AtomicBoolean isNewTableCreated = new AtomicBoolean(false); return(SqlFs.create(dbPath, new SimpleFileData(), ctxt, isNewTableCreated)); }
protected internal virtual FsID getLastInsertID(SQLiteDatabase db) { return(SqlFs.getLastInsertID(db)); }