ext_attr.c revision 342d847db355d81299218e07a1e58ece82080a04
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	struct ext2_ext_attr_entry *entry;
64
65 	retval = io_channel_read_blk(fs->io, block, 1, buf);
66	if (retval)
67		return retval;
68#ifdef EXT2FS_ENABLE_SWAPFS
69	if ((fs->flags & (EXT2_FLAG_SWAP_BYTES|
70			  EXT2_FLAG_SWAP_BYTES_READ)) != 0)
71		ext2fs_swap_ext_attr(fs, buf, buf);
72#endif
73	return 0;
74}
75
76errcode_t ext2fs_write_ext_attr(ext2_filsys fs, blk_t block, void *inbuf)
77{
78	errcode_t	retval;
79	char		*p, *end, *write_buf;
80	char		*buf = NULL;
81	struct ext2_dir_entry *dirent;
82
83#ifdef EXT2FS_ENABLE_SWAPFS
84	if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
85	    (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)) {
86		retval = ext2fs_get_mem(fs->blocksize, (void **) &buf);
87		if (retval)
88			return retval;
89		write_buf = buf;
90		ext2fs_swap_ext_attr(fs, buf, inbuf);
91	} else
92#endif
93		write_buf = (char *) inbuf;
94 	retval = io_channel_write_blk(fs->io, block, 1, write_buf);
95	if (buf)
96		ext2fs_free_mem((void **) &buf);
97	return retval;
98}
99