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