Skip to content

Commit

Permalink
Make more use of smart pointers
Browse files Browse the repository at this point in the history
Signed-off-by: Sven Strickroth <[email protected]>
Signed-off-by: Sup Yut Sum <[email protected]>
  • Loading branch information
csware authored and ch3cooli committed Dec 11, 2015
1 parent 80b55dd commit 1119989
Show file tree
Hide file tree
Showing 21 changed files with 210 additions and 346 deletions.
104 changes: 39 additions & 65 deletions src/Git/Git.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ int CGit::RunAsync(CString cmd, PROCESS_INFORMATION *piOut, HANDLE *hReadOut, HA
}

STARTUPINFO si = { 0 };
PROCESS_INFORMATION pi;
PROCESS_INFORMATION pi = { 0 };
si.cb=sizeof(STARTUPINFO);

if (hErrReadOut)
Expand Down Expand Up @@ -326,7 +326,6 @@ int CGit::RunAsync(CString cmd, PROCESS_INFORMATION *piOut, HANDLE *hReadOut, HA
dwFlags |= DETACHED_PROCESS;

memset(&this->m_CurrentGitPi,0,sizeof(PROCESS_INFORMATION));
memset(&pi, 0, sizeof(PROCESS_INFORMATION));

if (ms_bMsys2Git && cmd.Find(_T("git")) == 0 && cmd.Find(L"git.exe config ") == -1)
{
Expand Down Expand Up @@ -847,19 +846,13 @@ int CGit::GetCurrentBranchFromFile(const CString &sProjectRoot, CString &sBranch

CString sHeadFile = sDotGitPath + _T("HEAD");

FILE *pFile;
_tfopen_s(&pFile, sHeadFile.GetString(), _T("r"));

CAutoFILE pFile = _tfsopen(sHeadFile.GetString(), _T("r"), SH_DENYWR);
if (!pFile)
{
return -1;
}

char s[256] = {0};
fgets(s, sizeof(s), pFile);

fclose(pFile);

const char *pfx = "ref: refs/heads/";
const int len = 16;//strlen(pfx)

Expand Down Expand Up @@ -1134,26 +1127,22 @@ int CGit::RunLogFile(CString cmd, const CString &filename, CString *stdErr)
si.cb=sizeof(STARTUPINFO);
GetStartupInfo(&si);

SECURITY_ATTRIBUTES psa={sizeof(psa),NULL,TRUE};;
SECURITY_ATTRIBUTES psa = {sizeof(psa), nullptr, TRUE};
psa.bInheritHandle=TRUE;

HANDLE hReadErr, hWriteErr;
if (!CreatePipe(&hReadErr, &hWriteErr, &psa, 0))
CAutoGeneralHandle hReadErr, hWriteErr;
if (!CreatePipe(hReadErr.GetPointer(), hWriteErr.GetPointer(), &psa, 0))
{
CString err = CFormatMessageWrapper();
CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": could not open stderr pipe: %s\n"), (LPCTSTR)err.Trim());
return TGIT_GIT_ERROR_OPEN_PIP;
}

HANDLE houtfile=CreateFile(filename,GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,
&psa,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);

if (houtfile == INVALID_HANDLE_VALUE)
CAutoFile houtfile = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, &psa, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (!houtfile)
{
CString err = CFormatMessageWrapper();
CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": could not open stdout pipe: %s\n"), (LPCTSTR)err.Trim());
CloseHandle(hReadErr);
CloseHandle(hWriteErr);
return TGIT_GIT_ERROR_OPEN_PIP;
}

Expand All @@ -1174,24 +1163,23 @@ int CGit::RunLogFile(CString cmd, const CString &filename, CString *stdErr)
CString err = CFormatMessageWrapper();
CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": failed to create Process: %s\n"), (LPCTSTR)err.Trim());
stdErr = &err;
CloseHandle(hReadErr);
CloseHandle(hWriteErr);
CloseHandle(houtfile);
return TGIT_GIT_ERROR_CREATE_PROCESS;
}

