"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 2 | /* |
| 3 | * ext_attr.c --- extended attribute blocks |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 4 | * |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 5 | * Copyright (C) 2001 Andreas Gruenbacher, <a.gruenbacher@computer.org> |
| 6 | * |
| 7 | * Copyright (C) 2002 Theodore Ts'o. |
| 8 | * |
| 9 | * %Begin-Header% |
| 10 | * This file may be redistributed under the terms of the GNU Public |
| 11 | * License. |
| 12 | * %End-Header% |
| 13 | */ |
| 14 | |
| 15 | #include <stdio.h> |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 16 | #include <unistd.h> |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 17 | #include <string.h> |
| 18 | #include <time.h> |
| 19 | |
| 20 | #include "ext2_fs.h" |
| 21 | #include "ext2_ext_attr.h" |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 22 | #include "ext2fs.h" |
| 23 | |
| 24 | errcode_t ext2fs_read_ext_attr(ext2_filsys fs, blk_t block, void *buf) |
| 25 | { |
| 26 | errcode_t retval; |
| 27 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 28 | retval = io_channel_read_blk(fs->io, block, 1, buf); |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 29 | if (retval) |
| 30 | return retval; |
Rob Landley | 7c94bed | 2006-05-03 21:58:45 +0000 | [diff] [blame] | 31 | #if BB_BIG_ENDIAN |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 32 | if ((fs->flags & (EXT2_FLAG_SWAP_BYTES| |
| 33 | EXT2_FLAG_SWAP_BYTES_READ)) != 0) |
| 34 | ext2fs_swap_ext_attr(buf, buf, fs->blocksize, 1); |
| 35 | #endif |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | errcode_t ext2fs_write_ext_attr(ext2_filsys fs, blk_t block, void *inbuf) |
| 40 | { |
| 41 | errcode_t retval; |
| 42 | char *write_buf; |
| 43 | char *buf = NULL; |
| 44 | |
Rob Landley | 11c7a7b | 2006-06-25 22:39:24 +0000 | [diff] [blame] | 45 | if (BB_BIG_ENDIAN && ((fs->flags & EXT2_FLAG_SWAP_BYTES) || |
| 46 | (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))) { |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 47 | retval = ext2fs_get_mem(fs->blocksize, &buf); |
| 48 | if (retval) |
| 49 | return retval; |
| 50 | write_buf = buf; |
| 51 | ext2fs_swap_ext_attr(buf, inbuf, fs->blocksize, 1); |
| 52 | } else |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 53 | write_buf = (char *) inbuf; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 54 | retval = io_channel_write_blk(fs->io, block, 1, write_buf); |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 55 | if (buf) |
| 56 | ext2fs_free_mem(&buf); |
| 57 | if (!retval) |
| 58 | ext2fs_mark_changed(fs); |
| 59 | return retval; |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * This function adjusts the reference count of the EA block. |
| 64 | */ |
| 65 | errcode_t ext2fs_adjust_ea_refcount(ext2_filsys fs, blk_t blk, |
| 66 | char *block_buf, int adjust, |
| 67 | __u32 *newcount) |
| 68 | { |
| 69 | errcode_t retval; |
| 70 | struct ext2_ext_attr_header *header; |
| 71 | char *buf = 0; |
| 72 | |
| 73 | if ((blk >= fs->super->s_blocks_count) || |
| 74 | (blk < fs->super->s_first_data_block)) |
| 75 | return EXT2_ET_BAD_EA_BLOCK_NUM; |
| 76 | |
| 77 | if (!block_buf) { |
| 78 | retval = ext2fs_get_mem(fs->blocksize, &buf); |
| 79 | if (retval) |
| 80 | return retval; |
| 81 | block_buf = buf; |
| 82 | } |
| 83 | |
| 84 | retval = ext2fs_read_ext_attr(fs, blk, block_buf); |
| 85 | if (retval) |
| 86 | goto errout; |
| 87 | |
| 88 | header = (struct ext2_ext_attr_header *) block_buf; |
| 89 | header->h_refcount += adjust; |
| 90 | if (newcount) |
| 91 | *newcount = header->h_refcount; |
| 92 | |
| 93 | retval = ext2fs_write_ext_attr(fs, blk, block_buf); |
| 94 | if (retval) |
| 95 | goto errout; |
| 96 | |
| 97 | errout: |
| 98 | if (buf) |
| 99 | ext2fs_free_mem(&buf); |
| 100 | return retval; |
| 101 | } |