forked from WinMerge/winmerge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DirItem.cpp
133 lines (119 loc) · 2.64 KB
/
DirItem.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// SPDX-License-Identifier: GPL-2.0-or-later
/**
* @file DirItem.cpp
*
* @brief Implementation for DirItem routines
*/
#include "pch.h"
#include "DirItem.h"
#include "UnicodeString.h"
#include "paths.h"
#include "TFile.h"
#include "DebugNew.h"
#include <filesystem>
/**
* @brief Set filename and path for the item.
* @param [in] fullpath Full path to file to set to item.
*/
void DirItem::SetFile(const String &fullPath)
{
String ext, filename2, path2;
paths::SplitFilename(fullPath, &path2, &filename2, &ext);
filename2 += _T(".");
filename2 += ext;
filename = filename2;
path = path2;
}
/**
* @brief Get the full path of the item.
* @return fullpath
*/
String DirItem::GetFile() const
{
return paths::ConcatPath(path.get(), filename.get());
}
/**
* @brief Update fileinfo from given file.
* This function updates file's information from given item. Function
* does not set filename and path.
* @param [in] sFilePath Full path to file/directory to update
* @return true if information was updated (item was found).
*/
bool DirItem::Update(const String &sFilePath)
{
bool retVal = false;
size = DirItem::FILE_SIZE_NONE;
flags.reset();
mtime = 0;
if (!sFilePath.empty())
{
try
{
TFile file(sFilePath);
mtime = file.getLastModified();
// There can be files without modification date.
// Then we must use creation date. Of course we assume
// creation date then exists...
if (mtime == 0)
mtime = file.created();
// No size for directory ( size remains as -1)
if (!file.isDirectory())
size = file.getSize();
flags.attributes = GetFileAttributes(file.wpath().c_str());
retVal = true;
}
catch (...)
{
}
}
return retVal;
}
/**
* @brief Update filename from given file.
* @param [in] sFilePath Full path to file/directory to update
* @return true if information was updated (item was found).
*/
bool DirItem::UpdateFileName(const String& sFilePath)
{
bool retVal = false;
if (!sFilePath.empty())
{
try
{
std::filesystem::path canonicalPath = std::filesystem::canonical(sFilePath);
filename = canonicalPath.filename();
retVal = true;
}
catch (...)
{
}
}
return retVal;
}
/**
* @brief Clears FileInfo data.
*/
/*void DirItem::Clear()
{
ClearPartial();
filename.erase();
path.erase();
}*/
/**
* @brief Clears FileInfo data except path/filename.
*/
void DirItem::ClearPartial()
{
ctime = 0;
mtime = 0;
size = DirItem::FILE_SIZE_NONE;
flags.reset();
}
/**
* @brief Return whether the item is a directory.
* @return true if the item is a directory.
*/
bool DirItem::IsDirectory() const
{
return ((flags.attributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY);
}