1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| staticssize_t_dfs_tmpfs_write(struct tmpfs_file *d_file, constvoid *buf, size_tcount, off_t *pos)
{
struct tmpfs_sb *superblock;
RT_ASSERT(d_file != NULL);
superblock = d_file->sb;
RT_ASSERT(superblock != NULL);
if (count + *pos > d_file->size)
{
rt_uint8_t *ptr;
ptr = rt_realloc(d_file->data, *pos + count);
rt_spin_lock(&superblock->lock);
superblock->df_size += (*pos - d_file->size + count);
rt_spin_unlock(&superblock->lock);
d_file->data = ptr;
d_file->size = *pos + count;
LOG_D("tmpfile ptr:%x, size:%d", ptr, d_file->size);
}
if (count > 0)
memcpy(d_file->data + *pos, buf, count);
*pos += count;
return count;
}
staticssize_tdfs_tmpfs_read(struct dfs_file *file, void *buf, size_tcount, off_t *pos)
{
ssize_t length;
struct tmpfs_file *d_file;
d_file = (struct tmpfs_file *)file->vnode->data;
rt_mutex_take(&file->vnode->lock, RT_WAITING_FOREVER);
ssize_t size = (ssize_t)file->vnode->size;
if ((ssize_t)count < size - *pos)
length = count;
else
length = size - *pos;
if (length > 0)
memcpy(buf, &(d_file->data[*pos]), length);
*pos += length;
rt_mutex_release(&file->vnode->lock);
return length;
}
|