Path: blob/master/Utilities/cmlibarchive/libarchive/archive_entry_xattr.c
3153 views
/*-1* Copyright (c) 2003-2007 Tim Kientzle2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR14* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES15* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,17* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT18* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,19* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY20* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT21* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF22* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.23*/2425#include "archive_platform.h"2627#ifdef HAVE_SYS_STAT_H28#include <sys/stat.h>29#endif30#ifdef HAVE_SYS_TYPES_H31#include <sys/types.h>32#endif33#ifdef HAVE_LIMITS_H34#include <limits.h>35#endif36#ifdef HAVE_LINUX_FS_H37#include <linux/fs.h> /* for Linux file flags */38#endif39/*40* Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.41* As the include guards don't agree, the order of include is important.42*/43#ifdef HAVE_LINUX_EXT2_FS_H44#include <linux/ext2_fs.h> /* for Linux file flags */45#endif46#if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)47#include <ext2fs/ext2_fs.h> /* for Linux file flags */48#endif49#include <stddef.h>50#include <stdio.h>51#ifdef HAVE_STDLIB_H52#include <stdlib.h>53#endif54#ifdef HAVE_STRING_H55#include <string.h>56#endif57#ifdef HAVE_WCHAR_H58#include <wchar.h>59#endif6061#include "archive.h"62#include "archive_entry.h"63#include "archive_private.h"64#include "archive_entry_private.h"6566/*67* extended attribute handling68*/6970void71archive_entry_xattr_clear(struct archive_entry *entry)72{73struct ae_xattr *xp;7475while (entry->xattr_head != NULL) {76xp = entry->xattr_head->next;77free(entry->xattr_head->name);78free(entry->xattr_head->value);79free(entry->xattr_head);80entry->xattr_head = xp;81}8283entry->xattr_head = NULL;84}8586void87archive_entry_xattr_add_entry(struct archive_entry *entry,88const char *name, const void *value, size_t size)89{90struct ae_xattr *xp;9192if ((xp = malloc(sizeof(struct ae_xattr))) == NULL)93__archive_errx(1, "Out of memory");9495if ((xp->name = strdup(name)) == NULL)96__archive_errx(1, "Out of memory");9798if ((xp->value = malloc(size)) != NULL) {99memcpy(xp->value, value, size);100xp->size = size;101} else102xp->size = 0;103104xp->next = entry->xattr_head;105entry->xattr_head = xp;106}107108109/*110* returns number of the extended attribute entries111*/112int113archive_entry_xattr_count(struct archive_entry *entry)114{115struct ae_xattr *xp;116int count = 0;117118for (xp = entry->xattr_head; xp != NULL; xp = xp->next)119count++;120121return count;122}123124int125archive_entry_xattr_reset(struct archive_entry * entry)126{127entry->xattr_p = entry->xattr_head;128129return archive_entry_xattr_count(entry);130}131132int133archive_entry_xattr_next(struct archive_entry * entry,134const char **name, const void **value, size_t *size)135{136if (entry->xattr_p) {137*name = entry->xattr_p->name;138*value = entry->xattr_p->value;139*size = entry->xattr_p->size;140141entry->xattr_p = entry->xattr_p->next;142143return (ARCHIVE_OK);144} else {145*name = NULL;146*value = NULL;147*size = (size_t)0;148return (ARCHIVE_WARN);149}150}151152/*153* end of xattr handling154*/155156157