1/*
2 * Copyright 2013-2014, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Ingo Weinhold <ingo_weinhold@gmx.de>
7 */
8
9
10#include "PackageFile.h"
11
12#include <fcntl.h>
13
14#include <File.h>
15
16#include <AutoDeleter.h>
17
18#include "DebugSupport.h"
19#include "PackageFileManager.h"
20
21
22PackageFile::PackageFile()
23	:
24	fNodeRef(),
25	fDirectoryRef(),
26	fFileName(),
27	fInfo(),
28	fEntryRefHashTableNext(NULL),
29// 	fNodeRefHashTableNext(NULL),
30	fOwner(NULL),
31	fIgnoreEntryCreated(0),
32	fIgnoreEntryRemoved(0)
33{
34}
35
36
37PackageFile::~PackageFile()
38{
39}
40
41
42status_t
43PackageFile::Init(const entry_ref& entryRef, PackageFileManager* owner)
44{
45	fDirectoryRef.device = entryRef.device;
46	fDirectoryRef.node = entryRef.directory;
47
48	// init the file name
49	fFileName = entryRef.name;
50	if (fFileName.IsEmpty())
51		RETURN_ERROR(B_NO_MEMORY);
52
53	// open the file and get the node_ref
54	BFile file;
55	status_t error = file.SetTo(&entryRef, B_READ_ONLY);
56	if (error != B_OK)
57		RETURN_ERROR(error);
58
59	error = file.GetNodeRef(&fNodeRef);
60	if (error != B_OK)
61		RETURN_ERROR(error);
62
63	// get the package info
64	FileDescriptorCloser fd(file.Dup());
65	if (!fd.IsSet())
66		RETURN_ERROR(error);
67
68	error = fInfo.ReadFromPackageFile(fd.Get());
69	if (error != B_OK)
70		RETURN_ERROR(error);
71
72	if (fFileName != fInfo.CanonicalFileName())
73		fInfo.SetFileName(fFileName);
74
75	fOwner = owner;
76
77	return B_OK;
78}
79
80
81BString
82PackageFile::RevisionedName() const
83{
84	return BString().SetToFormat("%s-%s", fInfo.Name().String(),
85		fInfo.Version().ToString().String());
86}
87
88
89BString
90PackageFile::RevisionedNameThrows() const
91{
92	BString result(RevisionedName());
93	if (result.IsEmpty())
94		throw std::bad_alloc();
95	return result;
96}
97
98
99void
100PackageFile::LastReferenceReleased()
101{
102	if (fOwner != NULL)
103		fOwner->RemovePackageFile(this);
104	delete this;
105}
106