Lines Matching full:file

2  * fileio.c --- Simple file I/O routines
7 * This file may be redistributed under the terms of the GNU Library
34 #define BMAP_BUFFER (file->buf + fs->blocksize)
40 ext2_file_t file; in ext2fs_file_open2() local
44 * Don't let caller create or open a file for writing if the in ext2fs_file_open2()
51 retval = ext2fs_get_mem(sizeof(struct ext2_file), &file); in ext2fs_file_open2()
55 memset(file, 0, sizeof(struct ext2_file)); in ext2fs_file_open2()
56 file->magic = EXT2_ET_MAGIC_EXT2_FILE; in ext2fs_file_open2()
57 file->fs = fs; in ext2fs_file_open2()
58 file->ino = ino; in ext2fs_file_open2()
59 file->flags = flags & EXT2_FILE_MASK; in ext2fs_file_open2()
62 memcpy(&file->inode, inode, sizeof(struct ext2_inode)); in ext2fs_file_open2()
64 retval = ext2fs_read_inode(fs, ino, &file->inode); in ext2fs_file_open2()
69 retval = ext2fs_get_array(3, fs->blocksize, &file->buf); in ext2fs_file_open2()
73 *ret = file; in ext2fs_file_open2()
77 if (file->buf) in ext2fs_file_open2()
78 ext2fs_free_mem(&file->buf); in ext2fs_file_open2()
79 ext2fs_free_mem(&file); in ext2fs_file_open2()
90 * This function returns the filesystem handle of a file from the structure
92 ext2_filsys ext2fs_file_get_fs(ext2_file_t file) in ext2fs_file_get_fs() argument
94 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE) in ext2fs_file_get_fs()
96 return file->fs; in ext2fs_file_get_fs()
100 * This function returns the pointer to the inode of a file from the structure
102 struct ext2_inode *ext2fs_file_get_inode(ext2_file_t file) in ext2fs_file_get_inode() argument
104 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE) in ext2fs_file_get_inode()
106 return &file->inode; in ext2fs_file_get_inode()
110 ext2_ino_t ext2fs_file_get_inode_num(ext2_file_t file) in ext2fs_file_get_inode_num() argument
112 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE) in ext2fs_file_get_inode_num()
114 return file->ino; in ext2fs_file_get_inode_num()
121 errcode_t ext2fs_file_flush(ext2_file_t file) in ext2fs_file_flush() argument
126 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE); in ext2fs_file_flush()
127 fs = file->fs; in ext2fs_file_flush()
129 if (!(file->flags & EXT2_FILE_BUF_VALID) || in ext2fs_file_flush()
130 !(file->flags & EXT2_FILE_BUF_DIRTY)) in ext2fs_file_flush()
137 if (!file->physblock) { in ext2fs_file_flush()
138 retval = ext2fs_bmap2(fs, file->ino, &file->inode, in ext2fs_file_flush()
139 BMAP_BUFFER, file->ino ? BMAP_ALLOC : 0, in ext2fs_file_flush()
140 file->blockno, 0, &file->physblock); in ext2fs_file_flush()
145 retval = io_channel_write_blk64(fs->io, file->physblock, 1, file->buf); in ext2fs_file_flush()
149 file->flags &= ~EXT2_FILE_BUF_DIRTY; in ext2fs_file_flush()
155 * This function synchronizes the file's block buffer and the current
156 * file position, possibly invalidating block buffer if necessary
158 static errcode_t sync_buffer_position(ext2_file_t file) in sync_buffer_position() argument
163 b = file->pos / file->fs->blocksize; in sync_buffer_position()
164 if (b != file->blockno) { in sync_buffer_position()
165 retval = ext2fs_file_flush(file); in sync_buffer_position()
168 file->flags &= ~EXT2_FILE_BUF_VALID; in sync_buffer_position()
170 file->blockno = b; in sync_buffer_position()
175 * This function loads the file's block buffer with valid data from
180 * function basically only sets file->physblock and EXT2_FILE_BUF_VALID
183 static errcode_t load_buffer(ext2_file_t file, int dontfill) in load_buffer() argument
185 ext2_filsys fs = file->fs; in load_buffer()
188 if (!(file->flags & EXT2_FILE_BUF_VALID)) { in load_buffer()
189 retval = ext2fs_bmap2(fs, file->ino, &file->inode, in load_buffer()
190 BMAP_BUFFER, 0, file->blockno, 0, in load_buffer()
191 &file->physblock); in load_buffer()
195 if (file->physblock) { in load_buffer()
197 file->physblock, in load_buffer()
198 1, file->buf); in load_buffer()
202 memset(file->buf, 0, fs->blocksize); in load_buffer()
204 file->flags |= EXT2_FILE_BUF_VALID; in load_buffer()
210 errcode_t ext2fs_file_close(ext2_file_t file) in ext2fs_file_close() argument
214 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE); in ext2fs_file_close()
216 retval = ext2fs_file_flush(file); in ext2fs_file_close()
218 if (file->buf) in ext2fs_file_close()
219 ext2fs_free_mem(&file->buf); in ext2fs_file_close()
220 ext2fs_free_mem(&file); in ext2fs_file_close()
226 errcode_t ext2fs_file_read(ext2_file_t file, void *buf, in ext2fs_file_read() argument
235 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE); in ext2fs_file_read()
236 fs = file->fs; in ext2fs_file_read()
238 while ((file->pos < EXT2_I_SIZE(&file->inode)) && (wanted > 0)) { in ext2fs_file_read()
239 retval = sync_buffer_position(file); in ext2fs_file_read()
242 retval = load_buffer(file, 0); in ext2fs_file_read()
246 start = file->pos % fs->blocksize; in ext2fs_file_read()
250 left = EXT2_I_SIZE(&file->inode) - file->pos ; in ext2fs_file_read()
254 memcpy(ptr, file->buf+start, c); in ext2fs_file_read()
255 file->pos += c; in ext2fs_file_read()
268 errcode_t ext2fs_file_write(ext2_file_t file, const void *buf, in ext2fs_file_write() argument
276 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE); in ext2fs_file_write()
277 fs = file->fs; in ext2fs_file_write()
279 if (!(file->flags & EXT2_FILE_WRITE)) in ext2fs_file_write()
283 retval = sync_buffer_position(file); in ext2fs_file_write()
287 start = file->pos % fs->blocksize; in ext2fs_file_write()
296 retval = load_buffer(file, (c == fs->blocksize)); in ext2fs_file_write()
304 if (!file->physblock) { in ext2fs_file_write()
305 retval = ext2fs_bmap2(fs, file->ino, &file->inode, in ext2fs_file_write()
307 file->ino ? BMAP_ALLOC : 0, in ext2fs_file_write()
308 file->blockno, 0, in ext2fs_file_write()
309 &file->physblock); in ext2fs_file_write()
314 file->flags |= EXT2_FILE_BUF_DIRTY; in ext2fs_file_write()
315 memcpy(file->buf+start, ptr, c); in ext2fs_file_write()
316 file->pos += c; in ext2fs_file_write()
324 if (count != 0 && EXT2_I_SIZE(&file->inode) < file->pos) { in ext2fs_file_write()
327 rc = ext2fs_file_set_size2(file, file->pos); in ext2fs_file_write()
337 errcode_t ext2fs_file_llseek(ext2_file_t file, __u64 offset, in ext2fs_file_llseek() argument
340 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE); in ext2fs_file_llseek()
343 file->pos = offset; in ext2fs_file_llseek()
345 file->pos += offset; in ext2fs_file_llseek()
347 file->pos = EXT2_I_SIZE(&file->inode) + offset; in ext2fs_file_llseek()
352 *ret_pos = file->pos; in ext2fs_file_llseek()
357 errcode_t ext2fs_file_lseek(ext2_file_t file, ext2_off_t offset, in ext2fs_file_lseek() argument
364 retval = ext2fs_file_llseek(file, loffset, whence, &ret_loffset); in ext2fs_file_lseek()
372 * This function returns the size of the file, according to the inode
374 errcode_t ext2fs_file_get_lsize(ext2_file_t file, __u64 *ret_size) in ext2fs_file_get_lsize() argument
376 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE) in ext2fs_file_get_lsize()
378 *ret_size = EXT2_I_SIZE(&file->inode); in ext2fs_file_get_lsize()
383 * This function returns the size of the file, according to the inode
385 ext2_off_t ext2fs_file_get_size(ext2_file_t file) in ext2fs_file_get_size() argument
389 if (ext2fs_file_get_lsize(file, &size)) in ext2fs_file_get_size()
397 static errcode_t ext2fs_file_zero_past_offset(ext2_file_t file, in ext2fs_file_zero_past_offset() argument
400 ext2_filsys fs = file->fs; in ext2fs_file_zero_past_offset()
410 retval = sync_buffer_position(file); in ext2fs_file_zero_past_offset()
415 retval = ext2fs_bmap2(fs, file->ino, NULL, NULL, 0, in ext2fs_file_zero_past_offset()
444 * This function sets the size of the file, truncating it if necessary
447 errcode_t ext2fs_file_set_size2(ext2_file_t file, ext2_off64_t size) in ext2fs_file_set_size2() argument
453 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE); in ext2fs_file_set_size2()
455 if (size && ext2fs_file_block_offset_too_big(file->fs, &file->inode, in ext2fs_file_set_size2()
456 (size - 1) / file->fs->blocksize)) in ext2fs_file_set_size2()
458 truncate_block = ((size + file->fs->blocksize - 1) >> in ext2fs_file_set_size2()
459 EXT2_BLOCK_SIZE_BITS(file->fs->super)); in ext2fs_file_set_size2()
460 old_size = EXT2_I_SIZE(&file->inode); in ext2fs_file_set_size2()
461 old_truncate = ((old_size + file->fs->blocksize - 1) >> in ext2fs_file_set_size2()
462 EXT2_BLOCK_SIZE_BITS(file->fs->super)); in ext2fs_file_set_size2()
464 /* If we're writing a large file, set the large_file flag */ in ext2fs_file_set_size2()
465 if (LINUX_S_ISREG(file->inode.i_mode) && in ext2fs_file_set_size2()
466 ext2fs_needs_large_file_feature(EXT2_I_SIZE(&file->inode)) && in ext2fs_file_set_size2()
467 (!EXT2_HAS_RO_COMPAT_FEATURE(file->fs->super, in ext2fs_file_set_size2()
469 file->fs->super->s_rev_level == EXT2_GOOD_OLD_REV)) { in ext2fs_file_set_size2()
470 file->fs->super->s_feature_ro_compat |= in ext2fs_file_set_size2()
472 ext2fs_update_dynamic_rev(file->fs); in ext2fs_file_set_size2()
473 ext2fs_mark_super_dirty(file->fs); in ext2fs_file_set_size2()
476 file->inode.i_size = size & 0xffffffff; in ext2fs_file_set_size2()
477 file->inode.i_size_high = (size >> 32); in ext2fs_file_set_size2()
478 if (file->ino) { in ext2fs_file_set_size2()
479 retval = ext2fs_write_inode(file->fs, file->ino, &file->inode); in ext2fs_file_set_size2()
484 retval = ext2fs_file_zero_past_offset(file, size); in ext2fs_file_set_size2()
491 return ext2fs_punch(file->fs, file->ino, &file->inode, 0, in ext2fs_file_set_size2()
495 errcode_t ext2fs_file_set_size(ext2_file_t file, ext2_off_t size) in ext2fs_file_set_size() argument
497 return ext2fs_file_set_size2(file, size); in ext2fs_file_set_size()