/// <summary> /// Get a single child by name /// </summary> /// @param [in] name -- name of child. If name is "..", it gets parent. If name is ".", return itself </param> private SqlFsNode __getChild(string name) { SqlFsNode fsNode = null; if (name.Equals(SqlFsConst.CURDIR)) { fsNode = this; // current dir } else if (name.Equals(SqlFsConst.PARENTDIR)) { // parent or itself if already root fsNode = (this.ID.Equals(SqlFsConst.ROOTDIRID) ? this : this.Parent); } else { Cursor c = getEntryByName(name, SqlFsConst.FSTYPE.ANY); if (c != null) { c.moveToFirst(); fsNode = SqlFsNode.getFsNode(db, fsLocker, c); c.close(); } else { SqlFsErrCode.CurrentError = SqlFsErrCode.FsErr.NoEntryByName; } } return(fsNode); }
/// <summary> /// Check if passed in 'dir' is one of its ancestor /// </summary> private bool __isAncestor(SqlDir dir) { bool found = false; SqlFsNode curNode = this; do { curNode = curNode.Parent; if (curNode == null) { break; } if (curNode.ID.Equals(dir.ID)) { found = true; break; } if (curNode.ID.Equals(SqlFsConst.ROOTDIRID)) // reach root { break; } } while (true); return(found); }
/// <summary> /// Test if a absolute path (dir/file) exists /// </summary> private bool __exists(string path) { if (SqlFsFunc.isNullOrEmpty(path)) { SqlFsErrCode.CurrentError = FsErr.EmptyString; return(false); } if (!path.StartsWith(SqlFsConst.STRPATHSEP)) // must start with '/' { SqlFsErrCode.CurrentError = FsErr.MustUseAbsolutePath; return(false); } path = SqlFsFunc.Trim(path, new char[] { SqlFsConst.PATHSEP }); if (SqlFsFunc.isNullOrEmpty(path)) // if empty after trim, it refers to root { return(true); } SqlDir rootDir = RootDir; if (rootDir == null) { SqlFsErrCode.CurrentError = FsErr.CannotAccessRoot; return(false); } SqlFsNode fsNode = rootDir.getFsNode(path); return(fsNode != null); }
//////////////////////////FS operations /////////////////////////////////// /// @param [in] id -- get entry using ID directly </param> internal static SqlFsNode getFsNodeByID(SQLiteDatabase db, SqlFsLocker fsLocker, FsID id) { SqlFsNode fsNode = null; string @where = SqlStr.genWhere(new SqlStr.SqlSimpCond(SqlFs.FSBLOCK.fsID.ToString(), "=", id)); Cursor c = null; try { c = db.query(SqlFs.DBNAMES.FsBlock.ToString(), new string[] { SqlFs.FSBLOCK.fsID.ToString(), SqlFs.FSBLOCK.fsType.ToString() }, @where, null, null, null, null); if (c.moveToFirst()) { fsNode = SqlFsNode.getFsNode(db, fsLocker, c); } } catch (Exception e) { SqlFsLog.debug(e); SqlFsErrCode.CurrentError = FsErr.GetFieldError; } finally { SqlFsFunc.close(c); } return(fsNode); }
/// @param [in] relative filePath -- e.g. "path/to/file" </param> private SqlFile __getFile(string filePath) { if (SqlFsFunc.isNullOrEmpty(filePath)) { SqlFsErrCode.CurrentError = FsErr.EmptyString; return(null); } if (filePath.StartsWith(SqlFsConst.STRPATHSEP) || filePath.EndsWith(SqlFsConst.STRPATHSEP)) // must *NOT* start or end with '/' { SqlFsErrCode.CurrentError = FsErr.MustNotStartOrEndWithPathSeparator; return(null); } string[] dirSeg = filePath.Split(SqlFsConst.STRPATHSEP, true); if (dirSeg == null || dirSeg.Length <= 0) { SqlFsErrCode.CurrentError = FsErr.SplitPathErr; return(null); } SqlFsNode curNode = this; // start looping to target file for (int i = 0; i < dirSeg.Length; ++i) { if (SqlFsFunc.isNullOrEmpty(dirSeg[i])) // to prevent empty space between separator { continue; } curNode = ((SqlDir)curNode).__getChild(dirSeg[i]); if (i == dirSeg.Length - 1) { if (curNode == null || curNode.Dir) // last one must be a file { curNode = null; SqlFsErrCode.CurrentError = FsErr.ChildNotFound; break; } } else { if (curNode == null || !curNode.Dir) // others' should be dir { curNode = null; SqlFsErrCode.CurrentError = FsErr.NotDirInPath; break; } } } return((SqlFile)curNode); }
/// @param [in] relative dirPath -- e.g. "path/to/dir" </param> private SqlFsNode __getFsNode(string path) { if (SqlFsFunc.isNullOrEmpty(path)) { SqlFsErrCode.CurrentError = FsErr.EmptyString; return(null); } if (path.StartsWith(SqlFsConst.STRPATHSEP)) // must *NOT* start with '/' { SqlFsErrCode.CurrentError = FsErr.MustUseRelativePath; return(null); } if (path.EndsWith(SqlFsConst.STRPATHSEP)) // trim trailing '/' { path = SqlFsFunc.trimEnd(path, new char[] { SqlFsConst.PATHSEP }); } string[] pathSeg = path.Split(SqlFsConst.STRPATHSEP, true); if (pathSeg == null || pathSeg.Length <= 0) { SqlFsErrCode.CurrentError = FsErr.SplitPathErr; return(null); } SqlFsNode curNode = this; // start looping to target node for (int i = 0; i < pathSeg.Length; ++i) { if (SqlFsFunc.isNullOrEmpty(pathSeg[i])) // to prevent empty space between separator { continue; } curNode = ((SqlDir)curNode).__getChild(pathSeg[i]); if (curNode == null) { SqlFsErrCode.CurrentError = FsErr.ChildNotFound; break; } if (!curNode.Dir && i != pathSeg.Length - 1) // if a file but not reach the end yet { curNode = null; SqlFsErrCode.CurrentError = FsErr.NotDirInPath; break; } } return(curNode); }
/// @param [in] relative dirPath -- e.g. "path/to/dir" </param> private SqlDir __getDir(string dirPath) { if (SqlFsFunc.isNullOrEmpty(dirPath)) { SqlFsErrCode.CurrentError = FsErr.EmptyString; return(null); } if (dirPath.StartsWith(SqlFsConst.STRPATHSEP)) // must *NOT* start with '/' { SqlFsErrCode.CurrentError = FsErr.MustUseRelativePath; return(null); } if (dirPath.EndsWith(SqlFsConst.STRPATHSEP)) // trim trailing '/' { dirPath = SqlFsFunc.trimEnd(dirPath, new char[] { SqlFsConst.PATHSEP }); } string[] dirSeg = dirPath.Split(SqlFsConst.STRPATHSEP, true); if (dirSeg == null || dirSeg.Length <= 0) { SqlFsErrCode.CurrentError = FsErr.SplitPathErr; return(null); } SqlFsNode curNode = this; // start looping to target dir foreach (string dir in dirSeg) { if (SqlFsFunc.isNullOrEmpty(dir)) // to prevent empty space between separator { continue; } curNode = ((SqlDir)curNode).__getChild(dir); if (curNode == null || !curNode.Dir) { curNode = null; SqlFsErrCode.CurrentError = FsErr.ChildNotFound; break; } } return((SqlDir)curNode); }
/// <summary> /// Get a fsNode from the result /// </summary> internal static SqlFsNode getFsNode(SQLiteDatabase db, SqlFsLocker fsLocker, Cursor c) { SqlFsNode fsNode = null; SqlFsConst.FSTYPE type = SqlFsConst.FSTYPE.toFSTYPE(c.getInt(SqlFs.FSBLOCK.fsType.ordinal())); if (type == SqlFsConst.FSTYPE.DIR) { fsNode = SqlDir.getDir(db, fsLocker, c); } else if (type == SqlFsConst.FSTYPE.FILE) { fsNode = SqlFile.getFile(db, fsLocker, c); } return(fsNode); }
/// <summary> /// Get a list of children (dirs and files) /// </summary> private List <SqlFsNode> __getChildList() { List <SqlFsNode> childList = null; Cursor c = getEntryByName(null, SqlFsConst.FSTYPE.ANY); if (c != null) { c.moveToFirst(); childList = new List <SqlFsNode>(c.Count); do { SqlFsNode fsNode = SqlFsNode.getFsNode(db, fsLocker, c); if (fsNode != null) { childList.Add(fsNode); } } while (c.moveToNext()); c.close(); } return(childList); }
/// <summary> /// Create an empty dir /// </summary> /// <returns> new ID for the inserted dir </returns> internal static FsID addDir(SQLiteDatabase db, string dirName, FsID parentID) { return(SqlFsNode.addFsNode(db, SqlFsConst.FSTYPE.DIR, dirName, parentID)); }
/// <summary> /// Create an empty file /// </summary> /// <returns> new ID for the inserted dir </returns> internal static FsID addFile(SQLiteDatabase db, string fileName, FsID parentID) { return(SqlFsNode.addFsNode(db, SqlFsConst.FSTYPE.FILE, fileName, parentID)); }
/// <summary> /// Get root directory /// </summary> private SqlDir __getRootDir() { SqlFsNode fsNode = getFsNodeByID(SqlFsConst.ROOTDIRID); return((SqlDir)fsNode); }