void exfat_flush_node(struct exfat* ef, struct exfat_node* node)
{
    cluster_t cluster;
    off64_t offset;
    off64_t meta1_offset, meta2_offset;
    struct exfat_entry_meta1 meta1;
    struct exfat_entry_meta2 meta2;

    if (ef->ro)
        exfat_bug("unable to flush node to read-only FS");

    if (node->parent == NULL)
        return; /* do not flush unlinked node */

    cluster = node->entry_cluster;
    offset = node->entry_offset;
    meta1_offset = co2o(ef, cluster, offset);
    next_entry(ef, node->parent, &cluster, &offset);
    meta2_offset = co2o(ef, cluster, offset);

    exfat_read_raw(&meta1, sizeof(meta1), meta1_offset, ef->fd);
    if (meta1.type != EXFAT_ENTRY_FILE)
        exfat_bug("invalid type of meta1: 0x%hhx", meta1.type);
    meta1.attrib = cpu_to_le16(node->flags);
    exfat_unix2exfat(node->mtime, &meta1.mdate, &meta1.mtime, &meta1.mtime_cs);
    exfat_unix2exfat(node->atime, &meta1.adate, &meta1.atime, NULL);

    exfat_read_raw(&meta2, sizeof(meta2), meta2_offset, ef->fd);
    if (meta2.type != EXFAT_ENTRY_FILE_INFO)
        exfat_bug("invalid type of meta2: 0x%hhx", meta2.type);
    meta2.size = meta2.real_size = cpu_to_le64(node->size);
    meta2.start_cluster = cpu_to_le32(node->start_cluster);
    meta2.flags = EXFAT_FLAG_ALWAYS1;
    /* empty files must not be marked as contiguous */
    if (node->size != 0 && IS_CONTIGUOUS(*node))
        meta2.flags |= EXFAT_FLAG_CONTIGUOUS;
    /* name hash remains unchanged, no need to recalculate it */

    meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name);

    exfat_write_raw(&meta1, sizeof(meta1), meta1_offset, ef->fd);
    exfat_write_raw(&meta2, sizeof(meta2), meta2_offset, ef->fd);

    node->flags &= ~EXFAT_ATTRIB_DIRTY;
}
static int opendir(struct exfat* ef, const struct exfat_node* dir,
                   struct iterator* it)
{
    if (!(dir->flags & EXFAT_ATTRIB_DIR))
        exfat_bug("not a directory");
    it->cluster = dir->start_cluster;
    it->offset = 0;
    it->contiguous = IS_CONTIGUOUS(*dir);
    it->chunk = malloc(CLUSTER_SIZE(*ef->sb));
    if (it->chunk == NULL)
    {
        exfat_error("out of memory");
        return -ENOMEM;
    }
    exfat_read_raw(it->chunk, CLUSTER_SIZE(*ef->sb),
                   exfat_c2o(ef, it->cluster), ef->fd);
    return 0;
}
static int fetch_next_entry(struct exfat* ef, const struct exfat_node* parent,
                            struct iterator* it)
{
    /* move iterator to the next entry in the directory */
    it->offset += sizeof(struct exfat_entry);
    /* fetch the next cluster if needed */
    if ((it->offset & (CLUSTER_SIZE(*ef->sb) - 1)) == 0)
    {
        it->cluster = exfat_next_cluster(ef, parent, it->cluster);
        if (CLUSTER_INVALID(it->cluster))
        {
            exfat_error("invalid cluster while reading directory");
            return 1;
        }
        exfat_read_raw(it->chunk, CLUSTER_SIZE(*ef->sb),
                       exfat_c2o(ef, it->cluster), ef->fd);
    }
    return 0;
}
Example #4
0
ssize_t exfat_read(const struct exfat* ef, struct exfat_node* node,
		void* buffer, size_t size, off_t offset)
{
	cluster_t cluster;
	char* bufp = buffer;
	off_t lsize, loffset, remainder;

	if (offset >= node->size)
		return 0;
	if (size == 0)
		return 0;

	cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb));
	if (CLUSTER_INVALID(cluster))
	{
		exfat_error("got invalid cluster");
		return -1;
	}

	loffset = offset % CLUSTER_SIZE(*ef->sb);
	remainder = MIN(size, node->size - offset);
	while (remainder > 0)
	{
		if (CLUSTER_INVALID(cluster))
		{
			exfat_error("got invalid cluster");
			return -1;
		}
		lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
		exfat_read_raw(bufp, lsize, exfat_c2o(ef, cluster) + loffset, ef->fd);
		bufp += lsize;
		loffset = 0;
		remainder -= lsize;
		cluster = exfat_next_cluster(ef, node, cluster);
	}
	if (!ef->ro && !ef->noatime)
		exfat_update_atime(node);
	return size - remainder;
}
/*
 * Reads one entry in directory at position pointed by iterator and fills
 * node structure.
 */
