tools/installer/LocalCopyMethod.cpp
author David Anderson <dvander@alliedmods.net>
Fri Jul 11 06:51:21 2008 +0000 (2008-07-11)
branchrefac-jit
changeset 2192 7602d7fb60d9
parent 2168 c24a6fa957db
child 2289 44045ead61cd
permissions -rw-r--r--
fixed a compat regression, forgot msvc does funky things with overloaded virtual calls
     1 #include "InstallerUtil.h"
     2 #include "LocalCopyMethod.h"
     3 
     4 LocalCopyMethod g_LocalCopier;
     5 
     6 DWORD CALLBACK CopyProgressRoutine(LARGE_INTEGER TotalFileSize,
     7 								   LARGE_INTEGER TotalBytesTransferred,
     8 								   LARGE_INTEGER StreamSize,
     9 								   LARGE_INTEGER StreamBytesTransferred,
    10 								   DWORD dwStreamNumber,
    11 								   DWORD dwCallbackReason,
    12 								   HANDLE hSourceFile,
    13 								   HANDLE hDestinationFile,
    14 								   LPVOID lpData)
    15 {
    16 	ICopyProgress *progress = (ICopyProgress *)lpData;
    17 
    18 	progress->UpdateProgress((size_t)TotalBytesTransferred.QuadPart,
    19 		(size_t)TotalFileSize.QuadPart);
    20 
    21 	return PROGRESS_CONTINUE;
    22 }
    23 
    24 LocalCopyMethod::LocalCopyMethod()
    25 {
    26 	m_pProgress = NULL;
    27 }
    28 
    29 void LocalCopyMethod::SetOutputPath(const TCHAR *path)
    30 {
    31 	UTIL_PathFormat(m_OutputPath,
    32 		sizeof(m_OutputPath) / sizeof(TCHAR),
    33 		_T("%s"),
    34 		path);
    35 
    36 	UTIL_PathFormat(m_CurrentPath,
    37 		sizeof(m_CurrentPath) / sizeof(TCHAR),
    38 		_T("%s"),
    39 		path);
    40 }
    41 
    42 void LocalCopyMethod::TrackProgress(ICopyProgress *pProgress)
    43 {
    44 	m_pProgress = pProgress;
    45 }
    46 
    47 bool LocalCopyMethod::CreateFolder(const TCHAR *name, TCHAR *buffer, size_t maxchars)
    48 {
    49 	TCHAR path[MAX_PATH];
    50 
    51 	UTIL_PathFormat(path,
    52 		sizeof(path) / sizeof(TCHAR),
    53 		_T("%s\\%s"),
    54 		m_CurrentPath,
    55 		name);
    56 
    57 	if (CreateDirectory(path, NULL))
    58 	{
    59 		return true;
    60 	}
    61 
    62 	DWORD error = GetLastError();
    63 	if (error == ERROR_ALREADY_EXISTS)
    64 	{
    65 		return true;
    66 	}
    67 
    68 	GenerateErrorMessage(error, buffer, maxchars);
    69 
    70 	return false;
    71 }
    72 
    73 bool LocalCopyMethod::SetCurrentFolder(const TCHAR *path, TCHAR *buffer, size_t maxchars)
    74 {
    75 	if (path == NULL)
    76 	{
    77 		UTIL_PathFormat(m_CurrentPath,
    78 			sizeof(m_CurrentPath) / sizeof(TCHAR),
    79 			_T("%s"),
    80 			m_OutputPath);
    81 	}
    82 	else
    83 	{
    84 		UTIL_PathFormat(m_CurrentPath,
    85 			sizeof(m_CurrentPath) / sizeof(TCHAR),
    86 			_T("%s\\%s"),
    87 			m_OutputPath,
    88 			path);
    89 	}
    90 
    91 	return true;
    92 }
    93 
    94 bool LocalCopyMethod::SendFile(const TCHAR *path, TCHAR *buffer, size_t maxchars)
    95 {
    96 	const TCHAR *filename = GetFileFromPath(path);
    97 
    98 	if (filename == NULL)
    99 	{
   100 		UTIL_Format(buffer, maxchars, _T("Invalid filename"));
   101 		return false;
   102 	}
   103 
   104 	TCHAR new_path[MAX_PATH];
   105 	UTIL_PathFormat(new_path,
   106 		sizeof(new_path) / sizeof(TCHAR),
   107 		_T("%s\\%s"),
   108 		m_CurrentPath,
   109 		filename);
   110 
   111 	m_bCancelStatus = FALSE;
   112 
   113 	if (m_pProgress != NULL)
   114 	{
   115 		m_pProgress->StartingNewFile(filename);
   116 	}
   117 
   118 	if (CopyFileEx(path,
   119 		new_path,
   120 		m_pProgress ? CopyProgressRoutine : NULL,
   121 		m_pProgress,
   122 		&m_bCancelStatus,
   123 		0) == 0)
   124 	{
   125 		/* Delete the file in case it was a partial copy */
   126 		DeleteFile(new_path);
   127 
   128 		GenerateErrorMessage(GetLastError(), buffer, maxchars);
   129 
   130 		return false;
   131 	}
   132 
   133 	if (m_pProgress != NULL)
   134 	{
   135 		m_pProgress->FileDone(UTIL_GetFileSize(path));
   136 	}
   137 
   138 	return true;
   139 }
   140 
   141 void LocalCopyMethod::CancelCurrentCopy()
   142 {
   143 	m_bCancelStatus = TRUE;
   144 }
   145 
   146 bool LocalCopyMethod::CheckForExistingInstall()
   147 {
   148 	TCHAR path[MAX_PATH];
   149 
   150 	UTIL_PathFormat(path,
   151 		sizeof(path) / sizeof(TCHAR),
   152 		_T("%s\\addons\\sourcemod"),
   153 		m_CurrentPath);
   154 	if (GetFileAttributes(path) == INVALID_FILE_ATTRIBUTES)
   155 	{
   156 		UTIL_PathFormat(path,
   157 			sizeof(path) / sizeof(TCHAR),
   158 			_T("%s\\cfg\\sourcemod"),
   159 			m_CurrentPath);
   160 		if (GetFileAttributes(path) == INVALID_FILE_ATTRIBUTES)
   161 		{
   162 			return false;
   163 		}
   164 	}
   165 
   166 	return true;
   167 }