void WalkDirectories()

in LittleFs_SDCard/src/Desktop/LittleFsDesktop/LittleFsDesktop/Source.c [139:226]


void WalkDirectories(char* startPath)
{
    char SlashBuffer[MAX_PATH];
    strcpy(SlashBuffer, startPath);
    for (int x = 0; x < strlen(startPath); x++)
    {
        if (SlashBuffer[x] == '/')
            SlashBuffer[x] = '\\';
    }

    snprintf(workingFolder, MAX_PATH, "%s%s", outputFolder, SlashBuffer);

    if (!PathFileExistsA(workingFolder))
    {
        CreateDirectoryA(workingFolder, NULL);
    }

    char SubDirectory[MAX_PATH];
    char FilePath[MAX_PATH];

    lfs_dir_t dir;
    struct lfs_info info;
    int dirReadResult = 0;
    lfs_dir_open(&lfs, &dir, startPath);
    do {
        dirReadResult = lfs_dir_read(&lfs, &dir, &info);
        if (dirReadResult != 0)
        {
            if (info.name[0] != '.')    // not '.' or '..'
            {
                if (info.type == 2)     // directory
                {
                    printf("Directory: %s\n", info.name);
                    if (startPath[strlen(startPath)-1] == '/')
                        snprintf(SubDirectory, MAX_PATH, "%s%s", startPath, info.name);
                    else 
                        snprintf(SubDirectory, MAX_PATH, "%s/%s", startPath, info.name);
                    WalkDirectories(SubDirectory);
                }
                else
                {
                    printf("    %s - ", info.name);
                    snprintf(FilePath, MAX_PATH, "%s/%s", startPath, info.name);

                    struct lfs_info lfsInfo;
                    lfs_stat(&lfs, FilePath, &lfsInfo);
                    printf("%d bytes\n", lfsInfo.size);
                    if (lfsInfo.size > 0)
                    {
                        uint8_t* pFile = malloc(lfsInfo.size);
                        if (pFile == NULL)
                        {
                            printf("failed to allocate space for %s (%d bytes requested)\n", info.name, lfsInfo.size);
                        }
                        else
                        {
                            memset(pFile, 0x00, lfsInfo.size);
                            lfs_file_t datafile;
                            lfs_file_open(&lfs, &datafile, FilePath, LFS_O_RDONLY);
                            lfs_file_read(&lfs, &datafile, pFile, lfsInfo.size);
                            lfs_file_close(&lfs, &datafile);

                            // we've got the data... now write it.
                            snprintf(FilePath, MAX_PATH, "%s%s\\%s", outputFolder, startPath, info.name);
                            for (int x = 0; x < strlen(FilePath); x++)
                            {
                                if (FilePath[x] == '/')
                                    FilePath[x] = '\\';
                            }

#ifdef SHOW_DEBUG_INFO
                            printf("Writing File to... %s\n", FilePath);
#endif

                            DeleteFileA(SubDirectory);
                            HANDLE hOutFile = CreateFileA(FilePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
                            DWORD numWritten = 0;
                            WriteFile(hOutFile, pFile, lfsInfo.size, &numWritten, NULL);
                            CloseHandle(hOutFile);
                            free(pFile);
                        }
                    }
                }
            }
        }
    } while (dirReadResult != 0);
    lfs_dir_close(&lfs, &dir);
}