Example #1
0
static BOOL append_file_name(LPTSTR lpPath, LPCTSTR lpFileName, size_t nFileNameLen = -1)
{
	if( lpPath[0] == _T('\0') )
	{
		return FALSE;
	}

	size_t nPathLen = _tcslen(lpPath);
	LPTSTR lpDstFileName = lpPath + nPathLen;
	
	if ( lpDstFileName[-1] != _T('\\') )
	{
		*lpDstFileName++ = _T('\\');
		nPathLen++;
	}

	while ( is_path_separator_char(*lpFileName) )
	{
		lpFileName++;
	}

	return fix_path_separator(lpDstFileName, MAX_PATH - nPathLen - 1, lpFileName, nFileNameLen);
}
Example #2
0
/*
 * create_script_for_old_cluster_deletion()
 *
 *	This is particularly useful for tablespace deletion.
 */
void
create_script_for_old_cluster_deletion(char **deletion_script_file_name)
{
	FILE	   *script = NULL;
	int			tblnum;
	char		old_cluster_pgdata[MAXPGPATH];

	*deletion_script_file_name = psprintf("%sdelete_old_cluster.%s",
										  SCRIPT_PREFIX, SCRIPT_EXT);

	/*
	 * Some users (oddly) create tablespaces inside the cluster data
	 * directory.  We can't create a proper old cluster delete script in that
	 * case.
	 */
	strlcpy(old_cluster_pgdata, old_cluster.pgdata, MAXPGPATH);
	canonicalize_path(old_cluster_pgdata);
	for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
	{
		char		old_tablespace_dir[MAXPGPATH];

		strlcpy(old_tablespace_dir, os_info.old_tablespaces[tblnum], MAXPGPATH);
		canonicalize_path(old_tablespace_dir);
		if (path_is_prefix_of_path(old_cluster_pgdata, old_tablespace_dir))
		{
			/* Unlink file in case it is left over from a previous run. */
			unlink(*deletion_script_file_name);
			pg_free(*deletion_script_file_name);
			*deletion_script_file_name = NULL;
			return;
		}
	}

	prep_status("Creating script to delete old cluster");

	if ((script = fopen_priv(*deletion_script_file_name, "w")) == NULL)
		pg_fatal("Could not open file \"%s\": %s\n",
				 *deletion_script_file_name, getErrorText(errno));

#ifndef WIN32
	/* add shebang header */
	fprintf(script, "#!/bin/sh\n\n");
#endif

	/* delete old cluster's default tablespace */
	fprintf(script, RMDIR_CMD " \"%s\"\n", fix_path_separator(old_cluster.pgdata));

	/* delete old cluster's alternate tablespaces */
	for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
	{
		/*
		 * Do the old cluster's per-database directories share a directory
		 * with a new version-specific tablespace?
		 */
		if (strlen(old_cluster.tablespace_suffix) == 0)
		{
			/* delete per-database directories */
			int			dbnum;

			fprintf(script, "\n");
			/* remove PG_VERSION? */
			if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804)
				fprintf(script, RM_CMD " %s%cPG_VERSION\n",
						fix_path_separator(os_info.old_tablespaces[tblnum]),
						PATH_SEPARATOR);

			for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
				fprintf(script, RMDIR_CMD " \"%s%c%d\"\n",
						fix_path_separator(os_info.old_tablespaces[tblnum]),
						PATH_SEPARATOR, old_cluster.dbarr.dbs[dbnum].db_oid);
		}
		else
		{
			char	   *suffix_path = pg_strdup(old_cluster.tablespace_suffix);

			/*
			 * Simply delete the tablespace directory, which might be ".old"
			 * or a version-specific subdirectory.
			 */
			fprintf(script, RMDIR_CMD " \"%s%s\"\n",
					fix_path_separator(os_info.old_tablespaces[tblnum]),
					fix_path_separator(suffix_path));
			pfree(suffix_path);
		}
	}

	fclose(script);

#ifndef WIN32
	if (chmod(*deletion_script_file_name, S_IRWXU) != 0)
		pg_fatal("Could not add execute permission to file \"%s\": %s\n",
				 *deletion_script_file_name, getErrorText(errno));