CAutoGeneralHandle piThread(pi.hThread);
CAutoGeneralHandle piProcess(pi.hProcess);

BYTE_VECTOR stderrVector;
CGitCall_ByteVector pcall(L"", nullptr, &stderrVector);
HANDLE thread;
ASYNCREADSTDERRTHREADARGS threadArguments;
threadArguments.fileHandle = hReadErr;
threadArguments.pcall = &pcall;
thread = CreateThread(nullptr, 0, AsyncReadStdErrThread, &threadArguments, 0, nullptr);
CAutoGeneralHandle thread = CreateThread(nullptr, 0, AsyncReadStdErrThread, &threadArguments, 0, nullptr);

WaitForSingleObject(pi.hProcess,INFINITE);

CloseHandle(hWriteErr);
CloseHandle(hReadErr);
hWriteErr.CloseHandle();
hReadErr.CloseHandle();

if (thread)
WaitForSingleObject(thread, INFINITE);
Expand All @@ -1209,9 +1197,6 @@ int CGit::RunLogFile(CString cmd, const CString &filename, CString *stdErr)
else
CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": process exited: %d\n"), exitcode);

CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
CloseHandle(houtfile);
return exitcode;
}

Expand Down Expand Up @@ -2486,27 +2471,20 @@ int CGit::GetOneFile(const CString &Refname, const CTGitPath &path, const CStrin
if (git_tree_entry_to_object((git_object**)blob.GetPointer(), repo, entry))
return -1;

FILE *file = nullptr;
_tfopen_s(&file, outputfile, _T("wb"));
CAutoFILE file = _tfsopen(outputfile, _T("wb"), SH_DENYWR);
if (file == nullptr)
{
giterr_set_str(GITERR_NONE, "Could not create file.");
return -1;
}
CAutoBuf buf;
if (git_blob_filtered_content(buf, blob, CUnicodeUtils::GetUTF8(path.GetGitPathString()), 0))
{
fclose(file);
return -1;
}
if (fwrite(buf->ptr, sizeof(char), buf->size, file) != buf->size)
{
giterr_set_str(GITERR_OS, "Could not write to file.");
fclose(file);

return -1;
}
fclose(file);

return 0;
}
Expand Down Expand Up @@ -3024,13 +3002,10 @@ int CGit::GetUnifiedDiff(const CTGitPath& path, const git_revnum_t& rev1, const
{
if (UsingLibGit2(GIT_CMD_DIFF))
{
FILE *file = nullptr;
_tfopen_s(&file, patchfile, _T("w"));
if (file == nullptr)
CAutoFILE file = _tfsopen(patchfile, _T("w"), SH_DENYRW);
if (!file)
return -1;
int ret = GetUnifiedDiffLibGit2(path, rev1, rev2, UnifiedDiffStatToFile, UnifiedDiffToFile, file, bMerge);
fclose(file);
return ret;
return GetUnifiedDiffLibGit2(path, rev1, rev2, UnifiedDiffStatToFile, UnifiedDiffToFile, file, bMerge);
}
else
{
Expand Down Expand Up @@ -3169,32 +3144,31 @@ int CGit::DeleteRef(const CString& reference)

bool CGit::LoadTextFile(const CString &filename, CString &msg)
{
if (PathFileExists(filename))
if (!PathFileExists(filename))
return false;

CAutoFILE pFile = _tfsopen(filename, _T("r"), SH_DENYWR);
if (!pFile)
{
FILE *pFile = nullptr;
_tfopen_s(&pFile, filename, _T("r"));
if (pFile)
{
CStringA str;
do
{
char s[8196] = { 0 };
int read = (int)fread(s, sizeof(char), sizeof(s), pFile);
if (read == 0)
break;
str += CStringA(s, read);
} while (true);
fclose(pFile);
msg = CUnicodeUtils::GetUnicode(str);
msg.Replace(_T("\r\n"), _T("\n"));
msg.TrimRight(_T("\n"));
msg += _T("\n");
}
else
::MessageBox(nullptr, _T("Could not open ") + filename, _T("TortoiseGit"), MB_ICONERROR);
::MessageBox(nullptr, _T("Could not open ") + filename, _T("TortoiseGit"), MB_ICONERROR);
return true; // load no further files
}
return false;

CStringA str;
do
{
char s[8196] = { 0 };
int read = (int)fread(s, sizeof(char), sizeof(s), pFile);
if (read == 0)
break;
str += CStringA(s, read);
} while (true);
msg = CUnicodeUtils::GetUnicode(str);
msg.Replace(_T("\r\n"), _T("\n"));
msg.TrimRight(_T("\n"));
msg += _T("\n");

return true; // load no further files
}

int CGit::GetWorkingTreeChanges(CTGitPathList& result, bool amend, CTGitPathList* filterlist)
Expand Down
4 changes: 2 additions & 2 deletions src/Git/GitAdminDir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "UnicodeUtils.h"
#include "GitAdminDir.h"
#include "Git.h"
#include "SmartHandle.h"

GitAdminDir::GitAdminDir()
{
Expand Down Expand Up @@ -173,15 +174,14 @@ bool GitAdminDir::GetAdminDirPath(const CString &projectTopDir, CString& adminDi

CString GitAdminDir::ReadGitLink(const CString& topDir, const CString& dotGitPath)
{
FILE* pFile = _tfsopen(dotGitPath, _T("r"), SH_DENYWR);
CAutoFILE pFile = _tfsopen(dotGitPath, _T("r"), SH_DENYWR);

if (!pFile)
return _T("");

int size = 65536;
std::unique_ptr<char[]> buffer(new char[size]);
int length = (int)fread(buffer.get(), sizeof(char), size, pFile);
fclose(pFile);
CStringA gitPathA(buffer.get(), length);
if (length < 8 || gitPathA.Left(8) != "gitdir: ")
return _T("");
Expand Down
8 changes: 1 addition & 7 deletions src/Git/GitIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,9 @@ int CGitIndexList::ReadIndex(CString dgitdir)
{
this->clear();

m_critRepoSec.Lock();
CAutoLocker lock(m_critRepoSec);
if (repository.Open(dgitdir))
{
m_critRepoSec.Unlock();
return -1;
}

// add config files
CAutoConfig config(true);
Expand All @@ -97,7 +94,6 @@ int CGitIndexList::ReadIndex(CString dgitdir)
if (git_repository_index(index.GetPointer(), repository))
{
repository.Free();
m_critRepoSec.Unlock();
return -1;
}

Expand All @@ -122,8 +118,6 @@ int CGitIndexList::ReadIndex(CString dgitdir)
CGit::GetFileModifyTime(dgitdir + _T("index"), &this->m_LastModifyTime);
std::sort(this->begin(), this->end(), SortIndex);

m_critRepoSec.Unlock();

return 0;
}

Expand Down
12 changes: 2 additions & 10 deletions src/TGitCache/GITStatusCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ void CGitStatusCache::Create()
#define LOADVALUEFROMFILE(x) if (fread(&x, sizeof(x), 1, pFile)!=1) goto exit;
#define LOADVALUEFROMFILE2(x) if (fread(&x, sizeof(x), 1, pFile)!=1) goto error;
unsigned int value = (unsigned int)-1;
FILE * pFile = NULL;
// find the location of the cache
CString path = CPathUtils::GetLocalAppDataDirectory();
CString path2;
Expand All @@ -78,7 +77,7 @@ void CGitStatusCache::Create()
DeleteFile(path2);
CopyFile(path, path2, FALSE);
DeleteFile(path);
pFile = _tfsopen(path2, _T("rb"), _SH_DENYNO);
CAutoFILE pFile = _tfsopen(path2, _T("rb"), _SH_DENYWR);
if (pFile)
{
try
Expand Down Expand Up @@ -135,15 +134,11 @@ void CGitStatusCache::Create()
}
}
exit:
if (pFile)
fclose(pFile);
DeleteFile(path2);
m_pInstance->watcher.ClearInfoMap();
CTraceToOutputDebugString::Instance()(__FUNCTION__ ": cache loaded from disk successfully!\n");
return;
error:
if (pFile)
fclose(pFile);
DeleteFile(path2);
m_pInstance->watcher.ClearInfoMap();
Destroy();
Expand All @@ -159,13 +154,12 @@ bool CGitStatusCache::SaveCache()
#define WRITEVALUETOFILE(x) if (fwrite(&x, sizeof(x), 1, pFile)!=1) goto error;
unsigned int value = 0;
// save the cache to disk
FILE * pFile = NULL;
// find a location to write the cache to
CString path = CPathUtils::GetLocalAppDataDirectory();
if (!path.IsEmpty())
{
path += STATUSCACHEFILENAME;
_tfopen_s(&pFile, path, _T("wb"));
CAutoFILE pFile = _tfsopen(path, _T("wb"), SH_DENYRW);
if (pFile)
{
value = CACHEDISKVERSION;
Expand All @@ -191,13 +185,11 @@ bool CGitStatusCache::SaveCache()
goto error;
}
}
fclose(pFile);
}
}
CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(": cache saved to disk at %s\n"), (LPCTSTR)path);
return true;
error:
fclose(pFile);
Destroy();
DeleteFile(path);
return false;
Expand Down
1 change: 1 addition & 0 deletions src/TGitCache/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ using namespace ATL;
#pragma warning(pop)
#include "SmartLibgit2Ref.h"

#include "scope_exit_noexcept.h"
#include "DebugOutput.h"

typedef CComCritSecLock<CComAutoCriticalSection> AutoLocker;
Expand Down
2 changes: 1 addition & 1 deletion src/TortoiseGitBlame/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@

#include <afxcontrolbars.h> // MFC support for ribbons and control bars

#include "scope_exit_noexcept.h"
#include "DebugOutput.h"


#include <string>
#include <vector>
#include <map>
Expand Down
1 change: 1 addition & 0 deletions src/TortoiseMerge/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@

#define XMESSAGEBOX_APPREGPATH "Software\\TortoiseGitMerge\\"

#include "scope_exit_noexcept.h"
#include "ProfilingInfo.h"
#include "CrashReport.h"

Expand Down
4 changes: 1 addition & 3 deletions src/TortoiseProc/AppUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3648,12 +3648,12 @@ int CAppUtils::Git2CertificateCheck(git_cert* base_cert, int /*valid*/, const ch
return 0;

PCCERT_CONTEXT pServerCert = CertCreateCertificateContext(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, (BYTE*)cert->data, (DWORD)cert->len);
SCOPE_EXIT { CertFreeCertificateContext(pServerCert); };

DWORD verificationError = VerifyServerCertificate(pServerCert, CUnicodeUtils::GetUnicode(host).GetBuffer(), 0);
if (!verificationError)
{
last_accepted_cert.set(cert);
CertFreeCertificateContext(pServerCert);
return 0;
}

Expand All @@ -3663,8 +3663,6 @@ int CAppUtils::Git2CertificateCheck(git_cert* base_cert, int /*valid*/, const ch
CString issuer;
CertGetNameString(pServerCert, CERT_NAME_SIMPLE_DISPLAY_TYPE, CERT_NAME_ISSUER_FLAG, nullptr, CStrBuf(issuer, 128), 128);

CertFreeCertificateContext(pServerCert);

CCheckCertificateDlg dlg;
dlg.cert = cert;
dlg.m_sCertificateCN = servernameInCert;
Expand Down
Loading

0 comments on commit 1119989

Please sign in to comment.