static int readdir(struct exfat* ef, const struct exfat_node* parent,
                   struct exfat_node** node, struct iterator* it)
{
    const struct exfat_entry* entry;
    const struct exfat_entry_meta1* meta1;
    const struct exfat_entry_meta2* meta2;
    const struct exfat_entry_name* file_name;
    const struct exfat_entry_upcase* upcase;
    const struct exfat_entry_bitmap* bitmap;
    const struct exfat_entry_label* label;
    uint8_t continuations = 0;
    le16_t* namep = NULL;
    uint16_t reference_checksum = 0;
    uint16_t actual_checksum = 0;

    *node = NULL;

    for (;;)
    {
        /* every directory (even empty one) occupies at least one cluster and
           must contain EOD entry */
        entry = get_entry_ptr(ef, it);

        switch (entry->type)
        {
        case EXFAT_ENTRY_EOD:
            if (continuations != 0)
            {
                exfat_error("expected %hhu continuations before EOD",
                            continuations);
                goto error;
            }
            return -ENOENT; /* that's OK, means end of directory */

        case EXFAT_ENTRY_FILE:
            if (continuations != 0)
            {
                exfat_error("expected %hhu continuations before new entry",
                            continuations);
                goto error;
            }
            meta1 = (const struct exfat_entry_meta1*) entry;
            continuations = meta1->continuations;
            /* each file entry must have at least 2 continuations:
               info and name */
            if (continuations < 2)
            {
                exfat_error("too few continuations (%hhu)", continuations);
                return -EIO;
            }
            reference_checksum = le16_to_cpu(meta1->checksum);
            actual_checksum = exfat_start_checksum(meta1);
            *node = allocate_node();
            if (*node == NULL)
                return -ENOMEM;
            /* new node has zero reference counter */
            (*node)->entry_cluster = it->cluster;
            (*node)->entry_offset = it->offset;
            init_node_meta1(*node, meta1);
            namep = (*node)->name;
            break;

        case EXFAT_ENTRY_FILE_INFO:
            if (continuations < 2)
            {
                exfat_error("unexpected continuation (%hhu)",
                            continuations);
                goto error;
            }
            meta2 = (const struct exfat_entry_meta2*) entry;
            if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS))
            {
                exfat_error("unknown flags in meta2 (0x%hhx)", meta2->flags);
                goto error;
            }
            init_node_meta2(*node, meta2);
            actual_checksum = exfat_add_checksum(entry, actual_checksum);
            /* There are two fields that contain file size. Maybe they plan
               to add compression support in the future and one of those
               fields is visible (uncompressed) size and the other is real
               (compressed) size. Anyway, currently it looks like exFAT does
               not support compression and both fields must be equal. */
            if (le64_to_cpu(meta2->real_size) != (*node)->size)
            {
                exfat_error("real size does not equal to size "
                            "(%"PRIu64" != %"PRIu64")",
                            le64_to_cpu(meta2->real_size), (*node)->size);
                //goto error;
            }
            /* empty files must be marked as non-contiguous */
            if ((*node)->size == 0 && (meta2->flags & EXFAT_FLAG_CONTIGUOUS))
            {
                exfat_error("empty file marked as contiguous (0x%hhx)",
                            meta2->flags);
                //goto error;
            }
            /* directories must be aligned on at cluster boundary */
            if (((*node)->flags & EXFAT_ATTRIB_DIR) &&
                    (*node)->size % CLUSTER_SIZE(*ef->sb) != 0)
            {
                char buffer[EXFAT_NAME_MAX + 1];

                exfat_get_name(*node, buffer, EXFAT_NAME_MAX);
                exfat_error("directory `%s' has invalid size %"PRIu64" bytes",
                            buffer, (*node)->size);
                goto error;
            }
            --continuations;
            break;