#endif

	check_ok();
}
Example #3
0
BOOL CCmdLineParser::_DoParser(LPCTSTR lpCmdLine,
							  LPCTSTR lpCurDir, 
							  LPCTSTR lpEnvVars, 
							  LPCTSTR lpExtNames,
							  LPCTSTR lpParentPath
							  )
{
	BOOL	bResult = FALSE;
	LPCTSTR	lpCmdBegin = NULL;
	BOOL	bQuotation = FALSE;
	
	m_szCmd[0] = 0;

    if (m_lpParam != NULL)
    {
        delete[] m_lpParam;
        m_lpParam = NULL;
    }

	if ( is_empty_str(lpCmdLine) ) return FALSE;
	if ( is_empty_str(lpParentPath) ) lpParentPath = NULL;
	if ( is_empty_str(lpCurDir) ) lpCurDir = NULL;
	if ( is_empty_str(lpEnvVars) ) lpEnvVars = NULL;
	if ( is_empty_str(lpExtNames) ) lpExtNames = NULL;

	/*
	1、The directory from which the application loaded. 
	2、The current directory for the parent process. 
	3、The 32-bit Windows system directory. Use the GetSystemDirectory function to get the path of this directory.
	Windows Me/98/95:  The Windows system directory. Use the GetSystemDirectory function to get the path of this directory.
	4、The 16-bit Windows system directory. There is no function that obtains the path of this directory, but it is searched. The name of this directory is System. 
	5、The Windows directory. Use the GetWindowsDirectory function to get the path of this directory. 
	6、The directories that are listed in the PATH environment variable. Note that this function does not search the per-application path specified by the App Paths registry key. To include this per-application path in the search sequence, use the ShellExecute function. 
	*/
	lpCmdBegin = lpCmdLine;
	if ( *lpCmdBegin == _T('\"') )
	{
		lpCmdBegin = skip_quotation(lpCmdBegin);
		bQuotation = TRUE;
	}

	if ( (m_dwFlag & DEF_EXT_NAME) && lpExtNames == NULL )
	{
		// 默认扩展名
		lpExtNames = _T("bat;cmd;exe;pif");
	}

	if ( _tcsnicmp(lpCmdBegin, _T("\\??\\"), 4) == 0 )
	{
		lpCmdBegin += 4;
	}

	if ( _tcsnicmp(lpCmdBegin, _T("file://"), 7) == 0 )
	{
		if ( lpCmdBegin[8] == _T(':') )
		{
			lpCmdBegin += 7;
		}
	}

	/*
		宏展开
	*/
	if ( *lpCmdBegin == _T('%') )
	{
		LPTSTR lpCmdBuff = ExpandMacro(lpCmdBegin);
		if ( lpCmdBuff != NULL )
		{
			bResult = ExpandAbsolute(lpCmdBuff, lpExtNames, bQuotation);

			delete lpCmdBuff;
			lpCmdBuff = NULL;
		}
		
		goto _Exit;
	}
	
	/*
		绝对路径展开
	*/
	if ( is_absolute_path(lpCmdBegin) )
	{
		bResult = ExpandAbsolute(lpCmdBegin, lpExtNames, bQuotation);
		goto _Exit;
	}

	/*
		相对路径转换
	*/
	if ( (lpCurDir != NULL) || (m_dwFlag & DEF_CUR_DIR) )
	{
		LPTSTR lpCmdBuff = ExpandRelative(lpCmdBegin, lpCurDir, bQuotation);
		if ( lpCmdBuff != NULL )
		{
			bResult = ExpandAbsolute(lpCmdBuff, lpExtNames, bQuotation);

			delete lpCmdBuff;
			lpCmdBuff = NULL;

			if ( bResult )
			{
				goto _Exit;
			}
		}
	}
	
	/*
		二次宏展开
	*/
	if ( m_dwFlag & DEF_ENV_VAR )
	{
		LPTSTR lpCmdBuff = ExpandMacro2(lpCmdBegin);
		if ( lpCmdBuff != NULL )
		{
			bResult = ExpandAbsolute(lpCmdBuff, lpExtNames, bQuotation);
			
			delete lpCmdBuff;
			lpCmdBuff = NULL;

			if ( bResult )
			{
				goto _Exit;
			}
		}
	}

	/*
		在父程序目录下查找
	*/
	if ( lpParentPath != NULL )
	{
		if ( MyGetLongPathName(lpParentPath, m_szCmd, MAX_PATH) != 0 )
		{
			LPTSTR lpFileName = (LPTSTR)get_file_name(m_szCmd);
			*lpFileName = 0;
			DWORD dwLen2 = (DWORD)_tcslen(m_szCmd);

			LPTSTR lpCmdBuff = new TCHAR[dwLen2 + _tcslen(lpCmdBegin) + 2];
			if ( lpCmdBuff != NULL )
			{
				_tcscpy(lpCmdBuff, m_szCmd);
				if ( lpCmdBuff[dwLen2 - 1] != _T('\\') )
				{
					lpCmdBuff[dwLen2++] = _T('\\');
				}
				_tcscpy(lpCmdBuff + dwLen2, lpCmdBegin);
				//MessageBox(0, lpCmdBuff, NULL, 0);

				bResult = ExpandAbsolute(lpCmdBuff, lpExtNames, bQuotation);
				
				delete lpCmdBuff;
				lpCmdBuff = NULL;

				if ( bResult )
				{
					goto _Exit;
				}
			}
		}
	}

	/*
	环境变量中查找
	*/
	if ( lpEnvVars != NULL || (m_dwFlag & DEF_ENV_VAR) )
	{
		LPCTSTR lpCmdEnd = NULL;

		if ( bQuotation )
		{
			lpCmdEnd = _tcschr(lpCmdBegin, _T('\"'));
			if ( lpCmdEnd == NULL )
			{
				goto _Exit;
			}
		}
		else
		{
			lpCmdEnd = skip_no_blank(lpCmdBegin);
		}

		if ( !fix_path_separator(m_szCmd, MAX_PATH, lpCmdBegin, lpCmdEnd - lpCmdBegin) )
		{
			goto _Exit;
		}

		if ( !is_relative_path(m_szCmd) )
		{
			bResult = ExpandEnvVars(m_szCmd, lpEnvVars, lpExtNames);
			if ( bResult )
			{
				ExpandParam(lpCmdEnd);
			}
		}
	}

_Exit:
	if ( !bResult )
	{
		m_szCmd[0] = 0;
	}

	return bResult;
}
Example #4
0
LPTSTR CCmdLineParser::ExpandRelative(LPCTSTR lpCmdBegin, LPCTSTR lpCurDir, BOOL bQuotation)
{
	LPTSTR	lpFileNameOffset, lpFileNameOffsetEnd;
	LPTSTR	lpDirOffset;
	BOOL	bJmpDir;
	TCHAR	szTempBuff[MAX_PATH + 10];
	LPCTSTR lpCmdEnd = NULL, lpCmdEnd_Last = NULL;
	LPTSTR	lpCmdBuff = NULL;

	if ( lpCurDir == NULL )
	{
		GetCurrentDirectory(MAX_PATH, m_szCmd);
	}
	else
	{
		fix_path_separator(m_szCmd, MAX_PATH, lpCurDir);
	}

	lpFileNameOffset = m_szCmd + _tcslen(m_szCmd);
	lpFileNameOffsetEnd = m_szCmd + MAX_PATH;
	if ( lpFileNameOffset[-1] == _T('\\') )
	{
		*--lpFileNameOffset = 0;
	}

	if ( bQuotation )
	{
		lpCmdEnd = _tcschr(lpCmdBegin, _T('\"'));
		if ( lpCmdEnd == NULL )
		{
			goto _Failed_Exit;
		}
	}
	else
	{
		lpCmdEnd = skip_no_blank(lpCmdBegin);
		if ( lpCmdEnd == lpCmdBegin )
		{
			lpCmdEnd = skip_blank(lpCmdBegin);
		}
	}

	if ( !fix_path_separator(szTempBuff, MAX_PATH, lpCmdBegin, lpCmdEnd - lpCmdBegin) )
	{
		goto _Failed_Exit;
	}

	// ../../a.exe
	// ./a.exe
	// ./../a.exe
	// a/../a.exe
	// ../a/2.exe
	bJmpDir = TRUE;
	lpDirOffset = szTempBuff;
	
	while ( *lpDirOffset != 0 )
	{
		if ( lpFileNameOffset >= lpFileNameOffsetEnd )
		{
			// 缓冲区不够
			goto _Failed_Exit;
		}

		if ( lpDirOffset[0] == _T('.') )
		{
			if ( lpDirOffset[1] == _T('.') && lpDirOffset[2] == _T('\\') )
			{
				lpFileNameOffset = (LPTSTR)re_strchr(m_szCmd, lpFileNameOffset, _T('\\'));
				if ( lpFileNameOffset == NULL )
				{
					// Bad Command Line
					goto _Failed_Exit;
				}

				*lpFileNameOffset = 0;
				lpDirOffset += 3;

				bJmpDir = TRUE;
				continue;
			}
			else if ( is_path_separator_char(lpDirOffset[1]) )
			{
				lpDirOffset += 2;

				bJmpDir = TRUE;
				continue;
			}
		}
		else if ( lpDirOffset[0] == _T('\\') )
		{
			lpDirOffset++;

			bJmpDir = TRUE;
			continue;
		}

		if ( bJmpDir )
		{
			bJmpDir = FALSE;
			*lpFileNameOffset++ = _T('\\');
		}

		*lpFileNameOffset++ = *lpDirOffset++;
		*lpFileNameOffset = 0;
	}

	if ( !bJmpDir )
	{
		size_t dwLen1 = _tcslen(m_szCmd);

		lpCmdBuff = new TCHAR[dwLen1 +  _tcslen(lpCmdEnd) + 2];
		if ( lpCmdBuff != NULL )
		{
			_tcscpy(lpCmdBuff, m_szCmd);
			if ( lpCmdBuff[dwLen1 - 1] == _T('\\') )
			{
				dwLen1--;
			}

			_tcscpy(lpCmdBuff + dwLen1, lpCmdEnd);
		}
	}

_Failed_Exit:
	return lpCmdBuff;
}
Example #5
0
BOOL CCmdLineParser::ExpandAbsolute(LPCTSTR lpCmdBegin, LPCTSTR lpExtNames, BOOL bQuotation)
{
	BOOL bResult = FALSE;

	if ( is_absolute_path(lpCmdBegin) )
	{
		//
		// 绝对路径
		// 支持不加引号的命令行如 C:\Program Files\ASUS\Power4 Gear\BatteryLife.exe 1
		//
		TCHAR szCmdBuff[MAX_PATH + 10];
		LPCTSTR lpCmdEnd = NULL, lpCmdEnd_Last = NULL;

		m_szCmd[0] = 0;

		if ( bQuotation )
		{
			lpCmdEnd = _tcschr(lpCmdBegin, _T('\"'));
			if ( fix_path_separator(szCmdBuff, MAX_PATH, lpCmdBegin, lpCmdEnd - lpCmdBegin) &&
				ExpandExtNames(szCmdBuff, lpExtNames)
				)
			{
				MyGetLongPathName(szCmdBuff, m_szCmd, MAX_PATH);

				lpCmdEnd = skip_quotation(lpCmdEnd);
				lpCmdEnd = skip_blank(lpCmdEnd);
			}
		}
		else
		{
			lpCmdEnd = skip_no_blank(lpCmdBegin);
			lpCmdEnd_Last = lpCmdEnd;

			while ( TRUE )
			{
				if ( !fix_path_separator(szCmdBuff, MAX_PATH, lpCmdBegin, lpCmdEnd - lpCmdBegin) )
				{
					break;
				}

				if ( ExpandExtNames(szCmdBuff, lpExtNames) )
				{
					lpCmdEnd_Last = lpCmdEnd;
					MyGetLongPathName(szCmdBuff, m_szCmd, MAX_PATH);
				}

				lpCmdEnd = skip_blank(lpCmdEnd);
				if ( *lpCmdEnd == 0 ) 
				{
					break;
				}

				lpCmdEnd = skip_no_blank(lpCmdEnd);
			}

			lpCmdEnd = lpCmdEnd_Last;
		}

		if ( m_szCmd[0] != 0 )
		{
			ExpandParam(lpCmdEnd);
			bResult = TRUE;
		}
	}

	return bResult;
}
Example #6
0
BOOL CCmdLineParser::ExpandEnvVars(LPTSTR lpFilePath, LPCTSTR lpEnvVars, LPCTSTR lpExtNames)
{
	TCHAR szTempBuff[MAX_PATH + 10];

	GetSystemDirectory(szTempBuff, MAX_PATH);
	append_file_name(szTempBuff, lpFilePath);
	if ( ExpandExtNames(szTempBuff, lpExtNames) )
	{
		MyGetLongPathName(szTempBuff, lpFilePath, MAX_PATH);
		return TRUE;
	}

	GetSystemWindowsDirectory(szTempBuff, MAX_PATH);
	append_file_name(szTempBuff, lpFilePath);
	if ( ExpandExtNames(szTempBuff, lpExtNames) )
	{
		MyGetLongPathName(szTempBuff, lpFilePath, MAX_PATH);
		return TRUE;
	}

	if ( (m_dwFlag & DEF_ENV_VAR) && lpEnvVars == NULL )
	{
		lpEnvVars = GetDefaultPathVar();
	}

	if ( lpEnvVars != NULL )
	{
		//
		// 展开环境变量
		//
		LPCTSTR lpVar = lpEnvVars;
		while ( !is_empty_str(lpVar)  )
		{
			size_t nVarLen;
			TCHAR c;

			while ( (c = *lpVar) != 0 )
			{
				if ( c != _T(';') && c != _T(' ') && c != _T('\t') )
				{
					break;
				}

				lpVar++;
			}

			if ( c == 0 )
			{
				break;
			}

			LPCTSTR lpNextVar = _tcschr(lpVar, _T(';'));
			if ( lpNextVar != NULL )
			{
				nVarLen = lpNextVar - lpVar;
				lpNextVar++;
			}
			else
			{
				nVarLen = _tcslen(lpVar);
			}

			if ( fix_path_separator(szTempBuff, MAX_PATH, lpVar, nVarLen) &&
				append_file_name(szTempBuff, lpFilePath) &&
				ExpandExtNames(szTempBuff, lpExtNames) )
			{
				MyGetLongPathName(szTempBuff, lpFilePath, MAX_PATH);
				return TRUE;
			}

			lpVar = lpNextVar;
		}
	}

	return FALSE;
}
Example #7
0
/*
 * create_script_for_old_cluster_deletion()
 *
 *	This is particularly useful for tablespace deletion.
 */
