/*
 * Copyright (c) 2008-2026 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3.0 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3.0 along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#import "OFMutableTarArchiveEntry.h"
#import "OFTarArchiveEntry+Private.h"
#import "OFData.h"
#import "OFDate.h"
#import "OFDictionary.h"
#import "OFNumber.h"
#import "OFString.h"

@implementation OFMutableTarArchiveEntry
@dynamic fileName, fileType, POSIXPermissions, ownerAccountID;
@dynamic groupOwnerAccountID, compressedSize, uncompressedSize;
@dynamic modificationDate, type, targetFileName, ownerAccountName;
@dynamic groupOwnerAccountName, deviceMajor, deviceMinor, extendedHeader;
@dynamic amigaProtection, amigaComment;
/*
 * The following is optional in OFMutableArchiveEntry, but Apple GCC 4.0.1 is
 * buggy and needs this to stop complaining.
 */
@dynamic fileComment;

+ (instancetype)entryWithFileName: (OFString *)fileName
{
	return objc_autoreleaseReturnValue(
	    [[self alloc] initWithFileName: fileName]);
}

- (instancetype)initWithFileName: (OFString *)fileName
{
	self = [self of_init];

	@try {
		_fileName = [fileName copy];
	} @catch (id e) {
		objc_release(self);
		@throw e;
	}

	return self;
}

- (id)copy
{
	OFMutableTarArchiveEntry *copy = [self mutableCopy];

	[copy makeImmutable];

	return copy;
}

- (void)setFileName: (OFString *)fileName
{
	OFString *old = _fileName;
	_fileName = [fileName copy];
	objc_release(old);
}

- (void)setFileType: (OFArchiveEntryFileType)fileType
{
	_fileType = fileType;
}

- (void)setPOSIXPermissions: (OFNumber *)POSIXPermissions
{
	OFNumber *old = _POSIXPermissions;
	_POSIXPermissions = objc_retain(POSIXPermissions);
	objc_release(old);
}

- (void)setOwnerAccountID: (OFNumber *)ownerAccountID
{
	OFNumber *old = _ownerAccountID;
	_ownerAccountID = objc_retain(ownerAccountID);
	objc_release(old);
}

- (void)setGroupOwnerAccountID: (OFNumber *)groupOwnerAccountID
{
	OFNumber *old = _groupOwnerAccountID;
	_groupOwnerAccountID = objc_retain(groupOwnerAccountID);
	objc_release(old);
}

- (void)setCompressedSize: (unsigned long long)compressedSize
{
	_compressedSize = compressedSize;
}

- (void)setUncompressedSize: (unsigned long long)uncompressedSize
{
	_uncompressedSize = uncompressedSize;
}

- (void)setModificationDate: (OFDate *)modificationDate
{
	OFDate *old = _modificationDate;
	_modificationDate = objc_retain(modificationDate);
	objc_release(old);
}

#if OF_GCC_VERSION >= 405
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
- (void)setType: (OFTarArchiveEntryType)type
{
	_fileType = type;
}
#if OF_GCC_VERSION >= 405
# pragma GCC diagnostic pop
#endif

- (void)setTargetFileName: (OFString *)targetFileName
{
	OFString *old = _targetFileName;
	_targetFileName = [targetFileName copy];
	objc_release(old);
}

- (void)setOwnerAccountName: (OFString *)ownerAccountName
{
	OFString *old = _ownerAccountName;
	_ownerAccountName = [ownerAccountName copy];
	objc_release(old);
}

- (void)setGroupOwnerAccountName: (OFString *)groupOwnerAccountName
{
	OFString *old = _groupOwnerAccountName;
	_groupOwnerAccountName = [groupOwnerAccountName copy];
	objc_release(old);
}

- (void)setDeviceMajor: (unsigned long)deviceMajor
{
	_deviceMajor = deviceMajor;
}

- (void)setDeviceMinor: (unsigned long)deviceMinor
{
	_deviceMinor = deviceMinor;
}

- (void)setExtendedHeader:
    (OFDictionary OF_GENERIC(OFString *, OFData *) *)extendedHeader
{
	OFMutableDictionary *old = _extendedHeader;
	_extendedHeader = [extendedHeader mutableCopy];
	objc_release(old);
}

- (void)setAmigaProtection: (OFNumber *)amigaProtection
{
	void *pool = objc_autoreleasePoolPush();
	uint32_t tmp = OFToBigEndian32(
	    (uint32_t)amigaProtection.unsignedLongValue);
	OFData *data = [OFData dataWithItems: &tmp count: sizeof(tmp)];

	if (_extendedHeader == nil)
		_extendedHeader = [[OFMutableDictionary alloc] init];

	[_extendedHeader setObject: data
			    forKey: @"SCHILY.xattr.user.amiga.mode"];

	objc_autoreleasePoolPop(pool);
}

- (void)setAmigaComment: (OFString *)amigaComment
{
	if (amigaComment != nil) {
		void *pool = objc_autoreleasePoolPush();
		const char *cString = [amigaComment
		    cStringWithEncoding: OFStringEncodingISO8859_1];
		size_t length = [amigaComment
		    cStringLengthWithEncoding: OFStringEncodingISO8859_1];
		OFData *data = [OFData dataWithItems: cString
					       count: length + 1];

		if (_extendedHeader == nil)
			_extendedHeader = [[OFMutableDictionary alloc] init];

		[_extendedHeader setObject: data
				    forKey: @"SCHILY.xattr.user.amiga.comment"];

		objc_autoreleasePoolPop(pool);
	} else
		[_extendedHeader removeObjectForKey:
		    @"SCHILY.xattr.user.amiga.comment"];
}

- (void)makeImmutable
{
	object_setClass(self, [OFTarArchiveEntry class]);
}
@end
