Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exfat/io: move exfat_generic_pwrite/pread to node.c from io.c #175

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 0 additions & 109 deletions libexfat/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -386,112 +386,3 @@ ssize_t exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size,
return pwrite(dev->fd, buffer, size, offset);
#endif
}

ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
void* buffer, size_t size, off_t offset)
{
uint64_t newsize = offset;
cluster_t cluster;
char* bufp = buffer;
off_t lsize, loffset, remainder;

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

cluster = exfat_advance_cluster(ef, node, newsize / CLUSTER_SIZE(*ef->sb));
if (CLUSTER_INVALID(*ef->sb, cluster))
{
exfat_error("invalid cluster 0x%x while reading", cluster);
return -EIO;
}

loffset = newsize % CLUSTER_SIZE(*ef->sb);
remainder = MIN(size, node->size - newsize);
while (remainder > 0)
{
if (CLUSTER_INVALID(*ef->sb, cluster))
{
exfat_error("invalid cluster 0x%x while reading", cluster);
return -EIO;
}
lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
if (exfat_pread(ef->dev, bufp, lsize,
exfat_c2o(ef, cluster) + loffset) < 0)
{
exfat_error("failed to read cluster %#x", cluster);
return -EIO;
}
bufp += lsize;
loffset = 0;
remainder -= lsize;
cluster = exfat_next_cluster(ef, node, cluster);
}
if (!(node->attrib & EXFAT_ATTRIB_DIR) && !ef->ro && !ef->noatime)
exfat_update_atime(node);
return MIN(size, node->size - newsize) - remainder;
}

ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
const void* buffer, size_t size, off_t offset)
{
uint64_t newsize = offset;
int rc;
cluster_t cluster;
const char* bufp = buffer;
off_t lsize, loffset, remainder;

if (offset < 0)
return -EINVAL;
if (newsize > node->size)
{
rc = exfat_truncate(ef, node, newsize, true);
if (rc != 0)
return rc;
}
if (newsize + size > node->size)
{
rc = exfat_truncate(ef, node, newsize + size, false);
if (rc != 0)
return rc;
}
if (size == 0)
return 0;

cluster = exfat_advance_cluster(ef, node, newsize / CLUSTER_SIZE(*ef->sb));
if (CLUSTER_INVALID(*ef->sb, cluster))
{
exfat_error("invalid cluster 0x%x while writing", cluster);
return -EIO;
}

loffset = newsize % CLUSTER_SIZE(*ef->sb);
remainder = size;
while (remainder > 0)
{
if (CLUSTER_INVALID(*ef->sb, cluster))
{
exfat_error("invalid cluster 0x%x while writing", cluster);
return -EIO;
}
lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
if (exfat_pwrite(ef->dev, bufp, lsize,
exfat_c2o(ef, cluster) + loffset) < 0)
{
exfat_error("failed to write cluster %#x", cluster);
return -EIO;
}
bufp += lsize;
loffset = 0;
remainder -= lsize;
cluster = exfat_next_cluster(ef, node, cluster);
}
if (!(node->attrib & EXFAT_ATTRIB_DIR))
/* directory's mtime should be updated by the caller only when it
creates or removes something in this directory */
exfat_update_mtime(node);
return size - remainder;
}
109 changes: 109 additions & 0 deletions libexfat/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -1238,3 +1238,112 @@ int exfat_set_label(struct exfat* ef, const char* label)
strcpy(ef->label, label);
return 0;
}

ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node,
void* buffer, size_t size, off_t offset)
{
uint64_t newsize = offset;
cluster_t cluster;
char* bufp = buffer;
off_t lsize, loffset, remainder;

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

cluster = exfat_advance_cluster(ef, node, newsize / CLUSTER_SIZE(*ef->sb));
if (CLUSTER_INVALID(*ef->sb, cluster))
{
exfat_error("invalid cluster 0x%x while reading", cluster);
return -EIO;
}

loffset = newsize % CLUSTER_SIZE(*ef->sb);
remainder = MIN(size, node->size - newsize);
while (remainder > 0)
{
if (CLUSTER_INVALID(*ef->sb, cluster))
{
exfat_error("invalid cluster 0x%x while reading", cluster);
return -EIO;
}
lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
if (exfat_pread(ef->dev, bufp, lsize,
exfat_c2o(ef, cluster) + loffset) < 0)
{
exfat_error("failed to read cluster %#x", cluster);
return -EIO;
}
bufp += lsize;
loffset = 0;
remainder -= lsize;
cluster = exfat_next_cluster(ef, node, cluster);
}
if (!(node->attrib & EXFAT_ATTRIB_DIR) && !ef->ro && !ef->noatime)
exfat_update_atime(node);
return MIN(size, node->size - newsize) - remainder;
}

ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node,
const void* buffer, size_t size, off_t offset)
{
uint64_t newsize = offset;
int rc;
cluster_t cluster;
const char* bufp = buffer;
off_t lsize, loffset, remainder;

if (offset < 0)
return -EINVAL;
if (newsize > node->size)
{
rc = exfat_truncate(ef, node, newsize, true);
if (rc != 0)
return rc;
}
if (newsize + size > node->size)
{
rc = exfat_truncate(ef, node, newsize + size, false);
if (rc != 0)
return rc;
}
if (size == 0)
return 0;

cluster = exfat_advance_cluster(ef, node, newsize / CLUSTER_SIZE(*ef->sb));
if (CLUSTER_INVALID(*ef->sb, cluster))
{
exfat_error("invalid cluster 0x%x while writing", cluster);
return -EIO;
}

loffset = newsize % CLUSTER_SIZE(*ef->sb);
remainder = size;
while (remainder > 0)
{
if (CLUSTER_INVALID(*ef->sb, cluster))
{
exfat_error("invalid cluster 0x%x while writing", cluster);
return -EIO;
}
lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder);
if (exfat_pwrite(ef->dev, bufp, lsize,
exfat_c2o(ef, cluster) + loffset) < 0)
{
exfat_error("failed to write cluster %#x", cluster);
return -EIO;
}
bufp += lsize;
loffset = 0;
remainder -= lsize;
cluster = exfat_next_cluster(ef, node, cluster);
}
if (!(node->attrib & EXFAT_ATTRIB_DIR))
/* directory's mtime should be updated by the caller only when it
creates or removes something in this directory */
exfat_update_mtime(node);
return size - remainder;
}