2 * fileio.c --- Simple file I/O routines
4 * Copyright (C) 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
25 struct ext2_inode inode;
33 #define BMAP_BUFFER (file->buf + fs->blocksize)
35 errcode_t ext2fs_file_open(ext2_filsys fs, ext2_ino_t ino,
36 int flags, ext2_file_t *ret)
42 * Don't let caller create or open a file for writing if the
43 * filesystem is read-only.
45 if ((flags & (EXT2_FILE_WRITE | EXT2_FILE_CREATE)) &&
46 !(fs->flags & EXT2_FLAG_RW))
47 return EXT2_ET_RO_FILSYS;
49 retval = ext2fs_get_mem(sizeof(struct ext2_file), (void **) &file);
53 memset(file, 0, sizeof(struct ext2_file));
54 file->magic = EXT2_ET_MAGIC_EXT2_FILE;
57 file->flags = flags & EXT2_FILE_MASK;
59 retval = ext2fs_read_inode(fs, ino, &file->inode);
63 retval = ext2fs_get_mem(fs->blocksize * 3, (void **) &file->buf);
72 ext2fs_free_mem((void **) &file->buf);
73 ext2fs_free_mem((void **) &file);
78 * This function returns the filesystem handle of a file from the structure
80 ext2_filsys ext2fs_file_get_fs(ext2_file_t file)
82 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
88 * This function flushes the dirty block buffer out to disk if
91 errcode_t ext2fs_file_flush(ext2_file_t file)
96 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
99 if (!(file->flags & EXT2_FILE_BUF_VALID) ||
100 !(file->flags & EXT2_FILE_BUF_DIRTY))
104 * OK, the physical block hasn't been allocated yet.
107 if (!file->physblock) {
108 retval = ext2fs_bmap(fs, file->ino, &file->inode,
109 BMAP_BUFFER, BMAP_ALLOC,
110 file->blockno, &file->physblock);
115 retval = io_channel_write_blk(fs->io, file->physblock,
120 file->flags &= ~EXT2_FILE_BUF_DIRTY;
126 * This function synchronizes the file's block buffer and the current
127 * file position, possibly invalidating block buffer if necessary
129 static errcode_t sync_buffer_position(ext2_file_t file)
134 b = file->pos / file->fs->blocksize;
135 if (b != file->blockno) {
136 retval = ext2fs_file_flush(file);
139 file->flags &= ~EXT2_FILE_BUF_VALID;
146 * This function loads the file's block buffer with valid data from
147 * the disk as necessary.
149 * If dontfill is true, then skip initializing the buffer since we're
150 * going to be replacing its entire contents anyway. If set, then the
151 * function basically only sets file->physblock and EXT2_FILE_BUF_VALID
154 static errcode_t load_buffer(ext2_file_t file, int dontfill)
156 ext2_filsys fs = file->fs;
159 if (!(file->flags & EXT2_FILE_BUF_VALID)) {
160 retval = ext2fs_bmap(fs, file->ino, &file->inode,
161 BMAP_BUFFER, 0, file->blockno,
166 if (file->physblock) {
167 retval = io_channel_read_blk(fs->io,
173 memset(file->buf, 0, fs->blocksize);
175 file->flags |= EXT2_FILE_BUF_VALID;
181 errcode_t ext2fs_file_close(ext2_file_t file)
185 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
187 retval = ext2fs_file_flush(file);
190 ext2fs_free_mem((void **) &file->buf);
191 ext2fs_free_mem((void **) &file);
197 errcode_t ext2fs_file_read(ext2_file_t file, void *buf,
198 unsigned int wanted, unsigned int *got)
201 errcode_t retval = 0;
202 unsigned int start, left, c, count = 0;
203 char *ptr = (char *) buf;
205 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
208 while ((file->pos < file->inode.i_size) && (wanted > 0)) {
209 retval = sync_buffer_position(file);
212 retval = load_buffer(file, 0);
216 start = file->pos % fs->blocksize;
217 c = fs->blocksize - start;
220 left = file->inode.i_size - file->pos ;
224 memcpy(ptr, file->buf+start, c);
238 errcode_t ext2fs_file_write(ext2_file_t file, const void *buf,
239 unsigned int nbytes, unsigned int *written)
242 errcode_t retval = 0;
243 unsigned int start, c, count = 0;
244 char *ptr = (char *) buf;
246 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
249 if (!(file->flags & EXT2_FILE_WRITE))
250 return EXT2_ET_FILE_RO;
253 retval = sync_buffer_position(file);
257 start = file->pos % fs->blocksize;
258 c = fs->blocksize - start;
263 * We only need to do a read-modify-update cycle if
264 * we're doing a partial write.
266 retval = load_buffer(file, (c == fs->blocksize));
270 file->flags |= EXT2_FILE_BUF_DIRTY;
271 memcpy(file->buf+start, ptr, c);
284 errcode_t ext2fs_file_lseek(ext2_file_t file, ext2_off_t offset,
285 int whence, ext2_off_t *ret_pos)
287 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
289 if (whence == EXT2_SEEK_SET)
291 else if (whence == EXT2_SEEK_CUR)
293 else if (whence == EXT2_SEEK_END)
294 file->pos = file->inode.i_size + offset;
296 return EXT2_ET_INVALID_ARGUMENT;
299 *ret_pos = file->pos;
305 * This function returns the size of the file, according to the inode
307 ext2_off_t ext2fs_file_get_size(ext2_file_t file)
309 if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
311 return file->inode.i_size;
315 * This function sets the size of the file, truncating it if necessary
317 * XXX still need to call truncate
319 errcode_t ext2fs_file_set_size(ext2_file_t file, ext2_off_t size)
322 EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
324 file->inode.i_size = size;
325 retval = ext2fs_write_inode(file->fs, file->ino, &file->inode);
330 * XXX truncate inode if necessary