1997-10-26 02:37:42 +04:00
|
|
|
/*
|
|
|
|
* fileio.c --- Simple file I/O routines
|
|
|
|
*
|
|
|
|
* Copyright (C) 1997 Theodore Ts'o.
|
|
|
|
*
|
|
|
|
* %Begin-Header%
|
|
|
|
* This file may be redistributed under the terms of the GNU Public
|
|
|
|
* License.
|
|
|
|
* %End-Header%
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
Many files:
inode.c (ext2fs_open_inode_scan): Initialize the group variables
so that we don't need to call get_next_blockgroup() the first
time around. Saves a bit of time, and prevents us from
needing to assign -1 to current_group (which is an unsigned
value).
icount.c (insert_icount_el): Cast the estimated number of inodes
from a float to an ino_t.
alloc.c, alloc_tables.c, badlbocks.c, bb_compat.c, bb_inode.c,
bitmaps.c, bitops.c, block.c, bmap.c, bmove.c, brel_ma.c,
check_desc.c, closefs.c, cmp_bitmaps.c, dblist.c,
dblist_dir.c, dir_iterate.c, dirblock.c, dupfs.c, expanddir.c,
ext2fs.h, fileio.c, freefs.c, get_pathname.c, getsize.c,
icount.c, initialize.c, inline.c, inode.c, irel_ma.c,
ismounted.c, link.c, lookup.c, mkdir.c, namei.c, native.c,
newdir.c, openfs.c, read_bb.c, read_bb_file.c, rs_bitmap.c,
rw_bitmaps.c, swapfs.c, test_io.c, tst_badblocks.c,
tst_getsize.c, tst_iscan.c, unix_io.c, unlink.c, valid_blk.c,
version.c: If EXT2_FLAT_INCLUDES is defined, then assume all
of the ext2-specific header files are in a flat directory.
block.c, bmove.c, dirblock.c, fileio.c: Explicitly cast
all assignments from void * to be compatible with C++.
closefs.c (ext2fs_flush): Add a call to io_channel_flush() to
make sure the contents of the disk are flushed to disk.
dblist.c (ext2fs_add_dir_block): Change new to be new_entry to
avoid C++ namespace clash.
bitmaps.c (ext2fs_copy_bitmap): Change new to be new_map to
avoid C++ namespace clash.
ext2fs.h, bb_inode.c, block.c, bmove.c, brel.h, brel_ma.c,
irel.h, irel_ma.c, dblist.c, dblist_dir.c, dir_iterate.c,
ext2fsP.h, expanddir.c, get_pathname.c, inode.c, link.c,
unlink.c: Change private to be priv_data (to avoid C++
namespace clash)
1998-01-19 17:47:53 +03:00
|
|
|
#include "ext2_fs.h"
|
1997-10-26 02:37:42 +04:00
|
|
|
#include "ext2fs.h"
|
|
|
|
|
|
|
|
struct ext2_file {
|
|
|
|
errcode_t magic;
|
|
|
|
ext2_filsys fs;
|
Many files:
alloc.c, bb_inode.c, bitmaps.c, bitops.h, block.c, bmap.c, bmove.c,
brel.h, cmp_bitmaps.c, dblist.c, dblist_dir.c, dir_iterate.c,
expanddir.c, ext2fs.h, ext2fsP.h, fileio.c, finddev.c, get_pathname.c,
icount.c, inode.c, irel.h, irel_ma.c, ismounted.c, link.c, lookup.c,
mkdir.c, mkjournal.c, namei.c, newdir.c, read_bb_file.c, test_io.c,
tst_iscan.c, unix_io.c, unlink.c: Change use of ino_t to ext2_ino_t,
to protect applications that attempt to compile
-D_FILE_OFFSET_BITS=64, since this inexplicably changes ino_t(!?). So
we use ext2_ino_t to avoid an unexpected ABI change.
2001-01-11 07:54:39 +03:00
|
|
|
ext2_ino_t ino;
|
1997-10-26 02:37:42 +04:00
|
|
|
struct ext2_inode inode;
|
|
|
|
int flags;
|
|
|
|
ext2_off_t pos;
|
|
|
|
blk_t blockno;
|
|
|
|
blk_t physblock;
|
|
|
|
char *buf;
|
|
|
|
};
|
|
|
|
|
2001-05-05 20:42:24 +04:00
|
|
|
#define BMAP_BUFFER (file->buf + fs->blocksize)
|
|
|
|
|
Many files:
alloc.c, bb_inode.c, bitmaps.c, bitops.h, block.c, bmap.c, bmove.c,
brel.h, cmp_bitmaps.c, dblist.c, dblist_dir.c, dir_iterate.c,
expanddir.c, ext2fs.h, ext2fsP.h, fileio.c, finddev.c, get_pathname.c,
icount.c, inode.c, irel.h, irel_ma.c, ismounted.c, link.c, lookup.c,
mkdir.c, mkjournal.c, namei.c, newdir.c, read_bb_file.c, test_io.c,
tst_iscan.c, unix_io.c, unlink.c: Change use of ino_t to ext2_ino_t,
to protect applications that attempt to compile
-D_FILE_OFFSET_BITS=64, since this inexplicably changes ino_t(!?). So
we use ext2_ino_t to avoid an unexpected ABI change.
2001-01-11 07:54:39 +03:00
|
|
|
errcode_t ext2fs_file_open(ext2_filsys fs, ext2_ino_t ino,
|
1997-10-26 02:37:42 +04:00
|
|
|
int flags, ext2_file_t *ret)
|
|
|
|
{
|
|
|
|
ext2_file_t file;
|
|
|
|
errcode_t retval;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't let caller create or open a file for writing if the
|
|
|
|
* filesystem is read-only.
|
|
|
|
*/
|
|
|
|
if ((flags & (EXT2_FILE_WRITE | EXT2_FILE_CREATE)) &&
|
|
|
|
!(fs->flags & EXT2_FLAG_RW))
|
|
|
|
return EXT2_ET_RO_FILSYS;
|
|
|
|
|
1997-10-26 06:41:24 +03:00
|
|
|
retval = ext2fs_get_mem(sizeof(struct ext2_file), (void **) &file);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
1997-10-26 02:37:42 +04:00
|
|
|
|
|
|
|
memset(file, 0, sizeof(struct ext2_file));
|
|
|
|
file->magic = EXT2_ET_MAGIC_EXT2_FILE;
|
|
|
|
file->fs = fs;
|
|
|
|
file->ino = ino;
|
|
|
|
file->flags = flags & EXT2_FILE_MASK;
|
|
|
|
|
|
|
|
retval = ext2fs_read_inode(fs, ino, &file->inode);
|
|
|
|
if (retval)
|
|
|
|
goto fail;
|
|
|
|
|
2001-05-05 20:42:24 +04:00
|
|
|
retval = ext2fs_get_mem(fs->blocksize * 3, (void **) &file->buf);
|
1997-10-26 06:41:24 +03:00
|
|
|
if (retval)
|
1997-10-26 02:37:42 +04:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
*ret = file;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (file->buf)
|
1997-10-26 06:41:24 +03:00
|
|
|
ext2fs_free_mem((void **) &file->buf);
|
|
|
|
ext2fs_free_mem((void **) &file);
|
1997-10-26 02:37:42 +04:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
1997-11-03 22:16:55 +03:00
|
|
|
/*
|
|
|
|
* This function returns the filesystem handle of a file from the structure
|
|
|
|
*/
|
|
|
|
ext2_filsys ext2fs_file_get_fs(ext2_file_t file)
|
|
|
|
{
|
|
|
|
if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
|
|
|
|
return 0;
|
|
|
|
return file->fs;
|
|
|
|
}
|
|
|
|
|
1997-10-26 02:37:42 +04:00
|
|
|
/*
|
|
|
|
* This function flushes the dirty block buffer out to disk if
|
|
|
|
* necessary.
|
|
|
|
*/
|
|
|
|
static errcode_t ext2fs_file_flush(ext2_file_t file)
|
|
|
|
{
|
|
|
|
errcode_t retval;
|
2001-05-05 20:42:24 +04:00
|
|
|
ext2_filsys fs;
|
1997-10-26 02:37:42 +04:00
|
|
|
|
|
|
|
EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
|
2001-05-05 20:42:24 +04:00
|
|
|
fs = file->fs;
|
1997-10-26 02:37:42 +04:00
|
|
|
|
|
|
|
if (!(file->flags & EXT2_FILE_BUF_VALID) ||
|
|
|
|
!(file->flags & EXT2_FILE_BUF_DIRTY))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* OK, the physical block hasn't been allocated yet.
|
|
|
|
* Allocate it.
|
|
|
|
*/
|
|
|
|
if (!file->physblock) {
|
2001-05-05 20:42:24 +04:00
|
|
|
retval = ext2fs_bmap(fs, file->ino, &file->inode,
|
|
|
|
BMAP_BUFFER, BMAP_ALLOC,
|
1997-10-26 02:37:42 +04:00
|
|
|
file->blockno, &file->physblock);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2001-05-05 20:42:24 +04:00
|
|
|
retval = io_channel_write_blk(fs->io, file->physblock,
|
1997-10-26 02:37:42 +04:00
|
|
|
1, file->buf);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
file->flags &= ~EXT2_FILE_BUF_DIRTY;
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2001-05-05 20:42:24 +04:00
|
|
|
/*
|
|
|
|
* This function synchronizes the file's block buffer and the current
|
|
|
|
* file position, possibly invalidating block buffer if necessary
|
|
|
|
*/
|
|
|
|
static errcode_t sync_buffer_position(ext2_file_t file)
|
|
|
|
{
|
|
|
|
blk_t b;
|
|
|
|
errcode_t retval;
|
|
|
|
|
|
|
|
b = file->pos / file->fs->blocksize;
|
|
|
|
if (b != file->blockno) {
|
|
|
|
retval = ext2fs_file_flush(file);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
file->flags &= ~EXT2_FILE_BUF_VALID;
|
|
|
|
}
|
|
|
|
file->blockno = b;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function loads the file's block buffer with valid data from
|
|
|
|
* the disk as necessary.
|
|
|
|
*
|
|
|
|
* If dontfill is true, then skip initializing the buffer since we're
|
|
|
|
* going to be replacing its entire contents anyway. If set, then the
|
|
|
|
* function basically only sets file->physblock and EXT2_FILE_BUF_VALID
|
|
|
|
*/
|
|
|
|
#define DONTFILL 1
|
|
|
|
static errcode_t load_buffer(ext2_file_t file, int dontfill)
|
|
|
|
{
|
|
|
|
ext2_filsys fs = file->fs;
|
|
|
|
errcode_t retval;
|
|
|
|
|
|
|
|
if (!(file->flags & EXT2_FILE_BUF_VALID)) {
|
|
|
|
retval = ext2fs_bmap(fs, file->ino, &file->inode,
|
|
|
|
BMAP_BUFFER, 0, file->blockno,
|
|
|
|
&file->physblock);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
if (!dontfill) {
|
|
|
|
if (file->physblock) {
|
|
|
|
retval = io_channel_read_blk(fs->io,
|
|
|
|
file->physblock,
|
|
|
|
1, file->buf);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
} else
|
|
|
|
memset(file->buf, 0, fs->blocksize);
|
|
|
|
}
|
|
|
|
file->flags |= EXT2_FILE_BUF_VALID;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-10-26 02:37:42 +04:00
|
|
|
errcode_t ext2fs_file_close(ext2_file_t file)
|
|
|
|
{
|
|
|
|
errcode_t retval;
|
|
|
|
|
|
|
|
EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
|
|
|
|
|
|
|
|
retval = ext2fs_file_flush(file);
|
|
|
|
|
|
|
|
if (file->buf)
|
1997-10-26 06:41:24 +03:00
|
|
|
ext2fs_free_mem((void **) &file->buf);
|
|
|
|
ext2fs_free_mem((void **) &file);
|
1997-10-26 02:37:42 +04:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
errcode_t ext2fs_file_read(ext2_file_t file, void *buf,
|
1997-11-03 22:16:55 +03:00
|
|
|
unsigned int wanted, unsigned int *got)
|
1997-10-26 02:37:42 +04:00
|
|
|
{
|
|
|
|
ext2_filsys fs;
|
2001-05-05 20:42:24 +04:00
|
|
|
errcode_t retval = 0;
|
1997-11-03 22:16:55 +03:00
|
|
|
unsigned int start, left, c, count = 0;
|
Many files:
inode.c (ext2fs_open_inode_scan): Initialize the group variables
so that we don't need to call get_next_blockgroup() the first
time around. Saves a bit of time, and prevents us from
needing to assign -1 to current_group (which is an unsigned
value).
icount.c (insert_icount_el): Cast the estimated number of inodes
from a float to an ino_t.
alloc.c, alloc_tables.c, badlbocks.c, bb_compat.c, bb_inode.c,
bitmaps.c, bitops.c, block.c, bmap.c, bmove.c, brel_ma.c,
check_desc.c, closefs.c, cmp_bitmaps.c, dblist.c,
dblist_dir.c, dir_iterate.c, dirblock.c, dupfs.c, expanddir.c,
ext2fs.h, fileio.c, freefs.c, get_pathname.c, getsize.c,
icount.c, initialize.c, inline.c, inode.c, irel_ma.c,
ismounted.c, link.c, lookup.c, mkdir.c, namei.c, native.c,
newdir.c, openfs.c, read_bb.c, read_bb_file.c, rs_bitmap.c,
rw_bitmaps.c, swapfs.c, test_io.c, tst_badblocks.c,
tst_getsize.c, tst_iscan.c, unix_io.c, unlink.c, valid_blk.c,
version.c: If EXT2_FLAT_INCLUDES is defined, then assume all
of the ext2-specific header files are in a flat directory.
block.c, bmove.c, dirblock.c, fileio.c: Explicitly cast
all assignments from void * to be compatible with C++.
closefs.c (ext2fs_flush): Add a call to io_channel_flush() to
make sure the contents of the disk are flushed to disk.
dblist.c (ext2fs_add_dir_block): Change new to be new_entry to
avoid C++ namespace clash.
bitmaps.c (ext2fs_copy_bitmap): Change new to be new_map to
avoid C++ namespace clash.
ext2fs.h, bb_inode.c, block.c, bmove.c, brel.h, brel_ma.c,
irel.h, irel_ma.c, dblist.c, dblist_dir.c, dir_iterate.c,
ext2fsP.h, expanddir.c, get_pathname.c, inode.c, link.c,
unlink.c: Change private to be priv_data (to avoid C++
namespace clash)
1998-01-19 17:47:53 +03:00
|
|
|
char *ptr = (char *) buf;
|
1997-10-26 02:37:42 +04:00
|
|
|
|
|
|
|
EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
|
|
|
|
fs = file->fs;
|
|
|
|
|
2001-05-05 20:42:24 +04:00
|
|
|
while ((file->pos < file->inode.i_size) && (wanted > 0)) {
|
|
|
|
retval = sync_buffer_position(file);
|
1997-10-26 02:37:42 +04:00
|
|
|
if (retval)
|
|
|
|
goto fail;
|
2001-05-05 20:42:24 +04:00
|
|
|
retval = load_buffer(file, 0);
|
1997-10-26 02:37:42 +04:00
|
|
|
if (retval)
|
|
|
|
goto fail;
|
2001-05-05 20:42:24 +04:00
|
|
|
|
|
|
|
start = file->pos % fs->blocksize;
|
|
|
|
c = fs->blocksize - start;
|
|
|
|
if (c > wanted)
|
|
|
|
c = wanted;
|
|
|
|
left = file->inode.i_size - file->pos ;
|
|
|
|
if (c > left)
|
|
|
|
c = left;
|
|
|
|
|
|
|
|
memcpy(ptr, file->buf+start, c);
|
|
|
|
file->pos += c;
|
|
|
|
ptr += c;
|
|
|
|
count += c;
|
|
|
|
wanted -= c;
|
1997-10-26 02:37:42 +04:00
|
|
|
}
|
|
|
|
|
2001-05-05 20:42:24 +04:00
|
|
|
fail:
|
1997-10-26 02:37:42 +04:00
|
|
|
if (got)
|
|
|
|
*got = count;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
errcode_t ext2fs_file_write(ext2_file_t file, void *buf,
|
1997-11-03 22:16:55 +03:00
|
|
|
unsigned int nbytes, unsigned int *written)
|
1997-10-26 02:37:42 +04:00
|
|
|
{
|
|
|
|
ext2_filsys fs;
|
2001-05-05 20:42:24 +04:00
|
|
|
errcode_t retval = 0;
|
|
|
|
unsigned int start, c, count = 0;
|
Many files:
inode.c (ext2fs_open_inode_scan): Initialize the group variables
so that we don't need to call get_next_blockgroup() the first
time around. Saves a bit of time, and prevents us from
needing to assign -1 to current_group (which is an unsigned
value).
icount.c (insert_icount_el): Cast the estimated number of inodes
from a float to an ino_t.
alloc.c, alloc_tables.c, badlbocks.c, bb_compat.c, bb_inode.c,
bitmaps.c, bitops.c, block.c, bmap.c, bmove.c, brel_ma.c,
check_desc.c, closefs.c, cmp_bitmaps.c, dblist.c,
dblist_dir.c, dir_iterate.c, dirblock.c, dupfs.c, expanddir.c,
ext2fs.h, fileio.c, freefs.c, get_pathname.c, getsize.c,
icount.c, initialize.c, inline.c, inode.c, irel_ma.c,
ismounted.c, link.c, lookup.c, mkdir.c, namei.c, native.c,
newdir.c, openfs.c, read_bb.c, read_bb_file.c, rs_bitmap.c,
rw_bitmaps.c, swapfs.c, test_io.c, tst_badblocks.c,
tst_getsize.c, tst_iscan.c, unix_io.c, unlink.c, valid_blk.c,
version.c: If EXT2_FLAT_INCLUDES is defined, then assume all
of the ext2-specific header files are in a flat directory.
block.c, bmove.c, dirblock.c, fileio.c: Explicitly cast
all assignments from void * to be compatible with C++.
closefs.c (ext2fs_flush): Add a call to io_channel_flush() to
make sure the contents of the disk are flushed to disk.
dblist.c (ext2fs_add_dir_block): Change new to be new_entry to
avoid C++ namespace clash.
bitmaps.c (ext2fs_copy_bitmap): Change new to be new_map to
avoid C++ namespace clash.
ext2fs.h, bb_inode.c, block.c, bmove.c, brel.h, brel_ma.c,
irel.h, irel_ma.c, dblist.c, dblist_dir.c, dir_iterate.c,
ext2fsP.h, expanddir.c, get_pathname.c, inode.c, link.c,
unlink.c: Change private to be priv_data (to avoid C++
namespace clash)
1998-01-19 17:47:53 +03:00
|
|
|
char *ptr = (char *) buf;
|
1997-10-26 02:37:42 +04:00
|
|
|
|
|
|
|
EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
|
|
|
|
fs = file->fs;
|
|
|
|
|
|
|
|
if (!(file->flags & EXT2_FILE_WRITE))
|
1997-10-31 09:07:47 +03:00
|
|
|
return EXT2_ET_FILE_RO;
|
1997-10-26 02:37:42 +04:00
|
|
|
|
2001-05-05 20:42:24 +04:00
|
|
|
while (nbytes > 0) {
|
|
|
|
retval = sync_buffer_position(file);
|
1997-10-26 02:37:42 +04:00
|
|
|
if (retval)
|
|
|
|
goto fail;
|
|
|
|
|
2001-05-05 20:42:24 +04:00
|
|
|
start = file->pos % fs->blocksize;
|
|
|
|
c = fs->blocksize - start;
|
|
|
|
if (c > nbytes)
|
|
|
|
c = nbytes;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We only need to do a read-modify-update cycle if
|
|
|
|
* we're doing a partial write.
|
|
|
|
*/
|
|
|
|
retval = load_buffer(file, (c == fs->blocksize));
|
1997-10-26 02:37:42 +04:00
|
|
|
if (retval)
|
|
|
|
goto fail;
|
2001-05-05 20:42:24 +04:00
|
|
|
|
|
|
|
file->flags |= EXT2_FILE_BUF_DIRTY;
|
|
|
|
memcpy(file->buf+start, ptr, c);
|
|
|
|
file->pos += c;
|
|
|
|
ptr += c;
|
|
|
|
count += c;
|
|
|
|
nbytes -= c;
|
1997-10-26 02:37:42 +04:00
|
|
|
}
|
|
|
|
|
2001-05-05 20:42:24 +04:00
|
|
|
fail:
|
1997-10-26 02:37:42 +04:00
|
|
|
if (written)
|
|
|
|
*written = count;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
Many files:
ext2fs.h: Add new superblock fields (s_algorithm_usage_bitmap,
s_prealloc_blocks, s_prealloc_dir_blocks). Added conditional defines
of new features COMPAT_DIR_PREALLOC, RO_COMPAT_LARGE_FILE
RO_COMPAT_BTREE_DIR, INCOMPAT_COMPRESSION, INCOMPAT_DIRNAME_SIZE.
Changed the library to declare that we support COMPAT_DIR_PREALLOC,
INCOMPAT_DIRNAME_SIZE, RO_COMPAT_LARGE_FILE.
fileio.c: Rename function ext2fs_file_llseek to be ext2fs_file_lseek,
which is more accurate.
block.c: Add new function ext2fs_block_iterate3 which calls the
iterator function with the blockcount argument of type blkcnt_t. This
version of the function is allowed to handle large files; the other
fucntions are not.
ext2fs.h: Add new type blkcnt_t
ext2_err.et.in: Add error code EXT2_ET_FILE_TOO_BIG
block.c (ext2fs_block_iterate2): Fix bug where the block count field
wasn't getting correctly incremented for sparse files when the
indirect or doubly-indirect block specified in the inode was zero.
unlink.c (unlink_proc):
lookup.c (lookup_proc):
link.c (link_proc):
get_pathname.c (get_pathname_proc):
dir_iterate.c (ext2fs_process_dir_block): Mask off high 8 bits from
dirent->name_len, so it can be used for other purposes.
ext2fs.h: Add definition of EXT2_FEATURE_INCOMPAT_DIRNAME_SIZE, and
indicate that we have support for this incompatible option.
1998-03-23 05:06:52 +03:00
|
|
|
errcode_t ext2fs_file_lseek(ext2_file_t file, ext2_off_t offset,
|
|
|
|
int whence, ext2_off_t *ret_pos)
|
1997-10-26 02:37:42 +04:00
|
|
|
{
|
|
|
|
EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
|
|
|
|
|
|
|
|
if (whence == EXT2_SEEK_SET)
|
|
|
|
file->pos = offset;
|
|
|
|
else if (whence == EXT2_SEEK_CUR)
|
|
|
|
file->pos += offset;
|
|
|
|
else if (whence == EXT2_SEEK_END)
|
|
|
|
file->pos = file->inode.i_size + offset;
|
|
|
|
else
|
1997-10-31 09:07:47 +03:00
|
|
|
return EXT2_ET_INVALID_ARGUMENT;
|
1997-10-26 02:37:42 +04:00
|
|
|
|
|
|
|
if (ret_pos)
|
|
|
|
*ret_pos = file->pos;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-11-03 22:16:55 +03:00
|
|
|
/*
|
|
|
|
* This function returns the size of the file, according to the inode
|
|
|
|
*/
|
|
|
|
ext2_off_t ext2fs_file_get_size(ext2_file_t file)
|
|
|
|
{
|
|
|
|
if (file->magic != EXT2_ET_MAGIC_EXT2_FILE)
|
|
|
|
return 0;
|
|
|
|
return file->inode.i_size;
|
|
|
|
}
|
1997-10-26 02:37:42 +04:00
|
|
|
|
1997-11-03 22:16:55 +03:00
|
|
|
/*
|
|
|
|
* This function sets the size of the file, truncating it if necessary
|
|
|
|
*
|
|
|
|
* XXX still need to call truncate
|
|
|
|
*/
|
1998-09-03 05:22:57 +04:00
|
|
|
errcode_t ext2fs_file_set_size(ext2_file_t file, ext2_off_t size)
|
1997-11-03 22:16:55 +03:00
|
|
|
{
|
|
|
|
errcode_t retval;
|
|
|
|
EXT2_CHECK_MAGIC(file, EXT2_ET_MAGIC_EXT2_FILE);
|
|
|
|
|
|
|
|
file->inode.i_size = size;
|
|
|
|
retval = ext2fs_write_inode(file->fs, file->ino, &file->inode);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX truncate inode if necessary
|
|
|
|
*/
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|