tinydir icon indicating copy to clipboard operation
tinydir copied to clipboard

`tinydir_open_sorted` returns -1 in MSVC 2005

Open tretdm opened this issue 1 year ago • 5 comments

This error occurs when the current directory is opened, the name of which is specified via _getcwd(). It does not appear in MSVC2019.

void FileManager::readCurrentDir() {
    char cwd[384];
    #ifdef _MSVC
        if (_getcwd(cwd, sizeof(cwd)) != NULL) {
    #else
        if (getcwd(cwd, sizeof(cwd)) != NULL) {
    #endif
        readDir(cwd);
    }
    else
        gInterface->onError(0, 1);
}
void FileManager::readDir(char* pDirPath) {
    int object_index = 0;
    tinydir_dir dir;

    #ifdef _MSVC
        sprintf_s(gCurrentPath, "%s", pDirPath);
    #else
        sprintf(gCurrentPath, "%s", pDirPath);
    #endif

    #ifdef _MSVC
        if (tinydir_open_sorted(&dir, (TCHAR*)pDirPath) == -1) {  // always returns -1 in MSVC 2005
    #else
        if (tinydir_open_sorted(&dir, pDirPath) == -1) {
    #endif
        gInterface->onError(0, -1);
        return;
    }

    for (int i = 1; i < dir.n_files; i++) {
        if (object_index >= MAX_FILES_COUNT) {
            break;
        }
        tinydir_file file;
        if (tinydir_readfile_n(&dir, &file, i) == -1) {
            continue;
        }
        gFiles[object_index] = file;
        object_index++;
    }

    gFilesCount = object_index;
    gInterface->onDirectoryRead(gFiles);
    tinydir_close(&dir);
}

MSVC2005 - x86 build:

SCR_20240610_215048_vmware

MSVC2019 - x64 build:

изображение

tretdm avatar Jun 10 '24 15:06 tretdm

What is the value of errno once tinydir_open_sorted fails? Are you able to step through with a debugger and see which line in tinydir_open_sorted it fails on?

cxong avatar Jun 12 '24 01:06 cxong

What is the value of errno once tinydir_open_sorted fails?

The errno value is 2, on some directories either the list is not returned or not completely (showing the first character for each filename in the iterate_sample.c)

tretdm avatar Jun 12 '24 13:06 tretdm

I also forgot to clarify that if you use a multibyte character set in MSVC2005, the error does not appear.

изображение

tretdm avatar Jun 13 '24 06:06 tretdm

iterate_sample uses tinydir_open instead of tinydir_open_sorted. Perhaps it's a different, similar issue?

Also I meant which line in tinydir.h does the error appear.

errono 2 is ENOENT https://learn.microsoft.com/en-us/cpp/c-runtime-library/errno-constants?view=msvc-170 which could be coming from this line https://github.com/cxong/tinydir/blob/a4a2db51af5fd5146eb1d7bcc72610c7bfa28ebb/tinydir.h#L331 It would be good to reproduce this, the exact API call that was made, and why it is returning an error

cxong avatar Jun 14 '24 02:06 cxong

iterate_sample uses tinydir_open instead of tinydir_open_sorted. Perhaps it's a different, similar issue?

Also I meant which line in tinydir.h does the error appear.

errono 2 is ENOENT https://learn.microsoft.com/en-us/cpp/c-runtime-library/errno-constants?view=msvc-170 which could be coming from this line

https://github.com/cxong/tinydir/blob/a4a2db51af5fd5146eb1d7bcc72610c7bfa28ebb/tinydir.h#L331

It would be good to reproduce this, the exact API call that was made, and why it is returning an error

Yes.

tretdm avatar Jun 14 '24 08:06 tretdm