ext_attr.c revision 4ea7bd04390935e1f8b473c8b857e518df2e226b
1/*
2 * ext_attr.c --- extended attribute blocks
3 *
4 * Copyright (C) Andreas Gruenbacher, <a.gruenbacher@computer.org>
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 */
11
12#include <stdio.h>
13#if HAVE_UNISTD_H
14#include <unistd.h>
15#endif
16#include <string.h>
17#include <time.h>
18
19#include "ext2_fs.h"
20#include "ext2_ext_attr.h"
21
22#include "ext2fs.h"
23
24#ifdef EXT2FS_ENABLE_SWAPFS
25void ext2fs_swap_ext_attr(ext2_filsys fs, char *to, char *from)
26{
27	struct ext2_ext_attr_header *from_header =
28		(struct ext2_ext_attr_header *)from;
29	struct ext2_ext_attr_header *to_header =
30		(struct ext2_ext_attr_header *)to;
31	struct ext2_ext_attr_entry *from_entry, *to_entry;
32	char *from_end = (char *)from_header + fs->blocksize;
33	int n;
34
35	if (to_header != from_header)
36		memcpy(to_header, from_header, fs->blocksize);
37
38	to_header->h_magic    = ext2fs_swab32(from_header->h_magic);
39	to_header->h_blocks   = ext2fs_swab32(from_header->h_blocks);
40	to_header->h_refcount = ext2fs_swab32(from_header->h_refcount);
41	for (n=0; n<4; n++)
42		to_header->h_reserved[n] =
43			ext2fs_swab32(from_header->h_reserved[n]);
44
45	from_entry = (struct ext2_ext_attr_entry *)(from_header+1);
46	to_entry   = (struct ext2_ext_attr_entry *)(to_header+1);
47	while ((char *)from_entry < from_end && *(__u32 *)from_entry) {
48		to_entry->e_value_offs  =
49			ext2fs_swab16(from_entry->e_value_offs);
50		to_entry->e_value_block =
51			ext2fs_swab32(from_entry->e_value_block);
52		to_entry->e_value_size  =
53			ext2fs_swab32(from_entry->e_value_size);
54		from_entry = EXT2_EXT_ATTR_NEXT(from_entry);
55		to_entry   = EXT2_EXT_ATTR_NEXT(to_entry);
56	}
57}
58#endif
59
60errcode_t ext2fs_read_ext_attr(ext2_filsys fs, blk_t block, void *buf)
61{
62	errcode_t	retval;
63
64 	retval = io_channel_read_blk(fs->io, block, 1, buf);
65	if (retval)
66		return retval;
67#ifdef EXT2FS_ENABLE_SWAPFS
68	if ((fs->flags & (EXT2_FLAG_SWAP_BYTES|
69			  EXT2_FLAG_SWAP_BYTES_READ)) != 0)
70		ext2fs_swap_ext_attr(fs, buf, buf);
71#endif
72	return 0;
73}
74
75errcode_t ext2fs_write_ext_attr(ext2_filsys fs, blk_t block, void *inbuf)
76{
77	errcode_t	retval;
78	char		*write_buf;
79	char		*buf = NULL;
80
81#ifdef EXT2FS_ENABLE_SWAPFS
82	if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
83	    (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)) {
84		retval = ext2fs_get_mem(fs->blocksize, (void **) &buf);
85		if (retval)
86			return retval;
87		write_buf = buf;
88		ext2fs_swap_ext_attr(fs, buf, inbuf);
89	} else
90#endif
91		write_buf = (char *) inbuf;
92 	retval = io_channel_write_blk(fs->io, block, 1, write_buf);
93	if (buf)
94		ext2fs_free_mem((void **) &buf);
95	if (!retval)
96		ext2fs_mark_changed(fs);
97	return retval;
98}
99