void
create_script_for_old_cluster_deletion(char **deletion_script_file_name)
{
	FILE	   *script = NULL;
	int			tblnum;

	*deletion_script_file_name = pg_malloc(MAXPGPATH);

	prep_status("Creating script to delete old cluster");

	snprintf(*deletion_script_file_name, MAXPGPATH, "delete_old_cluster.%s",
			 SCRIPT_EXT);

	if ((script = fopen_priv(*deletion_script_file_name, "w")) == NULL)
		pg_log(PG_FATAL, "Could not open file \"%s\": %s\n",
			   *deletion_script_file_name, getErrorText(errno));

#ifndef WIN32
	/* add shebang header */
	fprintf(script, "#!/bin/sh\n\n");
#endif

	/* delete old cluster's default tablespace */
	fprintf(script, RMDIR_CMD " %s\n", fix_path_separator(old_cluster.pgdata));

	/* delete old cluster's alternate tablespaces */
	for (tblnum = 0; tblnum < os_info.num_tablespaces; tblnum++)
	{
		/*
		 * Do the old cluster's per-database directories share a directory
		 * with a new version-specific tablespace?
		 */
		if (strlen(old_cluster.tablespace_suffix) == 0)
		{
			/* delete per-database directories */
			int			dbnum;

			fprintf(script, "\n");
			/* remove PG_VERSION? */
			if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804)
				fprintf(script, RM_CMD " %s%s%cPG_VERSION\n",
						fix_path_separator(os_info.tablespaces[tblnum]), 
						fix_path_separator(old_cluster.tablespace_suffix),
						PATH_SEPARATOR);

			for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
			{
				fprintf(script, RMDIR_CMD " %s%s%c%d\n",
						fix_path_separator(os_info.tablespaces[tblnum]),
						fix_path_separator(old_cluster.tablespace_suffix),
						PATH_SEPARATOR, old_cluster.dbarr.dbs[dbnum].db_oid);
			}
		}
		else

			/*
			 * Simply delete the tablespace directory, which might be ".old"
			 * or a version-specific subdirectory.
			 */
			fprintf(script, RMDIR_CMD " %s%s\n",
					fix_path_separator(os_info.tablespaces[tblnum]), 
					fix_path_separator(old_cluster.tablespace_suffix));
	}

	fclose(script);

#ifndef WIN32
	if (chmod(*deletion_script_file_name, S_IRWXU) != 0)
		pg_log(PG_FATAL, "Could not add execute permission to file \"%s\": %s\n",
			   *deletion_script_file_name, getErrorText(errno));
#endif

	check_ok();
}