public override Stream OpenInputFileStream(string path) { Stream st = null; StreamResourceInfo sr = Application.GetResourceStream(new Uri(CFilePath.removeFirstSlash(path), UriKind.Relative)); if (sr != null) { st = sr.Stream; } if (st == null) { try { CRhoFile file = new CRhoFile(); file.open(path, CRhoFile.EOpenModes.OpenReadOnly); st = file.getStream(); } catch (Exception exc) { throw new System.IO.FileNotFoundException(); } } if (st == null) { throw new System.IO.FileNotFoundException(); } return(st); }
public void fromFile(String strFilePath) { String strData = CRhoFile.readStringFromFile(strFilePath); Tokenizer oTokenizer = new Tokenizer(strData, ";"); int nPos = 0; while (oTokenizer.hasMoreTokens()) { String tok = oTokenizer.nextToken().trim(); switch (nPos) { case 0: m_strRhoVer = tok; break; case 1: m_strAppVer = tok; break; case 2: m_bEncrypted = tok.compareTo("encrypted") == 0; break; case 3: m_bSqlite = tok.compareTo("sqlite") == 0; break; } nPos++; } }
bool dispatch(String uri, CRoute route) { if (isknowntype(uri)) { return(false); } // Trying to parse route if (!parse_route(uri, route)) { return(false); } // Convert CamelCase to underscore_case String controllerName = ""; for (int i = 0; i < route.model.Length; i++) { if (Char.IsUpper(route.model[i]) && i > 0) { controllerName += '_'; } controllerName += route.model[i]; } controllerName = controllerName.ToLower(); String model_name_controller = m_root + "/" + route.application + "/" + route.model + "/" + controllerName + "_controller.rb"; String controller = m_root + "/" + route.application + "/" + route.model + "/controller.rb"; return(CRhoFile.isResourceFileExist(model_name_controller) || CRhoFile.isResourceFileExist(controller)); }
String processDispatch(CRoute route, String method, String uri, String query, String body) { Object rhoReq = create_request_hash(route, method, uri, query, null, body); Object rhoResp = RhoRuby.callServe(rhoReq); String strRedirectUrl = getRedirectUrl(rhoResp); if (strRedirectUrl.Length > 0) { return(strRedirectUrl); } String strFilePath = RHODESAPP().canonicalizeRhoPath(uri) + ".gen.html"; if (route.id.Length > 0) { strFilePath = CFilePath.join(m_root, route.application + "/" + route.model + "/" + route.action) + ".gen.html"; } CRhoFile.recursiveCreateDir(strFilePath); CRhoFile.writeDataToFile(strFilePath, getResponseBody(rhoResp)); if (method == "GET") { RHODESAPP().keepLastVisitedUrl(uri); } return(strFilePath); }
public void open(String strPath, String strSqlScript, String strEncryptionInfo) { try { String dbURI = RHODESAPP().resolveDBFilesPath(strPath); dbURI = CFilePath.removeFirstSlash(dbURI); boolean bEncrypted = strEncryptionInfo != null && strEncryptionInfo.length() > 0; //DatabaseSecurityOptions dbso = new DatabaseSecurityOptions(bEncrypted); boolean bExist = CRhoFile.isFileExist(strPath); int res = Sqlite3.sqlite3_open(dbURI, ref m_db); if (res != Sqlite3.SQLITE_OK) { throw new DBException(res, "Could not open database file: " + strPath); } res = Sqlite3.sqlite3_exec(m_db, "PRAGMA journal_mode=PERSIST", 0, 0, 0); if (res != Sqlite3.SQLITE_OK) { Sqlite3.sqlite3_close(m_db); m_db = null; throw new DBException(res, "Cannot set journal mode to PERSIST: " + strPath); } string[] ar1 = CRhoFile.enumDirectory("db"); if (!bExist) { createSchema(strSqlScript); } Sqlite3.sqlite3_create_function(m_db, "rhoOnDeleteObjectRecord", 3, Sqlite3.SQLITE_ANY, 0, DBAdapter.SyncBlob_DeleteCallback, null, null); Sqlite3.sqlite3_create_function(m_db, "rhoOnUpdateObjectRecord", 3, Sqlite3.SQLITE_ANY, 0, DBAdapter.SyncBlob_UpdateCallback, null, null); Sqlite3.sqlite3_create_function(m_db, "rhoOnDeleteSchemaRecord", 1, Sqlite3.SQLITE_ANY, 0, DBAdapter.SyncBlob_DeleteSchemaCallback, null, null); Sqlite3.sqlite3_create_function(m_db, "rhoOnUpdateSchemaRecord", 2, Sqlite3.SQLITE_ANY, 0, DBAdapter.SyncBlob_UpdateSchemaCallback, null, null); string[] ar2 = CRhoFile.enumDirectory("db"); if (m_bPendingTransaction) { startTransaction(); } m_bPendingTransaction = false; } catch (Exception exc) { throw new DBException(exc); } finally { m_nInsideTransaction = 0; } }
public override bool DirectoryExists(string path) { if (path == "") { return(false); } return(CRhoFile.isDirectoryExist(path)); }
public override bool FileExists(string path) { if (path == "") { return(false); } return(CRhoFile.isResourceFileExist(path) || CRhoFile.isFileExist(path)); }
//public static IDBResult createResult(){ // return getInstance().m_dbStorage.createResult(); //} public String makeBlobFolderName() { String fName = CFilePath.join(CRhodesApp.getRhoRootPath(), "db/db-files"); CRhoFile.recursiveCreateDir(fName); return(fName); }
public void clear() { try{ close(); CRhoFile.deleteFile(m_oLogConf.getLogFilePath()); CRhoFile.deleteFile(m_oLogConf.getLogFilePath() + "_pos"); }catch (Exception) { } }
public void toFile(String strFilePath) { String strFullVer = m_strRhoVer + ";" + m_strAppVer + ";" + (m_bEncrypted ? "encrypted":"") + ";" + (m_bSqlite ? "sqlite" : ""); try{ CRhoFile.writeStringToFile(strFilePath, strFullVer); }catch (Exception e) { LOG.ERROR("writeDBVersion failed.", e); throw e; } }
IDictionary processIndex(CRoute route, String method, String uri, Dictionary <String, String> headers, String query, String body) { Object rhoResp = new Dictionary <String, String>(); String fullPath = uri.StartsWith(m_root) ? uri : CFilePath.join(m_root, uri); String strIndexFile = getIndex(fullPath); if (!CRhoFile.isResourceFileExist(strIndexFile)) { String error = "<html><font size=\"+4\"><h2>404 Not Found.</h2> The file " + uri + " was not found.</font></html>"; String strFilePath = CFilePath.join(m_root, "rhodes_error") + ".gen.html"; CRhoFile.recursiveCreateDir(strFilePath); CRhoFile.writeStringToFile(strFilePath, error); ((IDictionary)rhoResp)[RESPONSE_BODY_FILEPATH] = strFilePath; ((IDictionary)rhoResp)[RESPONSE_STATUS] = "404"; ((IDictionary)rhoResp)[RESPONSE_STATUS_MESSAGE] = "Not Found"; return((IDictionary)rhoResp); } if (CFilePath.getExtension(fullPath).Length > 0) { ((IDictionary)rhoResp)[RESPONSE_BODY_FILEPATH] = strIndexFile; ((IDictionary)rhoResp)[RESPONSE_STATUS] = "200"; ((IDictionary)rhoResp)[RESPONSE_STATUS_MESSAGE] = "Ok"; return((IDictionary)rhoResp); } Object rhoReq = create_request_hash(route, method, uri, query, headers, body); rhoResp = RhoRuby.callServeIndex(strIndexFile, rhoReq); String strRedirectUrl = getRedirectUrl(rhoResp); if (strRedirectUrl.Length > 0) { ((IDictionary)rhoResp)[RESPONSE_BODY_FILEPATH] = strRedirectUrl; return((IDictionary)rhoResp); } strIndexFile += ".gen.html"; CRhoFile.recursiveCreateDir(strIndexFile); CRhoFile.writeDataToFile(strIndexFile, getResponseBody(rhoResp)); if (method == "GET") { RHODESAPP().keepLastVisitedUrl(uri); } ((IDictionary)rhoResp)[RESPONSE_BODY_FILEPATH] = strIndexFile; return((IDictionary)rhoResp); }
private void processBlobDelete(int nSrcID, String attrib, String value) { if (value == null || value.length() == 0) { return; } try{ String strFilePath = RHODESAPP().resolveDBFilesPath(value); CRhoFile.deleteFile(strFilePath); }catch (Exception exc) { LOG.ERROR("DBCallback.OnDeleteFromTable: Error delete file: " + value, exc); } }
public static Object __rho_exist_in_resources(Object /*!*/ self, String path) { Object res = null; try { res = CRhoFile.isResourceFileExist(path); } catch (Exception ex) { LOG.HandleRubyException(ex, RhoRuby.rubyContext.CurrentException, "__rho_exist_in_resources"); } return(res); }
public static void SyncBlob_DeleteSchemaCallback(Sqlite3.sqlite3_context dbContext, int nArgs, Sqlite3.Mem[] Args) { String value = Sqlite3.sqlite3_value_text(Args[0]); if (value == null) { return; } String strFilePath = RHODESAPP().resolveDBFilesPath(value); if (strFilePath != "") { CRhoFile.deleteFile(strFilePath); } }
public String getLogText() { String res = ""; boolean bOldSaveToFile = isLogToFile(); setLogToFile(false); try{ res = CRhoFile.readStringFromFile(getLogFilePath()); }finally { setLogToFile(bOldSaveToFile); } return(res); }
public void writeLogMessage(String strMsg) { try{ int len = strMsg.length(); if (m_pFile == null) { m_pFile = new CRhoFile(); } if (!m_pFile.isOpened()) { m_pFile.open(getLogConf().getLogFilePath(), CRhoFile.EOpenModes.OpenForAppend); m_nFileLogSize = (int)m_pFile.size(); loadLogPosition(); } if (getLogConf().getMaxLogFileSize() > 0) { if ((m_nCirclePos >= 0 && m_nCirclePos + len > getLogConf().getMaxLogFileSize()) || (m_nCirclePos < 0 && m_nFileLogSize + len > getLogConf().getMaxLogFileSize())) { m_pFile.movePosToStart(); m_nFileLogSize = 0; m_nCirclePos = 0; } } //int nWritten = m_pFile.writeString(strMsg); m_pFile.writeString(strMsg); m_pFile.flush(); if (m_nCirclePos >= 0) { m_nCirclePos += len; } else { m_nFileLogSize += len; } saveLogPosition(); }catch (Exception exc) { log(exc.Message); } }
public void close() { if (m_pFile != null) { try{ m_pFile.close(); }catch (Exception) {} m_pFile = null; } if (m_pPosFile != null) { try{ m_pPosFile.close(); }catch (Exception) {} m_pPosFile = null; } m_nCirclePos = -1; m_nFileLogSize = 0; }
public static void SyncBlob_UpdateCallback(Sqlite3.sqlite3_context dbContext, int nArgs, Sqlite3.Mem[] Args) { if (nArgs < 3) { return; } DBAttrManager attrMgr = getDBByHandle(Sqlite3.sqlite3_context_db_handle(dbContext)).getAttrMgr(); String szAttrName = Sqlite3.sqlite3_value_text(Args[2]); int nSrcID = Sqlite3.sqlite3_value_int(Args[1]); if (attrMgr.isBlobAttr(nSrcID, szAttrName)) { String strFilePath = RHODESAPP().resolveDBFilesPath(Sqlite3.sqlite3_value_text(Args[0])); CRhoFile.deleteFile(strFilePath); } }
String makeBulkDataFileName(String strDataUrl, String strDbPath, String strExt) { String strNewName = CFilePath.getBaseName(strDataUrl); String strOldName = RhoConf.getInstance().getString("bulksync_filename"); if (strOldName.length() > 0 && strNewName.compareTo(strOldName) != 0) { String strFToDelete = CFilePath.changeBaseName(strDbPath, strOldName + strExt); LOG.INFO("Bulk sync: remove old bulk file '" + strFToDelete + "'"); //RhoFile.deleteFile( strFToDelete.c_str() ); CRhoFile.deleteFile(strFToDelete); } RhoConf.getInstance().setString("bulksync_filename", strNewName, true); return(CFilePath.changeBaseName(strDbPath, strNewName + strExt)); }
public NetResponse pullFile(String strUrl, String strFileName, IRhoSession oSession, Hashtable <String, String> headers) { NetResponse resp = null; m_isPullFile = true; m_bCancel = false; try{ if (!strFileName.startsWith("file:")) { try{ strFileName = CFilePath.join(CRhodesApp.getRhoRootPath(), strFileName); } catch (IOException e) { LOG.ERROR("getDirPath failed.", e); } } m_pulledFile = RhoClassFactory.createFile(); m_pulledFile.open(strFileName, CRhoFile.EOpenModes.OpenForReadWrite); m_pulledFile.setPosTo(m_pulledFile.size()); do { resp = doRequest("GET", strUrl, null, oSession, headers, m_pulledFile.size()); }while(!m_bCancel && (resp == null || resp.isOK()) && m_nCurDownloadSize > 0); }finally{ if (m_pulledFile != null) { try { m_pulledFile.close(); } catch (IOException e) { LOG.ERROR("file closing failed.", e); } m_pulledFile = null; } } copyHashtable(m_OutHeaders, headers); m_isPullFile = false; m_nCurDownloadSize = 0; return(resp != null && !m_bCancel ? resp : makeResponse("", Convert.ToInt32(HttpStatusCode.InternalServerError))); }
public static void SyncBlob_UpdateSchemaCallback(Sqlite3.sqlite3_context dbContext, int nArgs, Sqlite3.Mem[] Args) { String szOldValue = Sqlite3.sqlite3_value_text(Args[0]); String szNewValue = Sqlite3.sqlite3_value_text(Args[1]); if (szOldValue == szNewValue || szOldValue == null) { return; } if (szOldValue != null && szNewValue != null && szOldValue == szNewValue) { return; } if (szOldValue != null) { String strFilePath = RHODESAPP().resolveDBFilesPath(szOldValue); CRhoFile.deleteFile(strFilePath); } }
String processIndex(CRoute route, String method, String uri, String query, String body) { String fullPath = uri.StartsWith(m_root) ? uri : CFilePath.join(m_root, uri); String strIndexFile = getIndex(fullPath); if (!CRhoFile.isResourceFileExist(strIndexFile)) { String error = "<html><font size=\"+4\"><h2>404 Not Found.</h2> The file " + uri + " was not found.</font></html>"; String strFilePath = CFilePath.join(m_root, "rhodes_error") + ".gen.html"; CRhoFile.recursiveCreateDir(strFilePath); CRhoFile.writeStringToFile(strFilePath, error); return(strFilePath); } if (CFilePath.getExtension(fullPath).Length > 0) { return(strIndexFile); } Object rhoReq = create_request_hash(route, method, uri, query, null, body); Object rhoResp = RhoRuby.callServeIndex(strIndexFile, rhoReq); String strRedirectUrl = getRedirectUrl(rhoResp); if (strRedirectUrl.Length > 0) { return(strRedirectUrl); } strIndexFile += ".gen.html"; CRhoFile.recursiveCreateDir(strIndexFile); CRhoFile.writeDataToFile(strIndexFile, getResponseBody(rhoResp)); if (method == "GET") { RHODESAPP().keepLastVisitedUrl(uri); } return(strIndexFile); }
private void loadLogPosition() { if (m_pPosFile == null) { m_pPosFile = new CRhoFile(); } if (!m_pPosFile.isOpened()) { String strPosPath = getLogConf().getLogFilePath() + "_pos"; m_pPosFile.open(strPosPath, CRhoFile.EOpenModes.OpenForReadWrite); } if (!m_pPosFile.isOpened()) { return; } m_pPosFile.movePosToStart(); String strPos = m_pPosFile.readString(); if (strPos.length() == 0) { return; } m_nCirclePos = int.Parse(strPos); if (m_nCirclePos < 0 || m_nCirclePos > (int)m_nFileLogSize) { m_nCirclePos = -1; } if (m_nCirclePos >= 0) { m_pFile.setPosTo(m_nCirclePos); } }
public override Stream OpenInputFileStream(string path, FileMode mode, FileAccess access, FileShare share) { //TODO: OpenInputFileStream with params Stream st = null; if (access == FileAccess.Read) { return(OpenInputFileStream(path)); } else { CRhoFile file = new CRhoFile(); file.open(path, CRhoFile.EOpenModes.OpenForReadWrite); st = file.getStream(); } if (st == null) { throw new System.IO.FileNotFoundException(); } return(st); }
public override bool DirectoryExists(string path) { return(CRhoFile.isDirectoryExist(path)); }
public override void CreateDirectory(string path) { CRhoFile.createDirectory(path); }
public CResponse decide(String method, String uri, String query, String body) { if (uri.EndsWith(".gen.html") || (isknowntype(uri) && uri.StartsWith(m_root))) { return(new CResponse(false)); } if (process_registered(uri)) { return(new CResponse(false)); } CRoute route = new CRoute(); if (dispatch(uri, route)) { Object rhoReq = create_request_hash(route, method, uri, query, null, body); Object rhoResp = RhoRuby.callServe(rhoReq); String strRedirectUrl = getRedirectUrl(rhoResp); if (strRedirectUrl.Length > 0) { return(new CResponse(strRedirectUrl)); } String strFilePath = RHODESAPP().canonicalizeRhoPath(uri) + ".gen.html"; if (route.id.Length > 0) { strFilePath = CFilePath.join(m_root, route.application + "/" + route.model + "/" + route.action) + ".gen.html"; } CRhoFile.recursiveCreateDir(strFilePath); CRhoFile.writeDataToFile(strFilePath, getResponseBody(rhoResp)); if (method == "GET") { RHODESAPP().keepLastVisitedUrl(uri); } return(new CResponse(strFilePath)); } String fullPath = uri.StartsWith(m_root) ? uri : CFilePath.join(m_root, uri); String strIndexFile = getIndex(fullPath); if (strIndexFile.Length > 0) { if (!CRhoFile.isResourceFileExist(strIndexFile)) { String error = "<html><font size=\"+4\"><h2>404 Not Found.</h2> The file " + uri + " was not found.</font></html>"; String strFilePath = CFilePath.join(m_root, "rhodes_error") + ".gen.html"; CRhoFile.recursiveCreateDir(strFilePath); CRhoFile.writeStringToFile(strFilePath, error); return(new CResponse(strFilePath)); } if (CFilePath.getExtension(fullPath).Length > 0) { return(new CResponse(strIndexFile)); } Object rhoReq = create_request_hash(route, method, uri, query, null, body); Object rhoResp = RhoRuby.callServeIndex(strIndexFile, rhoReq); String strRedirectUrl = getRedirectUrl(rhoResp); if (strRedirectUrl.Length > 0) { return(new CResponse(strRedirectUrl)); } strIndexFile += ".gen.html"; CRhoFile.recursiveCreateDir(strIndexFile); CRhoFile.writeDataToFile(strIndexFile, getResponseBody(rhoResp)); if (method == "GET") { RHODESAPP().keepLastVisitedUrl(uri); } return(new CResponse(strIndexFile)); } return(new CResponse(false)); }
private String getSqlScript() { return(CRhoFile.readStringFromResourceFile("db/syncdb.schema")); }
public void rb_destroy_tables(Vector <String> vecIncludes, Vector <String> vecExcludes) { if (!m_bIsOpen) { return; } IDBStorage db = null; try{ String dbNewName = CFilePath.changeBaseName(m_strDBPath, "resetdbtemp.sqlite"); CRhoFile.deleteFile(dbNewName); CRhoFile.deleteFile(dbNewName + "-journal"); CRhoFile.deleteFile(dbNewName + ".version"); db = RhoClassFactory.createDBStorage(); db.open(dbNewName, getSqlScript(), getEncryptionInfo()); String[] vecTables = m_dbStorage.getAllTableNames(); //IDBResult res; db.startTransaction(); for (int i = 0; i < vecTables.Length; i++) { String tableName = vecTables[i]; if (destroyTableName(tableName, vecIncludes, vecExcludes)) { continue; } copyTable(tableName, this.m_dbStorage, db); } db.commit(); db.close(); String dbOldName = m_strDBPath; m_dbStorage.close(); m_dbStorage = null; m_bIsOpen = false; CRhoFile.deleteFilesInFolder(RHODESAPP().getBlobsDirPath()); string[] ar1 = CRhoFile.enumDirectory("db"); CRhoFile.deleteFile(dbOldName); CRhoFile.deleteFile(dbOldName + "-journal"); CRhoFile.renameFile(dbNewName, dbOldName); CRhoFile.renameFile(dbNewName + "-journal", dbOldName + "-journal"); string[] ar2 = CRhoFile.enumDirectory("db"); m_dbStorage = RhoClassFactory.createDBStorage(); m_dbStorage.open(m_strDBPath, getSqlScript(), getEncryptionInfo()); m_bIsOpen = true; string[] ar3 = CRhoFile.enumDirectory("db"); m_dbStorage.setDbCallback(new DBCallback(this)); }catch (Exception e) { LOG.ERROR("destroy_table failed.", e); if (!m_bIsOpen) { LOG.ERROR("destroy_table error.Try to open old DB."); try{ m_dbStorage.open(m_strDBPath, getSqlScript(), getEncryptionInfo()); m_bIsOpen = true; }catch (Exception exc) { LOG.ERROR("destroy_table open old table failed.", exc); } } try { if (db != null) { db.close(); } } catch (DBException e1) { LOG.ERROR("closing of DB caused exception: " + e1.Message); } throw e; } }
void checkDBVersion() { DBVersion dbNewVer = new DBVersion(); dbNewVer.m_strRhoVer = RhoRuby.getRhoDBVersion(); dbNewVer.m_strAppVer = RhoConf.getInstance().getString("app_db_version"); String strEncryptionInfo = getEncryptionInfo(); dbNewVer.m_bEncrypted = strEncryptionInfo != null && strEncryptionInfo.length() > 0; dbNewVer.m_bSqlite = true; DBVersion dbVer = new DBVersion(); dbVer.fromFile(m_strDbVerPath); if (dbVer.m_strRhoVer.length() == 0) { dbNewVer.toFile(m_strDbVerPath); return; } boolean bRhoReset = dbVer.isRhoVerChanged(dbNewVer); boolean bAppReset = dbVer.isAppVerChanged(dbNewVer); boolean bDbFormatChanged = dbVer.isDbFormatChanged(dbNewVer); if (!bDbFormatChanged && dbVer.m_bEncrypted) { //TODO: check encryption key //if (!com.rho.RhoCrypto.isKeyExist(strEncryptionInfo) ) // bDbFormatChanged = true; } if (bDbFormatChanged) { LOG.INFO("Reset Database( format changed ):" + m_strDBPath); } if (bRhoReset && !bAppReset && !bDbFormatChanged) { bRhoReset = !migrateDB(dbVer, dbNewVer); } if (bRhoReset || bAppReset || bDbFormatChanged) { if (!bDbFormatChanged) { IDBStorage db = null; try { db = RhoClassFactory.createDBStorage(); if (db.isDbFileExists(m_strDBPath)) { db.open(m_strDBPath, "", strEncryptionInfo); IDBResult res = db.executeSQL("SELECT * FROM client_info", null, false, false); if (!res.isEnd()) { m_strClientInfoInsert = createInsertStatement(res, "client_info"); m_dataClientInfo = res.getCurData(); } } }catch (Exception exc) { LOG.ERROR("Copy client_info table failed.", exc); }finally { if (db != null) { try { db.close(); } catch (Exception) { } } } } m_dbStorage.deleteAllFiles(m_strDBPath); if (this.m_strDbPartition.compareTo("user") == 0) //do it only once { String fName = makeBlobFolderName(); CRhoFile.deleteDirectory(fName); makeBlobFolderName(); //Create folder back } dbNewVer.toFile(m_strDbVerPath); if (RhoConf.getInstance().isExist("bulksync_state") && RhoConf.getInstance().getInt("bulksync_state") != 0) { RhoConf.getInstance().setInt("bulksync_state", 0, true); } } }