blob: d554cbb0ca9d32c0cb9b467a3b75dbb6081bb7cd [file] [log] [blame]
Dennis Syrovatsky110e7ee2005-10-19 07:01:41 +00001/* Copyright (C) 2005 TightVNC Team. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 *
18 * TightVNC distribution homepage on the Web: http://www.tightvnc.com/
19 *
20 */
21
22// -=- FolderManager.cxx
23
24#include <rfb_win32/FolderManager.h>
25
26using namespace rfb;
27using namespace rfb::win32;
28
29FolderManager::FolderManager()
30{
31
32}
33
34FolderManager::~FolderManager()
35{
36
37}
38
39bool
40FolderManager::createDir(char *pFullPath)
41{
Dennis Syrovatsky514c3072005-10-19 07:34:40 +000042 if (CreateDirectory(pFullPath, NULL) == 0) return false;
43
44 return true;
Dennis Syrovatsky110e7ee2005-10-19 07:01:41 +000045}
46
47bool
48FolderManager::renameDir(char *pOldName, char *pNewName)
49{
Dennis Syrovatsky514c3072005-10-19 07:34:40 +000050 if (MoveFile(pOldName, pNewName)) return true;
51
Dennis Syrovatsky110e7ee2005-10-19 07:01:41 +000052 return false;
53}
54
55bool
56FolderManager::deleteDir(char *pFullPath)
57{
Dennis Syrovatsky880318b2005-10-19 10:05:51 +000058 FileInfo fileInfo;
59 fileInfo.add(pFullPath, 0, 0, FT_ATTR_DIR);
60
61 unsigned int num = fileInfo.getNumEntries();
62 unsigned int last = num - 1;
63
64 while (num > 0) {
65 if (fileInfo.getFlagsAt(last) & FT_ATTR_DIR) {
66 if (RemoveDirectory(fileInfo.getNameAt(last)) == 0) {
67 if (GetLastError() == ERROR_DIR_NOT_EMPTY) {
68 if (!getFolderInfoWithPrefix(fileInfo.getNameAt(last), &fileInfo)) {
69 fileInfo.free();
70 return false;
71 }
72 }
73 } else {
74 fileInfo.deleteAt(last);
75 }
76 } else {
77 if (DeleteFile(fileInfo.getNameAt(last)) == 0) {
78 fileInfo.free();
79 return false;
80 } else {
81 fileInfo.deleteAt(last);
82 }
83 }
84
85 num = fileInfo.getNumEntries();
86 last = num - 1;
87 }
88
89 return true;
90}
91
92bool
93FolderManager::getFolderInfoWithPrefix(char *pPrefix, FileInfo *pFileInfo)
94{
95 char prefix[FT_FILENAME_SIZE];
96 strcpy(prefix, pPrefix);
97
98 FileInfo tmpFileInfo;
99 if (!getFolderInfo(prefix, &tmpFileInfo, 0)) {
100 tmpFileInfo.free();
101 return false;
102 } else {
103 char buf[FT_FILENAME_SIZE];
104 for (unsigned int i = 0; i < tmpFileInfo.getNumEntries(); i++) {
105 sprintf(buf, "%s\\%s", prefix, tmpFileInfo.getNameAt(i));
106 pFileInfo->add(buf, tmpFileInfo.getSizeAt(i), tmpFileInfo.getDataAt(i), tmpFileInfo.getFlagsAt(i));
107 }
108 }
109 tmpFileInfo.free();
110 return true;
Dennis Syrovatsky110e7ee2005-10-19 07:01:41 +0000111}
112
113bool
Dennis Syrovatsky84ea20f2005-10-19 07:46:31 +0000114FolderManager::getFolderInfo(char *pPath, FileInfo *pFileInfo, unsigned int dirOnly)
Dennis Syrovatsky110e7ee2005-10-19 07:01:41 +0000115{
Dennis Syrovatsky84ea20f2005-10-19 07:46:31 +0000116 if (strlen(pPath) == 0) return getDrivesInfo(pFileInfo);
117
118 char path[FT_FILENAME_SIZE];
119 sprintf(path, "%s\\*", pPath);
120
121 WIN32_FIND_DATA FindFileData;
122 SetErrorMode(SEM_FAILCRITICALERRORS);
123 HANDLE handle = FindFirstFile(path, &FindFileData);
124 DWORD lastError = GetLastError();
125 SetErrorMode(0);
126
127 if (handle != INVALID_HANDLE_VALUE) {
128 do {
129 if (strcmp(FindFileData.cFileName, ".") != 0 &&
130 strcmp(FindFileData.cFileName, "..") != 0) {
131 LARGE_INTEGER li;
132 li.LowPart = FindFileData.ftLastWriteTime.dwLowDateTime;
133 li.HighPart = FindFileData.ftLastWriteTime.dwHighDateTime;
134 li.QuadPart = (li.QuadPart - 116444736000000000) / 10000000;
135 if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
Dennis Syrovatsky880318b2005-10-19 10:05:51 +0000136 pFileInfo->add(FindFileData.cFileName, 0, li.LowPart, FT_ATTR_DIR);
Dennis Syrovatsky84ea20f2005-10-19 07:46:31 +0000137 } else {
138 if (!dirOnly)
139 pFileInfo->add(FindFileData.cFileName, FindFileData.nFileSizeLow, li.LowPart, FT_ATTR_FILE);
140 }
141 }
142
143 } while (FindNextFile(handle, &FindFileData));
144 } else {
145 return false;
146 }
147 FindClose(handle);
148 return true;
149}
150
151bool
152FolderManager::getDrivesInfo(FileInfo *pFileInfo)
153{
154 TCHAR szDrivesList[256];
155 if (GetLogicalDriveStrings(255, szDrivesList) == 0)
156 return false;
157
158 int i = 0;
159 while (szDrivesList[i] != '\0') {
160 char *drive = strdup(&szDrivesList[i]);
161 char *backslash = strrchr(drive, '\\');
162 if (backslash != NULL)
163 *backslash = '\0';
Dennis Syrovatsky880318b2005-10-19 10:05:51 +0000164 pFileInfo->add(drive, 0, 0, FT_ATTR_DIR);
Dennis Syrovatsky84ea20f2005-10-19 07:46:31 +0000165 free(drive);
166 i += strcspn(&szDrivesList[i], "\0") + 1;
167 }
168 return true;
Dennis Syrovatsky110e7ee2005-10-19 07:01:41 +0000169}