FAT12 read only file system

Fork of FATFileSystem by mbed official

F12RFileHandle.cpp

Committer:
va009039
Date:
2016-04-10
Revision:
9:d92e85b739a7
Parent:
8:6c6acf81ff08

File content as of revision 9:d92e85b739a7:

/* mbed Microcontroller Library
 * Copyright (c) 2006-2016 ARM Limited
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include <algorithm>
#include "F12RFileHandle.h"
#include "fsdebug.h"

F12RFileHandle::F12RFileHandle(F12RFileSystem& fs_, uint32_t d):fs(fs_) {
    pos = 0;
    cluster = fs.storage_peek(d + 26, 2);
    file_length = fs.storage_peek(d + 28, 4);
    FS_DBG("cluster=%d", cluster);
    FS_DBG("file_length=%d", file_length);
}

int F12RFileHandle::close() {
    delete this;
    return 0;
}

ssize_t F12RFileHandle::write(const void* buffer, size_t length) {
    return -1;
}

ssize_t F12RFileHandle::read(void* buffer, size_t length) {
    FS_DBG("length=%d", length);
    ssize_t n = 0;
    while(n < length && pos < file_length) {
        uint32_t chunk = std::min((filesize_t)length - n, file_length - pos);
        chunk = std::min(chunk, fs.cluster_tail(pos));
        FS_TEST_ASSERT(cluster >= 2 && cluster < 0xff8);
        fs.storage->Read(fs.base_data + (cluster-2)*fs.cluster_size + fs.cluster_head(pos), (uint8_t*)buffer + n, chunk);
        pos += chunk;
        if (fs.cluster_head(pos) == 0) {
            cluster = fs.fat_read(cluster);
        }
        n += chunk;
    }
    return n;
}

int F12RFileHandle::isatty() {
    return 0;
}

off_t F12RFileHandle::lseek(off_t position, int whence) {
    FS_DBG("position=%d whence=%d", (uint32_t)position, whence);
    return -1;
}

int F12RFileHandle::fsync() {
    return 0;
}

off_t F12RFileHandle::flen() {
    FS_DBG("file_length=%d", file_length);
    return file_length;
}