不同操作系统中不同的文件系统对于文件目录名长度的支持是不一样的,具体可以参考
http://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits
在windows logo kit中的IFS test中有一个长文件名测试,请参考链接中OpenCreateGeneral File System Test Group——FileNameLengthTest:
http://msdn.microsoft.com/en-us/library/ff563405%28VS.85%29.aspx
该测试会生成两个超长文件名的文件,在某些系统上是允许生成,某些系统上是不支持的。
而我要做的是在windows环境下用win32 api来写长文件名测试,模拟IFS test中的FileNameLengthTest测试,在windows下若要生成超长文件名,则需要在文件路径(绝对路径)前添加\\?\前缀,具体请参考msdn上链接:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
以下是我写的长文件名测试程序,允许在任意盘符,生成任意长度的文件名和目录名,在mingw下测试通过:
https://github.com/TraumLou/CPPEverything/blob/master/win32/LongFileNameTest.cpp
以下是我所写的程序:
/*************************************************************************** * File: LongFileNameTest.cpp * * Purpose: . Test whether DayuFS can support Long FileName * * Caution: This program can only compile and run in windows * * Compile Command: g++ -D_UNICODE -DUNICODE -static-libgcc -static-libstdc++ * LongFileName.cpp -o LongFileName; strip -s LongFileName * ***************************************************************************/ #include <windows.h> #include <iostream> #include <string> std::string format_errmsg(DWORD error) { if(error) { LPVOID lpMsgBuf; DWORD dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS; DWORD bufLen = FormatMessageA(dwFlags, NULL, error, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (LPSTR)&lpMsgBuf, 0, NULL); if(bufLen) { LPSTR lpMsgStr = (LPSTR)lpMsgBuf; std::string result(lpMsgStr); LocalFree(lpMsgBuf); return result; } } return std::string(); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE HPrevInstance, LPSTR lpCmdLine, int nCmdShow) { using namespace std; wstring mp; int length; cout << "Please enter the mountpoint(example: R): "; wcin >> mp; cout << "Please enter the length of filename you want " << "to create(less than 3000 and greater than 0): "; cin >> length; cout << endl; WCHAR filename[3020] = L"\\\\?\\"; WCHAR dirname[3020] = L"\\\\?\\"; wcsncat(filename, mp.c_str(), 1); wcsncat(dirname, mp.c_str(), 1); wcscat(filename, L":\\"); wcscat(dirname, L":\\"); WCHAR longname[3000] = L""; wcsncat(longname, mp.c_str(), 1); wcscat(longname, L":\\"); for (int i = 0; i < length; ++i) { wcscat(filename, L"F"); wcscat(dirname, L"D"); wcscat(longname, L"L"); } HANDLE hndl = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); if (hndl == INVALID_HANDLE_VALUE) { DWORD aa = GetLastError(); wcout << filename << std::endl; cout << "extended-length filename(" << length << " character):" << endl << "Error code: " << aa << endl << "Error message: " << format_errmsg(aa) << endl; } else { cout << "extended-length file created successfully!" << endl; } HANDLE hndl1 = CreateFile(longname, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); if (hndl == INVALID_HANDLE_VALUE) { DWORD aa = GetLastError(); cout << "filename(" << length << " character):" << endl << "Error code: " << aa << endl << "Error message: " << format_errmsg(aa) << endl; } else { cout << "file created successfully!" << endl; } if (!CreateDirectory(dirname, NULL)) { DWORD aa = GetLastError(); cout << "extended-length directory(" << length << " character):" << endl << "Error code: " << aa << endl << "Error message: " << format_errmsg(aa) << endl; } else { cout << "extended-length direcotry created successfully!" << endl; } CloseHandle(hndl); CloseHandle(hndl1); return 0; }
本文出自 “风雨兼程北京梦” 博客,请务必保留此出处http://traumlou.blog.51cto.com/8561535/1579358
原文地址:http://traumlou.blog.51cto.com/8561535/1579358