Blame libarchive/archive_entry_xattr.c

Packit 08bd4c
/*-
Packit 08bd4c
 * Copyright (c) 2003-2007 Tim Kientzle
Packit 08bd4c
 * All rights reserved.
Packit 08bd4c
 *
Packit 08bd4c
 * Redistribution and use in source and binary forms, with or without
Packit 08bd4c
 * modification, are permitted provided that the following conditions
Packit 08bd4c
 * are met:
Packit 08bd4c
 * 1. Redistributions of source code must retain the above copyright
Packit 08bd4c
 *    notice, this list of conditions and the following disclaimer.
Packit 08bd4c
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 08bd4c
 *    notice, this list of conditions and the following disclaimer in the
Packit 08bd4c
 *    documentation and/or other materials provided with the distribution.
Packit 08bd4c
 *
Packit 08bd4c
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
Packit 08bd4c
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit 08bd4c
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit 08bd4c
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit 08bd4c
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit 08bd4c
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 08bd4c
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 08bd4c
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 08bd4c
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit 08bd4c
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 08bd4c
 */
Packit 08bd4c
Packit 08bd4c
#include "archive_platform.h"
Packit 08bd4c
__FBSDID("$FreeBSD: head/lib/libarchive/archive_entry_xattr.c 201096 2009-12-28 02:41:27Z kientzle $");
Packit 08bd4c
Packit 08bd4c
#ifdef HAVE_SYS_STAT_H
Packit 08bd4c
#include <sys/stat.h>
Packit 08bd4c
#endif
Packit 08bd4c
#ifdef HAVE_SYS_TYPES_H
Packit 08bd4c
#include <sys/types.h>
Packit 08bd4c
#endif
Packit 08bd4c
#ifdef HAVE_LIMITS_H
Packit 08bd4c
#include <limits.h>
Packit 08bd4c
#endif
Packit 08bd4c
#ifdef HAVE_LINUX_FS_H
Packit 08bd4c
#include <linux/fs.h>	/* for Linux file flags */
Packit 08bd4c
#endif
Packit 08bd4c
/*
Packit 08bd4c
 * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
Packit 08bd4c
 * As the include guards don't agree, the order of include is important.
Packit 08bd4c
 */
Packit 08bd4c
#ifdef HAVE_LINUX_EXT2_FS_H
Packit 08bd4c
#include <linux/ext2_fs.h>	/* for Linux file flags */
Packit 08bd4c
#endif
Packit 08bd4c
#if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
Packit 08bd4c
#include <ext2fs/ext2_fs.h>	/* for Linux file flags */
Packit 08bd4c
#endif
Packit 08bd4c
#include <stddef.h>
Packit 08bd4c
#include <stdio.h>
Packit 08bd4c
#ifdef HAVE_STDLIB_H
Packit 08bd4c
#include <stdlib.h>
Packit 08bd4c
#endif
Packit 08bd4c
#ifdef HAVE_STRING_H
Packit 08bd4c
#include <string.h>
Packit 08bd4c
#endif
Packit 08bd4c
#ifdef HAVE_WCHAR_H
Packit 08bd4c
#include <wchar.h>
Packit 08bd4c
#endif
Packit 08bd4c
Packit 08bd4c
#include "archive.h"
Packit 08bd4c
#include "archive_entry.h"
Packit 08bd4c
#include "archive_private.h"
Packit 08bd4c
#include "archive_entry_private.h"
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * extended attribute handling
Packit 08bd4c
 */
Packit 08bd4c
Packit 08bd4c
void
Packit 08bd4c
archive_entry_xattr_clear(struct archive_entry *entry)
Packit 08bd4c
{
Packit 08bd4c
	struct ae_xattr	*xp;
Packit 08bd4c
Packit 08bd4c
	while (entry->xattr_head != NULL) {
Packit 08bd4c
		xp = entry->xattr_head->next;
Packit 08bd4c
		free(entry->xattr_head->name);
Packit 08bd4c
		free(entry->xattr_head->value);
Packit 08bd4c
		free(entry->xattr_head);
Packit 08bd4c
		entry->xattr_head = xp;
Packit 08bd4c
	}
Packit 08bd4c
Packit 08bd4c
	entry->xattr_head = NULL;
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
void
Packit 08bd4c
archive_entry_xattr_add_entry(struct archive_entry *entry,
Packit 08bd4c
	const char *name, const void *value, size_t size)
Packit 08bd4c
{
Packit 08bd4c
	struct ae_xattr	*xp;
Packit 08bd4c
Packit 08bd4c
	if ((xp = (struct ae_xattr *)malloc(sizeof(struct ae_xattr))) == NULL)
Packit 08bd4c
		__archive_errx(1, "Out of memory");
Packit 08bd4c
Packit 08bd4c
	if ((xp->name = strdup(name)) == NULL)
Packit 08bd4c
		__archive_errx(1, "Out of memory");
Packit 08bd4c
Packit 08bd4c
	if ((xp->value = malloc(size)) != NULL) {
Packit 08bd4c
		memcpy(xp->value, value, size);
Packit 08bd4c
		xp->size = size;
Packit 08bd4c
	} else
Packit 08bd4c
		xp->size = 0;
Packit 08bd4c
Packit 08bd4c
	xp->next = entry->xattr_head;
Packit 08bd4c
	entry->xattr_head = xp;
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * returns number of the extended attribute entries
Packit 08bd4c
 */
Packit 08bd4c
int
Packit 08bd4c
archive_entry_xattr_count(struct archive_entry *entry)
Packit 08bd4c
{
Packit 08bd4c
	struct ae_xattr *xp;
Packit 08bd4c
	int count = 0;
Packit 08bd4c
Packit 08bd4c
	for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
Packit 08bd4c
		count++;
Packit 08bd4c
Packit 08bd4c
	return count;
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
int
Packit 08bd4c
archive_entry_xattr_reset(struct archive_entry * entry)
Packit 08bd4c
{
Packit 08bd4c
	entry->xattr_p = entry->xattr_head;
Packit 08bd4c
Packit 08bd4c
	return archive_entry_xattr_count(entry);
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
int
Packit 08bd4c
archive_entry_xattr_next(struct archive_entry * entry,
Packit 08bd4c
	const char **name, const void **value, size_t *size)
Packit 08bd4c
{
Packit 08bd4c
	if (entry->xattr_p) {
Packit 08bd4c
		*name = entry->xattr_p->name;
Packit 08bd4c
		*value = entry->xattr_p->value;
Packit 08bd4c
		*size = entry->xattr_p->size;
Packit 08bd4c
Packit 08bd4c
		entry->xattr_p = entry->xattr_p->next;
Packit 08bd4c
Packit 08bd4c
		return (ARCHIVE_OK);
Packit 08bd4c
	} else {
Packit 08bd4c
		*name = NULL;
Packit 08bd4c
		*value = NULL;
Packit 08bd4c
		*size = (size_t)0;
Packit 08bd4c
		return (ARCHIVE_WARN);
Packit 08bd4c
	}
Packit 08bd4c
}
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * end of xattr handling
Packit 08bd4c
 */