Initial public busybox upstream commit
[busybox4maemo] / e2fsprogs / old_e2fsprogs / ext2fs / ext_attr.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * ext_attr.c --- extended attribute blocks
4  *
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>
16 #include <unistd.h>
17 #include <string.h>
18 #include <time.h>
19
20 #include "ext2_fs.h"
21 #include "ext2_ext_attr.h"
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
28         retval = io_channel_read_blk(fs->io, block, 1, buf);
29         if (retval)
30                 return retval;
31 #if BB_BIG_ENDIAN
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
45         if (BB_BIG_ENDIAN && ((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                 write_buf = (char *) inbuf;
54         retval = io_channel_write_blk(fs->io, block, 1, write_buf);
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 }