blob: c86a1c59a358b365968d6dca528f9f306c184afd [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger1fd98e02005-05-09 22:10:42 +00002/*
3 * ind_block.c --- indirect block I/O routines
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00004 *
5 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
6 * 2001, 2002, 2003, 2004, 2005 by Theodore Ts'o.
Mike Frysinger1fd98e02005-05-09 22:10:42 +00007 *
8 * %Begin-Header%
9 * This file may be redistributed under the terms of the GNU Public
10 * License.
11 * %End-Header%
12 */
13
14#include <stdio.h>
15#include <string.h>
16#if HAVE_UNISTD_H
17#include <unistd.h>
18#endif
19
20#include "ext2_fs.h"
21#include "ext2fs.h"
22
23errcode_t ext2fs_read_ind_block(ext2_filsys fs, blk_t blk, void *buf)
24{
25 errcode_t retval;
Rob Landley7c94bed2006-05-03 21:58:45 +000026#if BB_BIG_ENDIAN
Mike Frysinger1fd98e02005-05-09 22:10:42 +000027 blk_t *block_nr;
28 int i;
29 int limit = fs->blocksize >> 2;
Mike Frysinger0301ffa2005-06-11 00:11:37 +000030#endif
Mike Frysinger1fd98e02005-05-09 22:10:42 +000031
32 if ((fs->flags & EXT2_FLAG_IMAGE_FILE) &&
33 (fs->io != fs->image_io))
34 memset(buf, 0, fs->blocksize);
35 else {
36 retval = io_channel_read_blk(fs->io, blk, 1, buf);
37 if (retval)
38 return retval;
39 }
Rob Landley7c94bed2006-05-03 21:58:45 +000040#if BB_BIG_ENDIAN
Mike Frysinger1fd98e02005-05-09 22:10:42 +000041 if (fs->flags & (EXT2_FLAG_SWAP_BYTES | EXT2_FLAG_SWAP_BYTES_READ)) {
42 block_nr = (blk_t *) buf;
43 for (i = 0; i < limit; i++, block_nr++)
44 *block_nr = ext2fs_swab32(*block_nr);
45 }
46#endif
47 return 0;
48}
49
50errcode_t ext2fs_write_ind_block(ext2_filsys fs, blk_t blk, void *buf)
51{
Rob Landley7c94bed2006-05-03 21:58:45 +000052#if BB_BIG_ENDIAN
Mike Frysinger1fd98e02005-05-09 22:10:42 +000053 blk_t *block_nr;
54 int i;
55 int limit = fs->blocksize >> 2;
Mike Frysinger0301ffa2005-06-11 00:11:37 +000056#endif
Mike Frysinger1fd98e02005-05-09 22:10:42 +000057
58 if (fs->flags & EXT2_FLAG_IMAGE_FILE)
59 return 0;
60
Rob Landley7c94bed2006-05-03 21:58:45 +000061#if BB_BIG_ENDIAN
Mike Frysinger1fd98e02005-05-09 22:10:42 +000062 if (fs->flags & (EXT2_FLAG_SWAP_BYTES | EXT2_FLAG_SWAP_BYTES_WRITE)) {
63 block_nr = (blk_t *) buf;
64 for (i = 0; i < limit; i++, block_nr++)
65 *block_nr = ext2fs_swab32(*block_nr);
66 }
67#endif
68 return io_channel_write_blk(fs->io, blk, 1, buf);
69}
70
71