Skip to content
This repository was archived by the owner on Sep 2, 2021. It is now read-only.

Commit 3d8350f

Browse files
committed
Fix all file IO related issues.
Rename FixFilename to ConvertToNativePath. Add a new function ConvertToUnixPath for converting all selected files to unix path before returning them to JS layer. Remove all calls to FixFilename since we're now using unix path and Windows file IO can handle unix path. Not sure we may still need it for Win XP. Append a '/' to the end of a directory before calling _wstat().
1 parent eeb31e4 commit 3d8350f

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

appshell/appshell_extensions_win.cpp

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
#include <sys/stat.h>
3232

3333
// Forward declarations for functions at the bottom of this file
34-
void FixFilename(ExtensionString& filename);
34+
void ConvertToNativePath(ExtensionString& filename);
35+
void ConvertToUnixPath(ExtensionString& filename);
3536
int ConvertErrnoCode(int errorCode, bool isReading = true);
3637
int ConvertWinErrorCode(int errorCode, bool isReading = true);
3738

@@ -55,8 +56,6 @@ int32 ShowOpenDialog(bool allowMulitpleSelection,
5556
wchar_t szFile[MAX_PATH];
5657
szFile[0] = 0;
5758

58-
FixFilename(initialDirectory);
59-
6059
// TODO (issue #64) - This method should be using IFileDialog instead of the
6160
/* outdated SHGetPathFromIDList and GetOpenFileName.
6261
@@ -128,7 +127,9 @@ int32 ShowOpenDialog(bool allowMulitpleSelection,
128127
// Check for two null terminators, which signal that only one file
129128
// was selected
130129
if (szFile[dir.length() + 1] == '\0') {
131-
selectedFiles->SetString(0, ExtensionString(dir));
130+
ExtensionString filePath(dir);
131+
ConvertToUnixPath(filePath);
132+
selectedFiles->SetString(0, filePath);
132133
} else {
133134
// Multiple files are selected
134135
wchar_t fullPath[MAX_PATH];
@@ -142,8 +143,11 @@ int32 ShowOpenDialog(bool allowMulitpleSelection,
142143

143144
// The filename is relative to the directory that was specified as
144145
// the first string
145-
if (PathCombine(fullPath, dir.c_str(), file.c_str()) != NULL)
146-
selectedFiles->SetString(fileIndex, ExtensionString(fullPath));
146+
if (PathCombine(fullPath, dir.c_str(), file.c_str()) != NULL) {
147+
ExtensionString filePath(fullPath);
148+
ConvertToUnixPath(filePath);
149+
selectedFiles->SetString(fileIndex, filePath);
150+
}
147151

148152
// Go to the start of the next file name
149153
i += file.length() + 1;
@@ -162,9 +166,10 @@ int32 ShowOpenDialog(bool allowMulitpleSelection,
162166

163167
int32 ReadDir(ExtensionString path, CefRefPtr<CefListValue>& directoryContents)
164168
{
165-
FixFilename(path);
169+
if (path.length() && path[path.length() - 1] != '/')
170+
path += '/';
166171

167-
path += L"\\*";
172+
path += '*';
168173

169174
WIN32_FIND_DATA ffd;
170175
HANDLE hFind = FindFirstFile(path.c_str(), &ffd);
@@ -205,8 +210,6 @@ int32 ReadDir(ExtensionString path, CefRefPtr<CefListValue>& directoryContents)
205210

206211
int32 GetFileModificationTime(ExtensionString filename, uint32& modtime, bool& isDir)
207212
{
208-
FixFilename(filename);
209-
210213
DWORD dwAttr = GetFileAttributes(filename.c_str());
211214

212215
if (dwAttr == INVALID_FILE_ATTRIBUTES) {
@@ -215,6 +218,11 @@ int32 GetFileModificationTime(ExtensionString filename, uint32& modtime, bool& i
215218

216219
isDir = ((dwAttr & FILE_ATTRIBUTE_DIRECTORY) != 0);
217220

221+
// Remove trailing "/", if present. _wstat will fail with a "file not found"
222+
// error if a directory has a trailing '/' in the name.
223+
if (filename[filename.length() - 1] == '/')
224+
filename[filename.length() - 1] = 0;
225+
218226
struct _stat buffer;
219227
if(_wstat(filename.c_str(), &buffer) == -1) {
220228
return ConvertErrnoCode(errno);
@@ -227,8 +235,6 @@ int32 GetFileModificationTime(ExtensionString filename, uint32& modtime, bool& i
227235

228236
int32 ReadFile(ExtensionString filename, ExtensionString encoding, std::string& contents)
229237
{
230-
FixFilename(filename);
231-
232238
if (encoding != L"utf8")
233239
return ERR_UNSUPPORTED_ENCODING;
234240

@@ -268,8 +274,6 @@ int32 ReadFile(ExtensionString filename, ExtensionString encoding, std::string&
268274

269275
int32 WriteFile(ExtensionString filename, std::string contents, ExtensionString encoding)
270276
{
271-
FixFilename(filename);
272-
273277
if (encoding != L"utf8")
274278
return ERR_UNSUPPORTED_ENCODING;
275279

@@ -292,8 +296,6 @@ int32 WriteFile(ExtensionString filename, std::string contents, ExtensionString
292296

293297
int32 SetPosixPermissions(ExtensionString filename, int32 mode)
294298
{
295-
FixFilename(filename);
296-
297299
DWORD dwAttr = GetFileAttributes(filename.c_str());
298300

299301
if (dwAttr == INVALID_FILE_ATTRIBUTES)
@@ -315,20 +317,24 @@ int32 SetPosixPermissions(ExtensionString filename, int32 mode)
315317

316318
int32 DeleteFileOrDirectory(ExtensionString filename)
317319
{
318-
FixFilename(filename);
319-
320320
if (!DeleteFile(filename.c_str()))
321321
return ConvertWinErrorCode(GetLastError());
322322

323323
return NO_ERROR;
324324
}
325325

326-
void FixFilename(ExtensionString& filename)
326+
void ConvertToNativePath(ExtensionString& filename)
327327
{
328328
// Convert '/' to '\'
329329
replace(filename.begin(), filename.end(), '/', '\\');
330330
}
331331

332+
void ConvertToUnixPath(ExtensionString& filename)
333+
{
334+
// Convert '\\' to '/'
335+
replace(filename.begin(), filename.end(), '\\', '/');
336+
}
337+
332338
// Maps errors from errno.h to the brackets error codes
333339
// found in brackets_extensions.js
334340
int ConvertErrnoCode(int errorCode, bool isReading)

0 commit comments

Comments
 (0)