Skip to content

Commit

Permalink
Allow NUL and \\.\NUL in paths specified as command line arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
sdottaka committed Oct 4, 2023
1 parent af1a695 commit 6c5dec2
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Src/DiffContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ void CDiffContext::GetComparePaths(const DIFFITEM &di, PathContext & tFiles) con
}
else
{
tFiles.SetPath(nIndex, _T("NUL"), false);
tFiles.SetPath(nIndex, paths::NATIVE_NULL_DEVICE_NAME, false);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Src/DiffWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1648,12 +1648,12 @@ void CDiffWrapper::WritePatchFile(struct change * script, file_data * inf)
return;
}

if (strcmp(inf[0].name, "NUL") == 0)
if (paths::IsNullDeviceName(ucr::toTString(inf[0].name)))
{
free((void *)inf_patch[0].name);
inf_patch[0].name = strdup("/dev/null");
}
if (strcmp(inf[1].name, "NUL") == 0)
if (paths::IsNullDeviceName(ucr::toTString(inf[1].name)))
{
free((void *)inf_patch[1].name);
inf_patch[1].name = strdup("/dev/null");
Expand Down
2 changes: 1 addition & 1 deletion Src/DirDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ bool CDirDoc::CompareFilesIfFilesAreLarge(int nFiles, const FileLocation ifilelo

PathContext paths;
for (int i = 0; i < nFiles; ++i)
paths.SetPath(i, ifileloc[i].filepath.empty() ? _T("NUL") : paths::GetParentPath(ifileloc[i].filepath));
paths.SetPath(i, ifileloc[i].filepath.empty() ? paths::NATIVE_NULL_DEVICE_NAME : paths::GetParentPath(ifileloc[i].filepath));
CDiffContext ctxt(paths, CMP_QUICK_CONTENT);
DirViewColItems ci(nFiles, std::vector<String>{});
String msg = LoadResString(IDS_COMPARE_LARGE_FILES);
Expand Down
4 changes: 2 additions & 2 deletions Src/DirView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1405,7 +1405,7 @@ void CDirView::Open(CDirDoc *pDoc, const PathContext& paths, fileopenflags_t dwF
if (paths::DoesPathExist(paths[i]) == paths::DOES_NOT_EXIST)
{
strDesc[i] = sUntitled[i];
filteredPaths.SetPath(i, _T("NUL"), false);
filteredPaths.SetPath(i, paths::NATIVE_NULL_DEVICE_NAME, false);
}
else
{
Expand Down Expand Up @@ -1583,7 +1583,7 @@ void CDirView::OpenSelectionAs(UINT id)
if (paths::DoesPathExist(paths[pane]) == paths::DOES_NOT_EXIST)
{
strDesc[pane] = sUntitled[pane];
filteredPaths.SetPath(pane, _T("NUL"), false);
filteredPaths.SetPath(pane, paths::NATIVE_NULL_DEVICE_NAME, false);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion Src/FolderCmp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ int FolderCmp::prepAndCompareFiles(DIFFITEM &di)

//DiffFileData diffdata; //(filepathTransformed1, filepathTransformed2);
// Invoke unpacking plugins
if (infoUnpacker && strutils::compare_nocase(filepathUnpacked[nIndex], _T("NUL")) != 0)
if (infoUnpacker && !paths::IsNullDeviceName(filepathUnpacked[nIndex]))
{
if (!infoUnpacker->Unpacking(nullptr, filepathUnpacked[nIndex], filteredFilenames, { tFiles[nIndex] }))
goto exitPrepAndCompare;
Expand Down
4 changes: 3 additions & 1 deletion Src/HexMergeDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ CString CHexMergeDoc::GetTooltipString() const
*/
HRESULT CHexMergeDoc::LoadOneFile(int index, const tchar_t* filename, bool readOnly, const String& strDesc)
{
if (filename[0])
if (filename[0] && !paths::IsNullDeviceName(filename))
{
if (Try(m_pView[index]->LoadFile(filename), MB_ICONSTOP) != 0)
return E_FAIL;
Expand All @@ -501,6 +501,8 @@ HRESULT CHexMergeDoc::LoadOneFile(int index, const tchar_t* filename, bool readO
{
m_nBufferType[index] = BUFFERTYPE::UNNAMED;
m_strDesc[index] = strDesc;
if (m_strDesc[index].empty())
m_strDesc[index] = (index == 0) ? _("Untitled left") : ((m_nBuffers < 3 || index == 2) ? _("Untitled right") : _("Untitled middle"));
}
UpdateHeaderPath(index);
m_pView[index]->ResizeWindow();
Expand Down
6 changes: 5 additions & 1 deletion Src/ImgMergeFrm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,12 @@ bool CImgMergeFrame::OpenDocs(int nFiles, const FileLocation fileloc[], const bo
m_filePaths.SetPath(pane, fileloc[pane].filepath, false);
m_bRO[pane] = bRO[pane];
m_strDesc[pane] = strDesc ? strDesc[pane] : _T("");
if (fileloc[pane].filepath.empty())
if (fileloc[pane].filepath.empty() || paths::IsNullDeviceName(fileloc[pane].filepath))
{
m_nBufferType[pane] = BUFFERTYPE::UNNAMED;
if (m_strDesc[pane].empty())
m_strDesc[pane] = (pane == 0) ? _("Untitled left") : ((nFiles < 3 || pane == 2) ? _("Untitled right") : _("Untitled middle"));
}
else
{
m_nBufferType[pane] = (!strDesc || strDesc[pane].empty()) ? BUFFERTYPE::NORMAL : BUFFERTYPE::NORMAL_NAMED;
Expand Down
3 changes: 2 additions & 1 deletion Src/MainFrm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,8 @@ bool CMainFrame::DoFileOrFolderOpen(const PathContext * pFiles /*= nullptr*/,
paths::PATH_EXISTENCE pathsType = paths::GetPairComparability(tFiles, IsArchiveFile);
bool allowFolderCompare = (static_cast<int>(nID) <= 0);
if (tFiles.GetSize() < 2 || pathsType == paths::DOES_NOT_EXIST &&
!std::any_of(tFiles.begin(), tFiles.end(), [](const auto& path) { return path.empty() || paths::IsURL(path); }))
!std::any_of(tFiles.begin(), tFiles.end(),
[](const auto& path) { return path.empty() || paths::IsNullDeviceName(path) || paths::IsURL(path); }))
{
CMultiDocTemplate* pOpenTemplate = theApp.GetOpenTemplate();
if (m_pMenus[MENU_OPENVIEW] == nullptr)
Expand Down
4 changes: 3 additions & 1 deletion Src/MergeDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2870,7 +2870,7 @@ FileLoadResult::flags_t CMergeDoc::LoadOneFile(int index, const String& filename
FileLoadResult::flags_t loadSuccess = FileLoadResult::FRESULT_ERROR;;

m_strDesc[index] = strDesc;
if (!filename.empty())
if (!filename.empty() && !paths::IsNullDeviceName(filename))
{
if (strDesc.empty())
m_nBufferType[index] = BUFFERTYPE::NORMAL;
Expand All @@ -2893,6 +2893,8 @@ FileLoadResult::flags_t CMergeDoc::LoadOneFile(int index, const String& filename
}
else
{
if (m_strDesc[index].empty())
m_strDesc[index] = (index == 0) ? _("Untitled left") : ((m_nBuffers < 3 || index == 2) ? _("Untitled right") : _("Untitled middle"));
m_nBufferType[index] = BUFFERTYPE::UNNAMED;
m_ptBuf[index]->InitNew();
m_ptBuf[index]->m_encoding = encoding;
Expand Down
5 changes: 3 additions & 2 deletions Src/OpenView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,8 @@ void COpenView::OnCompare(UINT nID)
pathsType = paths::GetPairComparability(m_files, IsArchiveFile);

if (pathsType == paths::DOES_NOT_EXIST &&
!std::any_of(m_files.begin(), m_files.end(), [](const auto& path) { return paths::IsURL(path); }))
!std::any_of(m_files.begin(), m_files.end(),
[](const auto& path) { return paths::IsURL(path) || paths::IsNullDeviceName(path); }))
{
LangMessageBox(IDS_ERROR_INCOMPARABLE, MB_ICONSTOP);
return;
Expand Down Expand Up @@ -1124,7 +1125,7 @@ static UINT UpdateButtonStatesThread(LPVOID lpParam)
pathType[i] = paths::DoesPathExist(paths[i], IsArchiveFile);
if (pathType[i] == paths::DOES_NOT_EXIST)
{
if (paths::IsURL(paths[i]))
if (paths::IsURL(paths[i]) || paths::IsNullDeviceName(paths[i]))
pathType[i] = paths::IS_EXISTING_FILE;
else
bInvalid[i] = true;
Expand Down
4 changes: 2 additions & 2 deletions Src/PatchTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ int CPatchTool::CreatePatch()
for (size_t index = 0; index < fileCount; index++)
{
const PATCHFILES& tFiles = fileList[index];
String filename1 = tFiles.lfile.length() == 0 ? _T("NUL") : tFiles.lfile;
String filename2 = tFiles.rfile.length() == 0 ? _T("NUL") : tFiles.rfile;
String filename1 = tFiles.lfile.length() == 0 ? paths::NATIVE_NULL_DEVICE_NAME : tFiles.lfile;
String filename2 = tFiles.rfile.length() == 0 ? paths::NATIVE_NULL_DEVICE_NAME : tFiles.rfile;

// Set up DiffWrapper
m_diffWrapper.SetPaths(PathContext(filename1, filename2), false);
Expand Down
8 changes: 7 additions & 1 deletion Src/WebPageDiffFrm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,14 @@ bool CWebPageDiffFrame::OpenDocs(int nFiles, const FileLocation fileloc[], const
m_filePaths.SetPath(pane, fileloc[pane].filepath, false);
m_bRO[pane] = bRO[pane];
m_strDesc[pane] = strDesc ? strDesc[pane] : _T("");
if (fileloc[pane].filepath.empty())
if (fileloc[pane].filepath.empty() || paths::IsNullDeviceName(fileloc[pane].filepath))
{
m_nBufferType[pane] = BUFFERTYPE::UNNAMED;
if (m_strDesc[pane].empty())
m_strDesc[pane] = (pane == 0) ? _("Untitled left") : ((nFiles < 3 || pane == 2) ? _("Untitled right") : _("Untitled middle"));
if (paths::IsNullDeviceName(fileloc[pane].filepath))
m_filePaths.SetPath(pane, _T("about:blank"), false);
}
else
{
m_nBufferType[pane] = (!strDesc || strDesc[pane].empty()) ? BUFFERTYPE::NORMAL : BUFFERTYPE::NORMAL_NAMED;
Expand Down
2 changes: 1 addition & 1 deletion Src/codepage_detect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ FileTextEncoding Guess(const String& ext, const void * src, size_t len, int gues
*/
FileTextEncoding Guess(const String& filepath, int guessEncodingType, ptrdiff_t mapmaxlen)
{
CMarkdown::FileImage fi(filepath != _T("NUL") ? filepath.c_str() : nullptr, mapmaxlen);
CMarkdown::FileImage fi(!paths::IsNullDeviceName(filepath) ? filepath.c_str() : nullptr, mapmaxlen);
String ext = paths::FindExtension(filepath);
return Guess(ext, fi.pImage, fi.cbImage, guessEncodingType);
}
Expand Down
6 changes: 6 additions & 0 deletions Src/paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -815,4 +815,10 @@ bool IsValidName(const String& name)
return true;
}

bool IsNullDeviceName(const String& name)
{
return (tc::tcsicmp(name.c_str(), NATIVE_NULL_DEVICE_NAME) == 0 ||
tc::tcsicmp(name.c_str(), NATIVE_NULL_DEVICE_NAME_LONG) == 0);
}

}
4 changes: 4 additions & 0 deletions Src/paths.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ typedef enum
IS_EXISTING_DIR, /**< It is existing folder */
} PATH_EXISTENCE;

constexpr tchar_t* NATIVE_NULL_DEVICE_NAME = _T("NUL");
constexpr tchar_t* NATIVE_NULL_DEVICE_NAME_LONG = _T("\\\\.\\NUL");

bool EndsWithSlash(const String& s);

PATH_EXISTENCE DoesPathExist(const String& szPath, bool (*IsArchiveFile)(const String&) = nullptr);
Expand Down Expand Up @@ -54,4 +57,5 @@ inline String AddTrailingSlash(const String& path) { return !EndsWithSlash(path)
String ToWindowsPath(const String& path);
String ToUnixPath(const String& path);
bool IsValidName(const String& name);
bool IsNullDeviceName(const String& name);
}

0 comments on commit 6c5dec2

Please sign in to comment.