comment all fields
[oweals/busybox.git] / e2fsprogs / ext2fs / ext_attr.c
1 /*
2  * ext_attr.c --- extended attribute blocks
3  *
4  * Copyright (C) 2001 Andreas Gruenbacher, <a.gruenbacher@computer.org>
5  *
6  * Copyright (C) 2002 Theodore Ts'o.
7  *
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 <unistd.h>
16 #include <string.h>
17 #include <time.h>
18
19 #include "ext2_fs.h"
20 #include "ext2_ext_attr.h"
21 #include "ext2fs.h"
22
23 errcode_t ext2fs_read_ext_attr(ext2_filsys fs, blk_t block, void *buf)
24 {
25         errcode_t       retval;
26
27         retval = io_channel_read_blk(fs->io, block, 1, buf);
28         if (retval)
29                 return retval;
30 #if BB_BIG_ENDIAN
31         if ((fs->flags & (EXT2_FLAG_SWAP_BYTES|
32                           EXT2_FLAG_SWAP_BYTES_READ)) != 0)
33                 ext2fs_swap_ext_attr(buf, buf, fs->blocksize, 1);
34 #endif
35         return 0;
36 }
37
38 errcode_t ext2fs_write_ext_attr(ext2_filsys fs, blk_t block, void *inbuf)
39 {
40         errcode_t       retval;
41         char            *write_buf;
42         char            *buf = NULL;
43
44 #if BB_BIG_ENDIAN
45         if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
46             (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)) {
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
53 #endif
54                 write_buf = (char *) inbuf;
55         retval = io_channel_write_blk(fs->io, block, 1, write_buf);
56         if (buf)
57                 ext2fs_free_mem(&buf);
58         if (!retval)
59                 ext2fs_mark_changed(fs);
60         return retval;
61 }
62
63 /*
64  * This function adjusts the reference count of the EA block.
65  */
66 errcode_t ext2fs_adjust_ea_refcount(ext2_filsys fs, blk_t blk,
67                                     char *block_buf, int adjust,
68                                     __u32 *newcount)
69 {
70         errcode_t       retval;
71         struct ext2_ext_attr_header *header;
72         char    *buf = 0;
73
74         if ((blk >= fs->super->s_blocks_count) ||
75             (blk < fs->super->s_first_data_block))
76                 return EXT2_ET_BAD_EA_BLOCK_NUM;
77
78         if (!block_buf) {
79                 retval = ext2fs_get_mem(fs->blocksize, &buf);
80                 if (retval)
81                         return retval;
82                 block_buf = buf;
83         }
84
85         retval = ext2fs_read_ext_attr(fs, blk, block_buf);
86         if (retval)
87                 goto errout;
88
89         header = (struct ext2_ext_attr_header *) block_buf;
90         header->h_refcount += adjust;
91         if (newcount)
92                 *newcount = header->h_refcount;
93
94         retval = ext2fs_write_ext_attr(fs, blk, block_buf);
95         if (retval)
96                 goto errout;
97
98 errout:
99         if (buf)
100                 ext2fs_free_mem(&buf);
101         return retval;
102 }