⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fat.c

📁 This project provides a general purpose library which implements read and write support for MMC, SD
💻 C
📖 第 1 页 / 共 5 页
字号:
                break;            }            /* a parent of the file exists, but not the file itself */            return 0;        }        fat_close_dir(dd);    }        return 0;}/** * \ingroup fat_file * Opens a file on a FAT filesystem. * * \param[in] fs The filesystem on which the file to open lies. * \param[in] dir_entry The directory entry of the file to open. * \returns The file handle, or 0 on failure. * \see fat_close_file */struct fat_file_struct* fat_open_file(struct fat_fs_struct* fs, const struct fat_dir_entry_struct* dir_entry){    if(!fs || !dir_entry || (dir_entry->attributes & FAT_ATTRIB_DIR))        return 0;#if USE_DYNAMIC_MEMORY    struct fat_file_struct* fd = malloc(sizeof(*fd));    if(!fd)        return 0;#else    struct fat_file_struct* fd = fat_file_handles;    uint8_t i;    for(i = 0; i < FAT_FILE_COUNT; ++i)    {        if(!fd->fs)            break;        ++fd;    }    if(i >= FAT_FILE_COUNT)        return 0;#endif        memcpy(&fd->dir_entry, dir_entry, sizeof(*dir_entry));    fd->fs = fs;    fd->pos = 0;    fd->pos_cluster = dir_entry->cluster;    return fd;}/** * \ingroup fat_file * Closes a file. * * \param[in] fd The file handle of the file to close. * \see fat_open_file */void fat_close_file(struct fat_file_struct* fd){    if(fd)#if USE_DYNAMIC_MEMORY        free(fd);#else        fd->fs = 0;#endif}/** * \ingroup fat_file * Reads data from a file. *  * The data requested is read from the current file location. * * \param[in] fd The file handle of the file from which to read. * \param[out] buffer The buffer into which to write. * \param[in] buffer_len The amount of data to read. * \returns The number of bytes read, 0 on end of file, or -1 on failure. * \see fat_write_file */intptr_t fat_read_file(struct fat_file_struct* fd, uint8_t* buffer, uintptr_t buffer_len){    /* check arguments */    if(!fd || !buffer || buffer_len < 1)        return -1;    /* determine number of bytes to read */    if(fd->pos + buffer_len > fd->dir_entry.file_size)        buffer_len = fd->dir_entry.file_size - fd->pos;    if(buffer_len == 0)        return 0;        uint16_t cluster_size = fd->fs->header.cluster_size;    cluster_t cluster_num = fd->pos_cluster;    uintptr_t buffer_left = buffer_len;    uint16_t first_cluster_offset = (uint16_t) (fd->pos & (cluster_size - 1));    /* find cluster in which to start reading */    if(!cluster_num)    {        cluster_num = fd->dir_entry.cluster;                if(!cluster_num)        {            if(!fd->pos)                return 0;            else                return -1;        }        if(fd->pos)        {            uint32_t pos = fd->pos;            while(pos >= cluster_size)            {                pos -= cluster_size;                cluster_num = fat_get_next_cluster(fd->fs, cluster_num);                if(!cluster_num)                    return -1;            }        }    }        /* read data */    do    {        /* calculate data size to copy from cluster */        offset_t cluster_offset = fat_cluster_offset(fd->fs, cluster_num) + first_cluster_offset;        uint16_t copy_length = cluster_size - first_cluster_offset;        if(copy_length > buffer_left)            copy_length = buffer_left;        /* read data */        if(!fd->fs->partition->device_read(cluster_offset, buffer, copy_length))            return buffer_len - buffer_left;        /* calculate new file position */        buffer += copy_length;        buffer_left -= copy_length;        fd->pos += copy_length;        if(first_cluster_offset + copy_length >= cluster_size)        {            /* we are on a cluster boundary, so get the next cluster */            if((cluster_num = fat_get_next_cluster(fd->fs, cluster_num)))            {                first_cluster_offset = 0;            }            else            {                fd->pos_cluster = 0;                return buffer_len - buffer_left;            }        }        fd->pos_cluster = cluster_num;    } while(buffer_left > 0); /* check if we are done */    return buffer_len;}#if DOXYGEN || FAT_WRITE_SUPPORT/** * \ingroup fat_file * Writes data to a file. *  * The data is written to the current file location. * * \param[in] fd The file handle of the file to which to write. * \param[in] buffer The buffer from which to read the data to be written. * \param[in] buffer_len The amount of data to write. * \returns The number of bytes written, 0 on disk full, or -1 on failure. * \see fat_read_file */intptr_t fat_write_file(struct fat_file_struct* fd, const uint8_t* buffer, uintptr_t buffer_len){    /* check arguments */    if(!fd || !buffer || buffer_len < 1)        return -1;    if(fd->pos > fd->dir_entry.file_size)        return -1;    uint16_t cluster_size = fd->fs->header.cluster_size;    cluster_t cluster_num = fd->pos_cluster;    uintptr_t buffer_left = buffer_len;    uint16_t first_cluster_offset = (uint16_t) (fd->pos & (cluster_size - 1));    /* find cluster in which to start writing */    if(!cluster_num)    {        cluster_num = fd->dir_entry.cluster;                if(!cluster_num)        {            if(!fd->pos)            {                /* empty file */                fd->dir_entry.cluster = cluster_num = fat_append_clusters(fd->fs, 0, 1);                if(!cluster_num)                    return -1;            }            else            {                return -1;            }        }        if(fd->pos)        {            uint32_t pos = fd->pos;            cluster_t cluster_num_next;            while(pos >= cluster_size)            {                pos -= cluster_size;                cluster_num_next = fat_get_next_cluster(fd->fs, cluster_num);                if(!cluster_num_next && pos == 0)                    /* the file exactly ends on a cluster boundary, and we append to it */                    cluster_num_next = fat_append_clusters(fd->fs, cluster_num, 1);                if(!cluster_num_next)                    return -1;                cluster_num = cluster_num_next;            }        }    }        /* write data */    do    {        /* calculate data size to write to cluster */        offset_t cluster_offset = fat_cluster_offset(fd->fs, cluster_num) + first_cluster_offset;        uint16_t write_length = cluster_size - first_cluster_offset;        if(write_length > buffer_left)            write_length = buffer_left;        /* write data which fits into the current cluster */        if(!fd->fs->partition->device_write(cluster_offset, buffer, write_length))            break;        /* calculate new file position */        buffer += write_length;        buffer_left -= write_length;        fd->pos += write_length;        if(first_cluster_offset + write_length >= cluster_size)        {            /* we are on a cluster boundary, so get the next cluster */            cluster_t cluster_num_next = fat_get_next_cluster(fd->fs, cluster_num);            if(!cluster_num_next && buffer_left > 0)                /* we reached the last cluster, append a new one */                cluster_num_next = fat_append_clusters(fd->fs, cluster_num, 1);            if(!cluster_num_next)            {                fd->pos_cluster = 0;                break;            }            cluster_num = cluster_num_next;            first_cluster_offset = 0;        }        fd->pos_cluster = cluster_num;    } while(buffer_left > 0); /* check if we are done */    /* update directory entry */    if(fd->pos > fd->dir_entry.file_size)    {        uint32_t size_old = fd->dir_entry.file_size;        /* update file size */        fd->dir_entry.file_size = fd->pos;        /* write directory entry */        if(!fat_write_dir_entry(fd->fs, &fd->dir_entry))        {            /* We do not return an error here since we actually wrote             * some data to disk. So we calculate the amount of data             * we wrote to disk and which lies within the old file size.             */            buffer_left = fd->pos - size_old;            fd->pos = size_old;        }    }    return buffer_len - buffer_left;}#endif/** * \ingroup fat_file * Repositions the read/write file offset. * * Changes the file offset where the next call to fat_read_file() * or fat_write_file() starts reading/writing. * * If the new offset is beyond the end of the file, fat_resize_file() * is implicitly called, i.e. the file is expanded. * * The new offset can be given in different ways determined by * the \c whence parameter: * - \b FAT_SEEK_SET: \c *offset is relative to the beginning of the file. * - \b FAT_SEEK_CUR: \c *offset is relative to the current file position. * - \b FAT_SEEK_END: \c *offset is relative to the end of the file. * * The resulting absolute offset is written to the location the \c offset * parameter points to. *  * \param[in] fd The file decriptor of the file on which to seek. * \param[in,out] offset A pointer to the new offset, as affected by the \c whence *                   parameter. The function writes the new absolute offset *                   to this location before it returns. * \param[in] whence Affects the way \c offset is interpreted, see above. * \returns 0 on failure, 1 on success. */uint8_t fat_seek_file(struct fat_file_struct* fd, int32_t* offset, uint8_t whence){    if(!fd || !offset)        return 0;    uint32_t new_pos = fd->pos;    switch(whence)    {        case FAT_SEEK_SET:            new_pos = *offset;            break;        case FAT_SEEK_CUR:            new_pos += *offset;            break;        case FAT_SEEK_END:            new_pos = fd->dir_entry.file_size + *offset;            break;        default:            return 0;    }    if(new_pos > fd->dir_entry.file_size#if FAT_WRITE_SUPPORT       && !fat_resize_file(fd, new_pos)#endif       )        return 0;    fd->pos = new_pos;    fd->pos_cluster = 0;    *offset = (int32_t) new_pos;    return 1;}#if DOXYGEN || FAT_WRITE_SUPPORT/** * \ingroup fat_file * Resizes a file to have a specific size. * * Enlarges or shrinks the file pointed to by the file descriptor to have * exactly the specified size. * * If the file is truncated, all bytes having an equal or larger offset * than the given size are lost. If the file is expanded, the additional * bytes are allocated. * * \note Please be aware that this function just allocates or deallocates disk * space, it does not explicitely clear it. To avoid data leakage, this * must be done manually. * * \param[in] fd The file decriptor of the file which to resize. * \param[in] size The new size of the file. * \returns 0 on failure, 1 on success. */uint8_t fat_resize_file(struct fat_file_struct* fd, uint32_t size){    if(!fd)        return 0;    cluster_t cluster_num = fd->dir_entry.cluster;    uint16_t cluster_size = fd->fs->header.cluster_size;    uint32_t size_new = size;    do    {        if(cluster_num == 0 && size_new == 0)            /* the file stays empty */            break;        /* seek to the next cluster as long as we need the space */        while(size_new > cluster_size)        {            /* get next cluster of file */            cluster_t cluster_num_next = fat_get_next_cluster(fd->fs, cluster_num);            if(cluster_num_next)            {                cluster_num = cluster_num_next;                size_new -= cluster_size;            }            else            {                break;            }        }        if(size_new > cluster_size || cluster_num == 0)        {            /* Allocate new cluster chain and append             * it to the existing one, if available.             */            cluster_t cluster_count = (size_new + cluster_size - 1) / cluster_size;            cluster_t cluster_new_chain = fat_append_clusters(fd->fs, cluster_num, cluster_count);            if(!cluster_new_chain)                return 0;            if(!cluster_num)            {                cluster_num = cluster_new_chain;                fd->dir_entry.cluster = cluster_num;            }        }        /* write new directory entry */        fd->dir_entry.file_size = size;        if(size == 0)            fd->dir_entry.cluster = 0;        if(!fat_write_dir_entry(fd->fs, &fd->dir_entry))            return 0;        if(size == 0)        {            /* free all clusters of file */            fat_free_clusters(fd->fs, cluster_num);        }        else if(size_new <= cluster_size)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -