Ejemplo n.º 1
0
static void _loadModuleDir(SG_context *pCtx, const SG_pathname *path, const char *modname, JSContext *cx, JSObject *glob)
{
	SG_rbtree * pJavascriptFiles = NULL;
	SG_rbtree_iterator * pJavascriptFile = NULL;
	const char *szJavascriptFile = NULL; 
	SG_pathname *pJavascriptFilePath = NULL;
	SG_bool ok = SG_FALSE;
	SG_bool valid = SG_FALSE;
	char *psz_js = NULL; // free
	SG_uint32 len_js = 0;
	jsval rval = JSVAL_VOID;
	
	SG_ERR_CHECK(  SG_dir__list(pCtx, path, NULL, NULL, ".js", &pJavascriptFiles)  );
	SG_ERR_CHECK(  SG_rbtree__iterator__first(pCtx, &pJavascriptFile, pJavascriptFiles, &ok, &szJavascriptFile, NULL)  );
	SG_ERR_CHECK(  SG_PATHNAME__ALLOC__COPY(pCtx, &pJavascriptFilePath, path)  );

	while(ok)
	{
		SG_ERR_CHECK(  _isValidJsFile(pCtx, szJavascriptFile, &valid)  );

		if (valid)
		{
			SG_ERR_CHECK(  SG_pathname__append__from_sz(pCtx, pJavascriptFilePath, szJavascriptFile)  );

			SG_ERR_CHECK(  sg_read_entire_file(pCtx, pJavascriptFilePath, &psz_js, &len_js)  );
			if(!JS_EvaluateScript(cx, glob, psz_js, len_js, szJavascriptFile, 1, &rval))
			{
				SG_ERR_CHECK_CURRENT;
				SG_ERR_THROW2(SG_ERR_JS, (pCtx, "An error occurred loading %s javascript file '%s'", modname, szJavascriptFile));
			}
			SG_NULLFREE(pCtx, psz_js);
			SG_ERR_CHECK(  SG_pathname__remove_last(pCtx, pJavascriptFilePath)  );
		}

		SG_ERR_CHECK(  SG_rbtree__iterator__next(pCtx, pJavascriptFile, &ok, &szJavascriptFile, NULL)  );
	}

fail:
	SG_PATHNAME_NULLFREE(pCtx, pJavascriptFilePath);
	SG_RBTREE_ITERATOR_NULLFREE(pCtx, pJavascriptFile);
	SG_RBTREE_NULLFREE(pCtx, pJavascriptFiles);
	SG_NULLFREE(pCtx, psz_js);
}
Ejemplo n.º 2
0
static void _sg_jscore__install_modules(SG_context * pCtx, JSContext *cx, JSObject *glob,
	const SG_vhash *pServerConfig)
{
	SG_pathname *pModuleDirPath = NULL;
	SG_rbtree_iterator *pModuleDir = NULL;
	SG_rbtree *pModules = NULL;
	const char *szModuleDir = NULL;
	SG_bool ok = SG_FALSE;
	SG_uint32 len_js = 0;
	jsval rval;
	char *psz_js = NULL;
	jsval fo = JSVAL_VOID;
	JSObject * jsoServerConfig = NULL;

	if (gpJSCoreGlobalState->bSkipModules)
		return;

	SG_ERR_CHECK(  _modulesInstalled(pCtx, cx, glob, &ok)  );

	if (ok)
		return;

	if (! gpJSCoreGlobalState->pPathToDispatchDotJS)
		return;
	
	SG_ERR_CHECK(  _setModulesInstalled(pCtx, cx, glob)  );

	SG_ERR_CHECK(  sg_read_entire_file(pCtx, gpJSCoreGlobalState->pPathToDispatchDotJS, &psz_js, &len_js)  );
	if(!JS_EvaluateScript(cx, glob, psz_js, len_js, "dispatch.js", 1, &rval))
	{
		SG_ERR_CHECK_CURRENT;
		SG_ERR_THROW2(SG_ERR_JS, (pCtx, "An error occurred evaluating dispatch.js!"));
	}
	SG_NULLFREE(pCtx, psz_js);

	// Call init function in dispatch.js
	if(pServerConfig)
	{
		jsval arg;
		JSBool js_ok;
		jsval rval2;

		SG_JS_NULL_CHECK(  jsoServerConfig = JS_NewObject(cx, NULL, NULL, NULL)  );
		SG_ERR_CHECK(  sg_jsglue__copy_vhash_into_jsobject(pCtx, cx, pServerConfig, jsoServerConfig)  );
		arg = OBJECT_TO_JSVAL(jsoServerConfig);
		js_ok = JS_CallFunctionName(cx, glob, "init", 1, &arg, &rval2);
		SG_ERR_CHECK_CURRENT;
		if(!js_ok)
			SG_ERR_THROW2(SG_ERR_JS, (pCtx, "An error occurred initializing JavaScript framework: call to JavaScript init() failed"));
		
		jsoServerConfig = NULL;
	}

	// Load core.
	SG_ERR_CHECK(  _loadModuleDir(pCtx, gpJSCoreGlobalState->pPathToCore, "core", cx, glob)  );

	// Load modules.

	SG_ERR_CHECK(  SG_dir__list(pCtx, gpJSCoreGlobalState->pPathToModules, NULL, NULL, NULL, &pModules)  );
	SG_ERR_CHECK(  SG_rbtree__iterator__first(pCtx, &pModuleDir, pModules, &ok, &szModuleDir, NULL)  );
	SG_ERR_CHECK(  SG_PATHNAME__ALLOC__COPY(pCtx, &pModuleDirPath, gpJSCoreGlobalState->pPathToModules)  );
	while(ok)
	{
		if (szModuleDir[0] != '.')
		{
			SG_fsobj_stat fss;

			SG_ERR_CHECK(  SG_pathname__append__from_sz(pCtx, pModuleDirPath, szModuleDir)  );

			SG_ERR_CHECK(  SG_fsobj__stat__pathname(pCtx, pModuleDirPath, &fss)  );

			if (fss.type & SG_FSOBJ_TYPE__DIRECTORY)  // dot paths?
			{
				SG_ERR_CHECK(  _loadModuleDir(pCtx, pModuleDirPath, szModuleDir, cx, glob)  );
			}

			SG_ERR_CHECK(  SG_pathname__remove_last(pCtx, pModuleDirPath)  );
		}

		SG_ERR_CHECK(  SG_rbtree__iterator__next(pCtx, pModuleDir, &ok, &szModuleDir, NULL)  );
	}
												
	if (! JS_LookupProperty(cx, glob, "initModules", &fo))
	{
		SG_ERR_CHECK_CURRENT;
		SG_ERR_THROW2(SG_ERR_JS, (pCtx, "lookup of initModules() failed"));
	}

	if (!JSVAL_IS_VOID(fo))
		if (! JS_CallFunctionName(cx, glob, "initModules", 0, NULL, &rval))
			{
				SG_ERR_CHECK_CURRENT;
				SG_ERR_THROW2(SG_ERR_JS, (pCtx, "Call to initModules() failed"));
			}

fail:
	SG_NULLFREE(pCtx, psz_js);
	SG_PATHNAME_NULLFREE(pCtx, pModuleDirPath);
	SG_RBTREE_ITERATOR_NULLFREE(pCtx, pModuleDir);
	SG_RBTREE_NULLFREE(pCtx, pModules);
}
Ejemplo n.º 3
0
void SG_workingdir__find_mapping(
	SG_context* pCtx,
	const SG_pathname* pPathLocalDirectory,
	SG_pathname** ppPathMappedLocalDirectory, /**< Return the actual local directory that contains the mapping */
	SG_string** ppstrNameRepoInstanceDescriptor, /**< Return the name of the repo instance descriptor */
	char** ppszidGidAnchorDirectory /**< Return the GID of the repo directory */
	)
{
	SG_pathname* curpath = NULL;
	SG_string* result_pstrDescriptorName = NULL;
	char* result_pszidGid = NULL;
	SG_pathname* result_mappedLocalDirectory = NULL;
	SG_vhash* pvhMapping = NULL;
	SG_pathname* pDrawerPath = NULL;
	SG_pathname* pMappingFilePath = NULL;
	SG_vhash* pvh = NULL;

	SG_NULLARGCHECK_RETURN(pPathLocalDirectory);

	SG_ERR_CHECK(  SG_PATHNAME__ALLOC__COPY(pCtx, &curpath, pPathLocalDirectory)  );

	/* it's a directory, so it should have a final slash */
	SG_ERR_CHECK(  SG_pathname__add_final_slash(pCtx, curpath)  );

	while (SG_TRUE)
	{
		SG_ERR_CHECK(  SG_workingdir__get_drawer_path(pCtx, curpath, &pDrawerPath)  );

		SG_fsobj__verify_directory_exists_on_disk__pathname(pCtx, pDrawerPath);
		if (!SG_context__has_err(pCtx))
		{
			const char* pszDescriptorName = NULL;
			const char* pszGid = NULL;

			SG_ERR_CHECK(  SG_PATHNAME__ALLOC__PATHNAME_SZ(pCtx, &pMappingFilePath, pDrawerPath, "repo.json")  );
			SG_PATHNAME_NULLFREE(pCtx, pDrawerPath);

			SG_ERR_CHECK(  SG_vfile__slurp(pCtx, pMappingFilePath, &pvh)  );

			SG_ERR_CHECK(  SG_vhash__get__vhash(pCtx, pvh, "mapping", &pvhMapping)  );

			SG_PATHNAME_NULLFREE(pCtx, pMappingFilePath);

			SG_ERR_CHECK(  SG_vhash__get__sz(pCtx, pvhMapping, "descriptor", &pszDescriptorName)  );
			SG_ERR_CHECK(  SG_vhash__get__sz(pCtx, pvhMapping, "anchor", &pszGid)  );

			SG_ERR_CHECK(  SG_STRING__ALLOC(pCtx, &result_pstrDescriptorName)  );
			SG_ERR_CHECK(  SG_string__set__sz(pCtx, result_pstrDescriptorName, pszDescriptorName)  );

			if (pszGid)
			{
				SG_ERR_CHECK(  SG_gid__alloc_clone(pCtx, pszGid, &result_pszidGid)  );
			}
			else
			{
				result_pszidGid = NULL;
			}

			SG_VHASH_NULLFREE(pCtx, pvh);

			result_mappedLocalDirectory = curpath;
			curpath = NULL;

			break;
		}
		else
			SG_context__err_reset(pCtx);

		SG_PATHNAME_NULLFREE(pCtx, pDrawerPath);

		SG_pathname__remove_last(pCtx, curpath);

		if (SG_context__err_equals(pCtx, SG_ERR_CANNOTTRIMROOTDIRECTORY))
		{
			SG_context__err_reset(pCtx);
			break;
		}
		else
		{
			SG_ERR_CHECK_CURRENT;
		}
	}

	if (result_mappedLocalDirectory)
	{
        if (ppPathMappedLocalDirectory)
        {
		    *ppPathMappedLocalDirectory = result_mappedLocalDirectory;
        }
        else
        {
	        SG_PATHNAME_NULLFREE(pCtx, result_mappedLocalDirectory);
        }
        if (ppstrNameRepoInstanceDescriptor)
        {
            *ppstrNameRepoInstanceDescriptor = result_pstrDescriptorName;
        }
        else
        {
            SG_STRING_NULLFREE(pCtx, result_pstrDescriptorName);
        }
        if (ppszidGidAnchorDirectory)
        {
            *ppszidGidAnchorDirectory = result_pszidGid;
        }
        else
        {
            SG_NULLFREE(pCtx, result_pszidGid);
        }

		return;
	}
	else
	{
		SG_PATHNAME_NULLFREE(pCtx, curpath);

		SG_ERR_THROW_RETURN(SG_ERR_NOT_FOUND);
	}

fail:
	SG_VHASH_NULLFREE(pCtx, pvh);
	SG_PATHNAME_NULLFREE(pCtx, pDrawerPath);
	SG_PATHNAME_NULLFREE(pCtx, pMappingFilePath);
	SG_PATHNAME_NULLFREE(pCtx, result_mappedLocalDirectory);
	SG_PATHNAME_NULLFREE(pCtx, curpath);
}