示例#1
0
void ParamSet::AddSampledSpectrumFiles(const string &name, const char **names,
        int nItems) {
    EraseSpectrum(name);
    Spectrum *s = new Spectrum[nItems];
    for (int i = 0; i < nItems; ++i) {
        string fn = AbsolutePath(ResolveFilename(names[i]));
        if (cachedSpectra.find(fn) != cachedSpectra.end()) {
            s[i] = cachedSpectra[fn];
            continue;
        }

        vector<float> vals;
        if (!ReadFloatFile(fn.c_str(), &vals)) {
            Warning("Unable to read SPD file \"%s\".  Using black distribution.",
                    fn.c_str());
            s[i] = Spectrum(0.);
        }
        else {
            if (vals.size() % 2) {
                Warning("Extra value found in spectrum file \"%s\". "
                        "Ignoring it.", fn.c_str());
            }
            vector<float> wls, v;
            for (uint32_t j = 0; j < vals.size() / 2; ++j) {
                wls.push_back(vals[2*j]);
                v.push_back(vals[2*j+1]);
            }
            s[i] = Spectrum::FromSampled(&wls[0], &v[0], wls.size());
        }
        cachedSpectra[fn] = s[i];
    }

    spectra.push_back(new ParamSetItem<Spectrum>(name, s, nItems));
    delete[] s;
}
示例#2
0
string ParamSet::FindOneFilename(const string &name, const string &d) const {
    string filename = FindOneString(name, "");
    if (filename == "")
        return d;
    filename = AbsolutePath(ResolveFilename(filename));
    return filename;
}
示例#3
0
// See if the .dsp or .dsw file changed on disk.
bool WorkspaceInfo::CheckProjectIntegrity(CString projectName)
{
	// Resolve the filename.
	ResolveFilename(GetWorkspaceLocation(), projectName);

	// Scan the projects list to find the project named [projectName].
	POSITION pos = m_projects.GetHeadPosition();
	Project* prj = NULL;
	while (pos != NULL)
	{
		prj = &m_projects.GetNext(pos);
		if (prj->m_name.CompareNoCase(projectName) == 0)
			break;
		prj = NULL;
	}

	// If it doesn't exist, exit.
	if (!prj)
	{
//			AfxMessageBox("Refresh: Project names differ.");
		return false;
	}

	// Check the date and time stamp.
	CFileStatus fileStatus;
	if (CFile::GetStatus(prj->m_name, fileStatus) == FALSE)
		return false;
	if (prj->m_timeStamp != fileStatus.m_mtime)
	{
//			AfxMessageBox("Refresh:  Date/time stamp differs.");
		return false;
	}

	return true;
}
示例#4
0
bool File::Create(File& file, const CString& rootPath, CString filename, bool resolve)
{
	// Resolve the filename.
	if (resolve)
		ResolveFilename(rootPath, filename);

	// Find the path.
	int pathEndPosition = filename.ReverseFind('\\');
	if (pathEndPosition == -1)
		return false;
	file.SetPath(filename.Left(pathEndPosition + 1));

	// Find the extension.
	int extPosition = filename.ReverseFind('.');
	if (extPosition != -1)
	{
		file.m_ext = filename.Mid(extPosition + 1);
		file.m_ext.MakeLower();
	}
	else
		extPosition = filename.GetLength();

	// Find the file title.
	file.m_title = filename.Mid(pathEndPosition + 1, extPosition - (pathEndPosition + 1));

	// Build the shortened name (no symbols).
	char shortName[200];
	int sLen = 0;
	for (int i = 0; i < file.m_title.GetLength(); i++)
		if (isalnum(file.m_title[i]))
			shortName[sLen++] = tolower(file.m_title[i]);
	shortName[sLen] = 0;
	file.m_shortName = shortName;

	// Set the parent.
	file.m_parent = NULL;

	return true;
}
示例#5
0
void ParamSet::AddSampledSpectrumFiles(const std::string &name,
                                       const char **names, int nValues) {
    EraseSpectrum(name);
    std::unique_ptr<Spectrum[]> s(new Spectrum[nValues]);
    for (int i = 0; i < nValues; ++i) {
        std::string fn = AbsolutePath(ResolveFilename(names[i]));
        if (cachedSpectra.find(fn) != cachedSpectra.end()) {
            s[i] = cachedSpectra[fn];
            continue;
        }

        std::vector<Float> vals;
        if (!ReadFloatFile(fn.c_str(), &vals)) {
            Warning(
                "Unable to read SPD file \"%s\".  Using black distribution.",
                fn.c_str());
            s[i] = Spectrum(0.);
        } else {
            if (vals.size() % 2) {
                Warning(
                    "Extra value found in spectrum file \"%s\". "
                    "Ignoring it.",
                    fn.c_str());
            }
            std::vector<Float> wls, v;
            for (size_t j = 0; j < vals.size() / 2; ++j) {
                wls.push_back(vals[2 * j]);
                v.push_back(vals[2 * j + 1]);
            }
            s[i] = Spectrum::FromSampled(&wls[0], &v[0], wls.size());
        }
        cachedSpectra[fn] = s[i];
    }

    std::shared_ptr<ParamSetItem<Spectrum>> psi(
        new ParamSetItem<Spectrum>(name, std::move(s), nValues));
    spectra.push_back(psi);
}
示例#6
0
static int AccessControl(EvalContext *ctx, const char *req_path, ServerConnectionState *conn, int encrypt)
{
    int access = false;
    char transrequest[CF_BUFSIZE];
    struct stat statbuf;
    char translated_req_path[CF_BUFSIZE];
    char transpath[CF_BUFSIZE];

/*
 * /var/cfengine -> $workdir translation.
 */
    TranslatePath(translated_req_path, req_path);

    if (ResolveFilename(translated_req_path, transrequest))
    {
        Log(LOG_LEVEL_VERBOSE, "Filename %s is resolved to %s", translated_req_path, transrequest);
    }
    else
    {
        Log(LOG_LEVEL_INFO, "Couldn't resolve (realpath: %s) filename: %s",
            GetErrorStr(), translated_req_path);
        return false;                /* can't continue without transrequest */
    }

    if (lstat(transrequest, &statbuf) == -1)
    {
        Log(LOG_LEVEL_INFO, "Couldn't stat (lstat: %s) filename: %s",
            GetErrorStr(), transrequest);
        return false;
    }

    Log(LOG_LEVEL_DEBUG, "AccessControl, match (%s,%s) encrypt request = %d", transrequest, conn->hostname, encrypt);

    if (SV.admit == NULL)
    {
        Log(LOG_LEVEL_INFO, "cf-serverd access list is empty, no files are visible");
        return false;
    }

    conn->maproot = false;

    for (Auth *ap = SV.admit; ap != NULL; ap = ap->next)
    {
        int res = false;

        Log(LOG_LEVEL_DEBUG, "Examining rule in access list (%s,%s)", transrequest, ap->path);

        /* TODO MapName when constructing this list. */
        strncpy(transpath, ap->path, CF_BUFSIZE - 1);
        MapName(transpath);

        /* If everything is allowed */
        if ((strcmp(transpath, FILE_SEPARATOR_STR) == 0)
            ||
            /* or if transpath is a parent directory of transrequest */
            (strlen(transrequest) > strlen(transpath)
            && strncmp(transpath, transrequest, strlen(transpath)) == 0
            && transrequest[strlen(transpath)] == FILE_SEPARATOR)
            ||
            /* or if it's an exact match */
            (strcmp(transpath, transrequest) == 0))
        {
            res = true;
        }

        /* Exact match means single file to admit */
        if (strcmp(transpath, transrequest) == 0)
        {
            res = true;
        }

        if (res)
        {
            Log(LOG_LEVEL_VERBOSE, "Found a matching rule in access list (%s in %s)", transrequest, transpath);

            if (stat(transpath, &statbuf) == -1)
            {
                Log(LOG_LEVEL_INFO,
                      "Warning cannot stat file object %s in admit/grant, or access list refers to dangling link",
                      transpath);
                continue;
            }

            if ((!encrypt) && (ap->encrypt == true))
            {
                Log(LOG_LEVEL_ERR, "File %s requires encrypt connection...will not serve", transpath);
                access = false;
            }
            else
            {
                Log(LOG_LEVEL_DEBUG, "Checking whether to map root privileges..");

                if ((IsMatchItemIn(ap->maproot, MapAddress(conn->ipaddr))) ||
                    (IsRegexItemIn(ctx, ap->maproot, conn->hostname)))
                {
                    conn->maproot = true;
                    Log(LOG_LEVEL_VERBOSE, "Mapping root privileges to access non-root files");
                }

                if ((IsMatchItemIn(ap->accesslist, MapAddress(conn->ipaddr)))
                    || (IsRegexItemIn(ctx, ap->accesslist, conn->hostname)))
                {
                    access = true;
                    Log(LOG_LEVEL_DEBUG, "Access granted to host: %s", conn->ipaddr);
                }
            }
            break;
        }
    }

    for (Auth *dp = SV.deny; dp != NULL; dp = dp->next)
    {
        strncpy(transpath, dp->path, CF_BUFSIZE - 1);
        MapName(transpath);

        /* If everything is denied */
        if ((strcmp(transpath, FILE_SEPARATOR_STR) == 0)
            ||
            /* or if transpath is a parent directory of transrequest */
            (strlen(transrequest) > strlen(transpath) &&
             strncmp(transpath, transrequest, strlen(transpath)) == 0 &&
             transrequest[strlen(transpath)] == FILE_SEPARATOR)
            ||
            /* or if it's an exact match */
            (strcmp(transpath, transrequest) == 0))
        {
            if ((IsMatchItemIn(dp->accesslist, MapAddress(conn->ipaddr))) ||
                (IsRegexItemIn(ctx, dp->accesslist, conn->hostname)))
            {
                access = false;
                Log(LOG_LEVEL_INFO, "Host '%s' in deny list, explicitly denying access to '%s'",
                    conn->ipaddr, transrequest);
                break;
            }
        }
    }

    if (access)
    {
        Log(LOG_LEVEL_VERBOSE, "Host %s granted access to %s", conn->hostname, req_path);

        if (encrypt && LOGENCRYPT)
        {
            /* Log files that were marked as requiring encryption */
            Log(LOG_LEVEL_INFO, "Host %s granted access to %s", conn->hostname, req_path);
        }
    }
    else
    {
        Log(LOG_LEVEL_INFO, "Host %s denied access to %s", conn->hostname, req_path);
    }

    return access;
}
示例#7
0
int AccessControl(EvalContext *ctx, const char *req_path, ServerConnectionState *conn, int encrypt)
{
    Auth *ap;
    int access = false;
    char transrequest[CF_BUFSIZE];
    struct stat statbuf;
    char translated_req_path[CF_BUFSIZE];
    char transpath[CF_BUFSIZE];

/*
 * /var/cfengine -> $workdir translation.
 */
    TranslatePath(translated_req_path, req_path);

    if (ResolveFilename(translated_req_path, transrequest))
    {
        Log(LOG_LEVEL_VERBOSE, "Filename %s is resolved to %s", translated_req_path, transrequest);
    }
    else
    {
        Log(LOG_LEVEL_INFO, "Couldn't resolve (realpath: %s) filename: %s",
            GetErrorStr(), translated_req_path);
    }

    if (lstat(transrequest, &statbuf) == -1)
    {
        Log(LOG_LEVEL_INFO, "Couldn't stat (lstat: %s) filename: %s",
            GetErrorStr(), transrequest);
        return false;
    }

    Log(LOG_LEVEL_DEBUG, "AccessControl, match (%s,%s) encrypt request = %d", transrequest, conn->hostname, encrypt);

    if (SV.admit == NULL)
    {
        Log(LOG_LEVEL_INFO, "cf-serverd access list is empty, no files are visible");
        return false;
    }

    conn->maproot = false;

    for (ap = SV.admit; ap != NULL; ap = ap->next)
    {
        int res = false;

        Log(LOG_LEVEL_DEBUG, "Examining rule in access list (%s,%s)", transrequest, ap->path);

        strncpy(transpath, ap->path, CF_BUFSIZE - 1);
        MapName(transpath);

        if ((strlen(transrequest) > strlen(transpath)) && (strncmp(transpath, transrequest, strlen(transpath)) == 0)
            && (transrequest[strlen(transpath)] == FILE_SEPARATOR))
        {
            res = true;         /* Substring means must be a / to link, else just a substring og filename */
        }

        /* Exact match means single file to admit */

        if (strcmp(transpath, transrequest) == 0)
        {
            res = true;
        }

        if (strcmp(transpath, "/") == 0)
        {
            res = true;
        }

        if (res)
        {
            Log(LOG_LEVEL_VERBOSE, "Found a matching rule in access list (%s in %s)", transrequest, transpath);

            if (stat(transpath, &statbuf) == -1)
            {
                Log(LOG_LEVEL_INFO,
                      "Warning cannot stat file object %s in admit/grant, or access list refers to dangling link\n",
                      transpath);
                continue;
            }

            if ((!encrypt) && (ap->encrypt == true))
            {
                Log(LOG_LEVEL_ERR, "File %s requires encrypt connection...will not serve", transpath);
                access = false;
            }
            else
            {
                Log(LOG_LEVEL_DEBUG, "Checking whether to map root privileges..");

                if ((IsMatchItemIn(ctx, ap->maproot, MapAddress(conn->ipaddr))) ||
                    (IsRegexItemIn(ctx, ap->maproot, conn->hostname)))
                {
                    conn->maproot = true;
                    Log(LOG_LEVEL_VERBOSE, "Mapping root privileges to access non-root files");
                }

                if ((IsMatchItemIn(ctx, ap->accesslist, MapAddress(conn->ipaddr)))
                    || (IsRegexItemIn(ctx, ap->accesslist, conn->hostname)))
                {
                    access = true;
                    Log(LOG_LEVEL_DEBUG, "Access privileges - match found");
                }
            }
            break;
        }
    }

    if (strncmp(transpath, transrequest, strlen(transpath)) == 0)
    {
        for (ap = SV.deny; ap != NULL; ap = ap->next)
        {
            if (IsRegexItemIn(ctx, ap->accesslist, conn->hostname))
            {
                access = false;
                Log(LOG_LEVEL_INFO, "Host %s explicitly denied access to %s", conn->hostname, transrequest);
                break;
            }
        }
    }

    if (access)
    {
        Log(LOG_LEVEL_VERBOSE, "Host %s granted access to %s", conn->hostname, req_path);

        if (encrypt && LOGENCRYPT)
        {
            /* Log files that were marked as requiring encryption */
            Log(LOG_LEVEL_INFO, "Host %s granted access to %s", conn->hostname, req_path);
        }
    }
    else
    {
        Log(LOG_LEVEL_INFO, "Host %s denied access to %s", conn->hostname, req_path);
    }

    if (!conn->rsa_auth)
    {
        Log(LOG_LEVEL_INFO, "Cannot map root access without RSA authentication");
        conn->maproot = false;  /* only public files accessible */
    }

    return access;
}
示例#8
0
// Internal helper function.
WorkspaceInfo::Project* WorkspaceInfo::AddHelper(CString projectName)
{
	// Resolve the filename.
	ResolveFilename(GetWorkspaceLocation(), projectName);
	
	// Make sure there is an extension.
	int dotPosition = projectName.ReverseFind('.');
	if (dotPosition == -1)
	{
		// What?!
		return NULL;
	}

	// Make sure the file exists.
	CFileStatus fileStatus;
	if (CFile::GetStatus(projectName, fileStatus) == FALSE)
		return NULL;

	// Create the project structure.
	Project prjToAdd;
	prjToAdd.m_name = projectName;
	prjToAdd.m_active = true;
	prjToAdd.m_timeStamp = fileStatus.m_mtime;
	prjToAdd.m_parent = NULL;

	// This is a little slow, but it can be moved later.
	CStdioFile file;
	if (file.Open(GetExcludeFilename(), CFile::modeRead) == TRUE)
	{
		CString line;
		
		// Count the number of extra projects.
		while (1)
		{
			// Read in a project name.
			if (!file.ReadString(line))
				break;

			// Resolve the exclude filename.
			ResolveFilename(GetWorkspaceLocation(), line);

			// If it matches, then the project is inactive.
			if (prjToAdd.m_name.CompareNoCase(line) == 0)
			{
				prjToAdd.m_active = false;
				break;
			}
		} 

		// Close the file.
		file.Close();
	}

	// Add it to the end of the projects list.
	POSITION pos = m_projects.AddTail(prjToAdd);
	Project& prj = WorkspaceInfo::m_projects.GetAt(pos);
		
	// If the project is inactive, leave.
	if (!prj.IsActive())
	{
		return &prj;
	}

	// Determine which type of file this is:
	CString ext = prj.m_name.Mid(dotPosition + 1);
	ext.MakeLower();
	if (ext == "dsw")
	{
		// This is a workspace file.
		ReadDSWFile(prj);
	}
	else if (ext == "dsp")
	{
		// Assume it is a project file.
		ReadDSPFile(prj);
	}
	else
	{
		// Fail
	}

	return &prj;
}
示例#9
0
// Read in a .dsw file.
void WorkspaceInfo::ReadDSWFile(Project& prj)
{
	// Open the .dsw file.
	CStdioFile file;
	if (file.Open(prj.m_name, CFile::modeRead) == FALSE)
	{
		// Huh?
		return;
	}

	// Build the root path to resolve filenames from.
	CString rootPath = prj.m_name.Left(prj.m_name.ReverseFind('\\') + 1);

	// Begin reading the file.
	CString line;
	while (1)
	{
		// Read in a line from the file.
		if (!file.ReadString(line))
			break;

		// Look for something that looks like this.
		// Project: "!EagleLib"=".\Prj\!EagleLib.dsp" - Package Owner=<4>
		// Project: "Gfx"=.\Prj\Gfx.dsp - Package Owner=<4>
		if (line.GetLength() <= 8  ||  strncmp(line, "Project:", 8) != 0)
			continue;

		// Search for the =.
		int endPos;		// Will be one past the last letter.
		int startPos = line.Find('=');
		if (startPos == -1)
			continue;
		startPos++;		// Move to the beginning of the name.

/*
		// See if the name is quoted.
		if (line[startPos] == '"')
		{
			// Move past the quote.
			startPos++;
			
			// Find the closing quote.
			endPos = line.Find('"', startPos);
			if (endPos == -1)
				continue;
		}
		else //if (line[namePos] == '"')
		{
			// Find a space, since that should denote the end of the filename.
			endPos = line.Find(' ', startPos);
		}
*/

		// Got the name isolated.  Add it.
		CString projectPath = line.Mid(startPos, endPos - startPos);
		ResolveFilename(rootPath, projectPath);
		Project* newlyAddedProject = AddHelper(projectPath);
		if (newlyAddedProject)
			newlyAddedProject->m_parent = &prj;
	} //while(1)
	// Close the .dsp file.
	file.Close();
}