コード例 #1
0
ファイル: pal.c プロジェクト: ArildF/masters
/*++
INIT_InitPalConfigDir

Create the PAL per-machine and per-configuration directories if necessary

returns TRUE on success, FALSE on failure; n case of failure, PAL_Initialize
must fail

--*/
BOOL INIT_InitPalConfigDir(void)
{
    uid_t uid;
    const char *librotor_fname;
    struct stat librotor_stat;
    int length;
    ino_t librotor_inode = -1;
    struct passwd *passwd;
    char hashpath[sizeof(ROTOR_NAME)+MAX_HASH_RESULT];
    char buffer[MAX_PATH];

    uid = getuid();

    /* Grab the full pathname to our librotor_pal.so
           Then we will try to grab the inode # using stat.
           If there is any error, we will just use an inode of
           -1 and put a WARN on the screen */
    librotor_fname = LOADGetLibRotorPalSoFileName();
    if (!librotor_fname)
    {
        return FALSE;
    }
    if (lstat(librotor_fname, &librotor_stat) != -1)
    {
        librotor_inode = librotor_stat.st_ino;
    }

    if (librotor_inode == -1)
    {
        WARN("Unable to get librotor_pal.so inode, cannot guarantee unique config dir\n");
    }
    if (snprintf(PALConfigDirName, 
         sizeof(PALConfigDirName),
                 "/tmp/.rotor-pal-%ld-%ld", 
                 (long) uid,
                 (long) librotor_inode) >= sizeof(PALConfigDirName)-1) {
        /* The directory name is too long */
        return FALSE;
    }

    /* Try to create the directory */
    if(-1 == mkdir(PALConfigDirName, 0700) )
    {
        /* If creation failed, see if it's because the directory existed */
        if( EEXIST == errno)
        {
            TRACE("PAL configuration directory %s already exists.\n",
                  PALConfigDirName);

            /* If the directory existed, see if we have full access to it */
            if(-1 == access(PALConfigDirName, R_OK|W_OK|X_OK))
            {
                ASSERT("PAL configuration directory %s has wrong permissions\n",
                      PALConfigDirName);
                return FALSE;
            }
        }
        else
        {
            ASSERT("mkdir() failed! error is %d (%s)\n", errno, strerror(errno));
            return FALSE;
        }
    }
    else
    {
        TRACE("Created PAL configuration directory %s\n", PALConfigDirName);
    }

    passwd = getpwuid(uid);
    if(!passwd)
    {
        ASSERT("getpwuid(%d) returned NULL!\n", uid);
        return FALSE;
    }
    TRACE("Home directory of user %d (%s) is %s\n",
          uid, passwd->pw_name, passwd->pw_dir);

    /* Build the string used to mangle object names, which is unique
       based on the inode of the PAL, plus the name "rotor" */
    strcpy(hashpath, ROTOR_NAME);
    HashPath(hashpath+sizeof(ROTOR_NAME)-1, librotor_fname);

    NameManglerLength = MultiByteToWideChar(CP_ACP, 0, 
                                            hashpath, -1, 
                                            NameManglerW, sizeof(NameManglerW)/sizeof(WCHAR))-1;
    TRACE("PAL mangled name is %s, with length %d\n", hashpath, NameManglerLength);

    /* Build the per-user configuration directory name */
    length = snprintf(buffer, sizeof(buffer), "%s/.%s", passwd->pw_dir, hashpath);
    if (length >= sizeof(buffer)) {
        /* the user directory name is too long */
        return FALSE;
    }
    TRACE("User director name is %s\n", buffer);

    length = MultiByteToWideChar(CP_ACP, 0, 
                                 buffer, -1,
                                 PALUserDirName, 
                                 sizeof(PALUserDirName)/sizeof(WCHAR));
    if (length == 0) {
        /* The user directory name is too long */
        return FALSE;
    }
    /* Ignore the error code, as it may be ERROR_ALREADY_EXISTS (183) */
    CreateDirectoryW(PALUserDirName, NULL);

    return TRUE;
}
コード例 #2
0
ファイル: hashmgr.cpp プロジェクト: xmoeproject/X-moe
int CHashManager::HashPath(char *pszBasePath, char *pszFileSpec)
{
	recursivesafe long lHandle = 0;
	recursivesafe long lSearcherHandle = 0;
	recursivesafe finddata fdInfo;
	recursivesafe char szFull[RH_MAX_PATH];
	recursivesafe char szAll[RH_MAX_PATH];

	if(pszBasePath == NULL) return RH_INVALID_PATH;
	if(pszFileSpec == NULL) return RH_INVALID_PATH;

	fmtPath(pszBasePath);
	fmtPath(pszFileSpec);

	if(strlen(pszBasePath) == 0) getcwd(pszBasePath, RH_MAX_PATH);
	fileonly(pszFileSpec);

	strcpy(szAll, pszBasePath);
	catdirsep(szAll);
	strcat(szAll, SZ_DIR_ALL);

	#ifdef RH_DEBUG
		printf("Function HashPath: pszBasePath=%s, pszFileSpec=%s", pszBasePath, pszFileSpec);
		printf(CPS_NEWLINE);
		printf("Function HashPath: szAll=%s", szAll);
		printf(CPS_NEWLINE);
	#endif

	//////////////////////////////////////////////////////////////////////////
	// Start directory enumeration code

	lSearcherHandle = findfirst(szAll, &fdInfo);
	while(1)
	{
		if(fdInfo.attrib & _A_SUBDIR)
		{
			if((ispathnav(fdInfo.name) == false) && (m_bRecursive))
			{
				if(chdir(fdInfo.name) == 0)
				{
					getcwd(szFull, RH_MAX_PATH);

					#ifdef RH_DEBUG
						printf("Opening new scan path: %s, filemask: %s", szFull, pszFileSpec);
						printf(CPS_NEWLINE);
					#endif

					HashPath(szFull, pszFileSpec);
					chdir(SZ_LEVEL_UP);
				}
			}
		}

		if(findnext(lSearcherHandle, &fdInfo) != 0) break;
	}
	findclose(lSearcherHandle);
	lSearcherHandle = 0;

	// End directory enumeration code
	//////////////////////////////////////////////////////////////////////////

	memset(&fdInfo, 0, sizeof(finddata));
	lHandle = findfirst(pszFileSpec, &fdInfo);
	if(lHandle == EINVAL) return RH_INVALID_PATH;
	if(lHandle == ENOENT) return RH_NO_PATTERN_MATCH;
	if(lHandle == -1) return RH_CANNOT_OPEN_FILE;

	while(1)
	{
		if(fdInfo.attrib & _A_SUBDIR)
		{
			// Don't process directories here
		}
		else
		{
			if(m_bFullPath)
			{
				fullpath(szFull, fdInfo.name, RH_MAX_PATH);
				HashFile(szFull);
			}
			else
			{
				HashFile(fdInfo.name);
			}
			printf(CPS_NEWLINE);
		}

		if(findnext(lHandle, &fdInfo) != 0) break;
	}

	findclose(lHandle);
	lHandle = 0;

	return RH_SUCCESS;
}