        case EXFAT_ENTRY_FILE_NAME:
            if (continuations == 0)
            {
                exfat_error("unexpected continuation");
                goto error;
            }
            file_name = (const struct exfat_entry_name*) entry;
            actual_checksum = exfat_add_checksum(entry, actual_checksum);

            memcpy(namep, file_name->name, EXFAT_ENAME_MAX * sizeof(le16_t));
            namep += EXFAT_ENAME_MAX;
            if (--continuations == 0)
            {
                if (actual_checksum != reference_checksum)
                {
                    exfat_error("invalid checksum (0x%hx != 0x%hx)",
                                actual_checksum, reference_checksum);
                    return -EIO;
                }
                if (fetch_next_entry(ef, parent, it) != 0)
                    goto error;
                return 0; /* entry completed */
            }
            break;

        case EXFAT_ENTRY_UPCASE:
            if (ef->upcase != NULL)
                break;
            upcase = (const struct exfat_entry_upcase*) entry;
            if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster)))
            {
                exfat_error("invalid cluster in upcase table");
                return -EIO;
            }
            if (le64_to_cpu(upcase->size) == 0 ||
                    le64_to_cpu(upcase->size) > 0xffff * sizeof(uint16_t) ||
                    le64_to_cpu(upcase->size) % sizeof(uint16_t) != 0)
            {
                exfat_error("bad upcase table size (%"PRIu64" bytes)",
                            le64_to_cpu(upcase->size));
                return -EIO;
            }
            ef->upcase = malloc(le64_to_cpu(upcase->size));
            if (ef->upcase == NULL)
            {
                exfat_error("failed to allocate upcase table (%"PRIu64" bytes)",
                            le64_to_cpu(upcase->size));
                return -ENOMEM;
            }
            ef->upcase_chars = le64_to_cpu(upcase->size) / sizeof(le16_t);

            exfat_read_raw(ef->upcase, le64_to_cpu(upcase->size),
                           exfat_c2o(ef, le32_to_cpu(upcase->start_cluster)), ef->fd);
            break;

        case EXFAT_ENTRY_BITMAP:
            bitmap = (const struct exfat_entry_bitmap*) entry;
            if (CLUSTER_INVALID(le32_to_cpu(bitmap->start_cluster)))
            {
                exfat_error("invalid cluster in clusters bitmap");
                return -EIO;
            }
            ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) -
                            EXFAT_FIRST_DATA_CLUSTER;
            if (le64_to_cpu(bitmap->size) < (ef->cmap.size + 7) / 8)
            {
                exfat_error("invalid clusters bitmap size: %"PRIu64
                            " (expected at least %u)",
                            le64_to_cpu(bitmap->size), (ef->cmap.size + 7) / 8);
                return -EIO;
            }
            ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster);
            /* FIXME bitmap can be rather big, up to 512 MB */
            ef->cmap.chunk_size = ef->cmap.size;
            ef->cmap.chunk = malloc(le64_to_cpu(bitmap->size));
            if (ef->cmap.chunk == NULL)
            {
                exfat_error("failed to allocate clusters bitmap chunk "
                            "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size));
                return -ENOMEM;
            }

            exfat_read_raw(ef->cmap.chunk, le64_to_cpu(bitmap->size),
                           exfat_c2o(ef, ef->cmap.start_cluster), ef->fd);
            break;

        case EXFAT_ENTRY_LABEL:
            label = (const struct exfat_entry_label*) entry;
            if (label->length > EXFAT_ENAME_MAX)
            {
                exfat_error("too long label (%hhu chars)", label->length);
                return -EIO;
            }
            if (utf16_to_utf8(ef->label, label->name,
                              sizeof(ef->label), EXFAT_ENAME_MAX) != 0)
                return -EIO;
            break;

        default:
            if (entry->type & EXFAT_ENTRY_VALID)
            {
                exfat_error("unknown entry type 0x%hhx", entry->type);
                goto error;
            }
            break;
        }

        if (fetch_next_entry(ef, parent, it) != 0)
            goto error;
    }
    /* we never reach here */

error:
    free(*node);
    *node = NULL;
    return -EIO;
}