OSDN Git Service

Fix issue #2046: Folder compare omits unique folders from results if they contain...
[winmerge-jp/winmerge-jp.git] / Src / DirItem.cpp
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /** 
3  * @file  DirItem.cpp
4  *
5  * @brief Implementation for DirItem routines
6  */
7
8 #include "pch.h"
9 #include "DirItem.h"
10 #include "UnicodeString.h"
11 #include "paths.h"
12 #include "TFile.h"
13 #include "DebugNew.h"
14 #include <filesystem>
15
16 /**
17  * @brief Set filename and path for the item.
18  * @param [in] fullpath Full path to file to set to item.
19  */
20 void DirItem::SetFile(const String &fullPath)
21 {
22         String ext, filename2, path2;
23         paths::SplitFilename(fullPath, &path2, &filename2, &ext);
24         filename2 += _T(".");
25         filename2 += ext;
26         filename = filename2;
27         path = path2;
28 }
29
30 /**
31  * @brief Get the full path of the item.
32  * @return fullpath
33  */
34 String DirItem::GetFile() const
35 {
36         return paths::ConcatPath(path.get(), filename.get());
37 }
38
39 /**
40  * @brief Update fileinfo from given file.
41  * This function updates file's information from given item. Function
42  * does not set filename and path.
43  * @param [in] sFilePath Full path to file/directory to update
44  * @return true if information was updated (item was found).
45  */
46 bool DirItem::Update(const String &sFilePath)
47 {
48         bool retVal = false;
49
50         size = DirItem::FILE_SIZE_NONE;
51         flags.reset();
52         mtime = 0;
53
54         if (!sFilePath.empty())
55         {
56                 try
57                 {
58                         TFile file(sFilePath);
59
60                         mtime = file.getLastModified();
61                         // There can be files without modification date.
62                         // Then we must use creation date. Of course we assume
63                         // creation date then exists...
64                         if (mtime == 0)
65                                 mtime = file.created();
66
67                         // No size for directory ( size remains as -1)
68                         if (!file.isDirectory())
69                                 size = file.getSize();
70
71                         flags.attributes = GetFileAttributes(file.wpath().c_str());
72
73                         retVal = true;
74                 }
75                 catch (...)
76                 {
77                 }
78         }
79         return retVal;
80 }
81
82 /**
83  * @brief Update filename from given file.
84  * @param [in] sFilePath Full path to file/directory to update
85  * @return true if information was updated (item was found).
86  */
87 bool DirItem::UpdateFileName(const String& sFilePath)
88 {
89         bool retVal = false;
90         if (!sFilePath.empty())
91         {
92                 try
93                 {
94                         std::filesystem::path canonicalPath = std::filesystem::canonical(sFilePath);
95                         filename = canonicalPath.filename();
96                         retVal = true;
97                 }
98                 catch (...)
99                 {
100                 }
101         }
102         return retVal;
103 }
104
105 /**
106  * @brief Clears FileInfo data.
107  */
108 /*void DirItem::Clear()
109 {
110         ClearPartial();
111         filename.erase();
112         path.erase();
113 }*/
114
115 /**
116  * @brief Clears FileInfo data except path/filename.
117  */
118 void DirItem::ClearPartial()
119 {
120         ctime = 0;
121         mtime = 0;
122         size = DirItem::FILE_SIZE_NONE;
123         flags.reset();
124 }
125
126 /**
127  * @brief Return whether the item is a directory.
128  * @return true if the item is a directory.
129  */
130 bool DirItem::IsDirectory() const
131 {
132         return ((flags.attributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY);
133 }