-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathManagedFile.cpp
More file actions
49 lines (43 loc) · 1.5 KB
/
ManagedFile.cpp
File metadata and controls
49 lines (43 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "winget/ManagedFile.h"
#include "AppInstallerLogging.h"
namespace AppInstaller::Utility
{
ManagedFile ManagedFile::CreateWriteLockedFile(const std::filesystem::path& path, DWORD desiredAccess, bool deleteOnExit)
{
ManagedFile file;
file.m_fileHandle.reset(CreateFileW(path.c_str(), desiredAccess, FILE_SHARE_READ, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr));
THROW_LAST_ERROR_IF(!file.m_fileHandle);
file.m_filePath = path;
file.m_deleteFileOnExit = deleteOnExit;
return file;
}
ManagedFile ManagedFile::OpenWriteLockedFile(const std::filesystem::path& path, DWORD desiredAccess)
{
ManagedFile file;
file.m_fileHandle.reset(CreateFileW(path.c_str(), desiredAccess, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr));
THROW_LAST_ERROR_IF(!file.m_fileHandle);
file.m_filePath = path;
return file;
}
ManagedFile::~ManagedFile()
{
if (m_deleteFileOnExit)
{
if (m_fileHandle)
{
m_fileHandle.reset();
}
try
{
std::filesystem::remove(m_filePath);
}
catch (...)
{
AICLI_LOG(Core, Info, << "Failed to remove managed file at: " << m_filePath);
}
}
}
}