Example #1
0
FSStatus
FSGetPosFileAsync(FSClient *client, FSCmdBlock *block, FSFileHandle handle, be_val<uint32_t> *pos, uint32_t flags, FSAsyncData *asyncData)
{
   assert(asyncData->callback);
   auto result = FSGetPosFile(client, block, handle, pos, flags);
   FSAsyncCallback cb = static_cast<uint32_t>(asyncData->callback);
   cb(client, block, result, asyncData->param);
   return result;
}
static off_t FSDrvSeek(struct _reent* r, int fd, off_t pos, int dir) {
    FSFileHandle* handle = (FSFileHandle*) fd;

    FSStatus err = FS_STATUS_OK;

    switch(dir) {
        case SEEK_SET:
            break;
        case SEEK_CUR: {
            u32 currPos = 0;
            if((err = FSGetPosFile(fsClient, fsCmdBlock, *handle, &currPos, FS_RET_ALL_ERROR)) == FS_STATUS_OK) {
                pos += currPos;
            }

            break;
        }
        case SEEK_END: {
            FSStat stat;
            if((err = FSGetStatFile(fsClient, fsCmdBlock, *handle, &stat, FS_RET_ALL_ERROR)) == FS_STATUS_OK) {
                pos += stat.size;
            }

            break;
        }
        default:
            r->_errno = EINVAL;
            return -1;
    }

    if(err == FS_STATUS_OK) {
        if(pos < 0) {
            r->_errno = EINVAL;
            return -1;
        }

        err = FSSetPosFile(fsClient, fsCmdBlock, *handle, (u32) pos, FS_RET_ALL_ERROR);
    }

    if(err != FS_ERROR_OK) {
        FSDrvReportError(r);
        return -1;
    }

    return 0;
}
static int FSDrvFTruncate(struct _reent* r, int fd, off_t len) {
    FSFileHandle* handle = (FSFileHandle*) fd;

    FSStatus err = FS_STATUS_OK;

    u32 currPos = 0;
    if((err = FSGetPosFile(fsClient, fsCmdBlock, *handle, &currPos, FS_RET_ALL_ERROR)) == FS_STATUS_OK
       && (err = FSSetPosFile(fsClient, fsCmdBlock, *handle, (u32) len, FS_RET_ALL_ERROR)) == FS_STATUS_OK) {
        err = FSTruncateFile(fsClient, fsCmdBlock, *handle, FS_RET_ALL_ERROR);

        FSStatus restoreErr = FSSetPosFile(fsClient, fsCmdBlock, *handle, currPos, FS_RET_ALL_ERROR);
        if(err == FS_STATUS_OK) {
            err = restoreErr;
        }
    }

    if(err != FS_ERROR_OK) {
        FSDrvReportError(r);
        return -1;
    }

    return 0;
}