int wolfSSH_SFTP_RecvCloseDir(WOLFSSH* ssh, byte* handle, word32 handleSz)
{
    DIR_HANDLE* cur = dirList;
    word32 h[2] = {0,0};

    if (ssh == NULL || handle == NULL || handleSz != (sizeof(word32)*2)) {
        return WS_BAD_ARGUMENT;
    }

    WLOG(WS_LOG_SFTP, "Receiving WOLFSSH_FTP_CLOSE Directory");

    /* find DIR given handle */
    ato32(handle, &h[0]);
    ato32(handle + UINT32_SZ, &h[1]);
    while (cur != NULL) {
        if (cur->id[0] == h[0] && cur->id[1] == h[1]) {
            break;
        }
        cur = cur->next;
    }
    if (cur == NULL) {
        /* unable to find handle */
        return WS_FATAL_ERROR;
    }

#ifdef USE_WINDOWS_API
    FindClose(cur->dir);
#else
    WCLOSEDIR(&cur->dir);
#endif

    /* remove directory from list */
    if (cur != NULL) {
        DIR_HANDLE* pre = dirList;

        WLOG(WS_LOG_SFTP, "Free'ing and closing handle %d%d pointer of [%p]",
                cur->id[1], cur->id[0], cur);
        /* case where node is at head of list */
        if (pre == cur) {
            dirList = cur->next;
            WFREE(cur->dirName, ssh->ctx->heap, DYNTYPE_SFTP);
            WFREE(cur, ssh->ctx->heap, DYNTYPE_SFTP);
        }
        else {
            while (pre->next != NULL && pre->next != cur) pre = pre->next;
            if (pre->next != cur) {
                /* error case where current handle is not in list? */
                return WS_FATAL_ERROR;
            }
            else {
                pre->next = cur->next;
                WFREE(cur->dirName, ssh->ctx->heap, DYNTYPE_SFTP);
                WFREE(cur, ssh->ctx->heap, DYNTYPE_SFTP);
            }
        }
    }

    return WS_SUCCESS;
}
int wolfSSH_SFTP_RecvCloseDir(WOLFSSH* ssh, byte* handle, word32 handleSz)
{
    DIR_HANDLE* cur = dirList;
    word32 h[2] = {0,0};

    if (ssh == NULL || handle == NULL || handleSz != (sizeof(word32)*2)) {
        return WS_BAD_ARGUMENT;
    }

    WLOG(WS_LOG_SFTP, "Receiving WOLFSSH_FTP_CLOSE Directory");

     
    ato32(handle, &h[0]);
    ato32(handle + UINT32_SZ, &h[1]);
    while (cur != NULL) {
        if (cur->id[0] == h[0] && cur->id[1] == h[1]) {
            break;
        }
        cur = cur->next;
    }
    if (cur == NULL) {
         
        return WS_FATAL_ERROR;
    }

#ifdef USE_WINDOWS_API
    FindClose(cur->dir);
#else
    WCLOSEDIR(&cur->dir);
#endif

     
    if (cur != NULL) {
        DIR_HANDLE* pre = dirList;

        WLOG(WS_LOG_SFTP, "Free'ing and closing handle %d%d pointer of [%p]",
                cur->id[1], cur->id[0], cur);
         
        if (pre == cur) {
            dirList = cur->next;
            WFREE(cur->dirName, ssh->ctx->heap, DYNTYPE_SFTP);
            WFREE(cur, ssh->ctx->heap, DYNTYPE_SFTP);
        }
        else {
            while (pre->next != NULL && pre->next != cur) pre = pre->next;
            if (pre->next != cur) {
                 
                return WS_FATAL_ERROR;
            }
            else {
                pre->next = cur->next;
                WFREE(cur->dirName, ssh->ctx->heap, DYNTYPE_SFTP);
                WFREE(cur, ssh->ctx->heap, DYNTYPE_SFTP);
            }
        }
    }

    return WS_SUCCESS;
}