win32api編程——文件系統長文件名測試

不同操作系統中不同的文件系統對於文件目錄名長度的支持是不一樣的,具體可以參考

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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章