xref: /haiku/src/servers/package/PackageFile.cpp (revision 1e60bdeab63fa7a57bc9a55b032052e95a18bd2c)
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 
22 PackageFile::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 
37 PackageFile::~PackageFile()
38 {
39 }
40 
41 
42 status_t
43 PackageFile::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 	int fd = file.Dup();
65 	if (fd < 0)
66 		RETURN_ERROR(error);
67 	FileDescriptorCloser fdCloser(fd);
68 
69 	error = fInfo.ReadFromPackageFile(fd);
70 	if (error != B_OK)
71 		RETURN_ERROR(error);
72 
73 	if (fFileName != fInfo.CanonicalFileName())
74 		fInfo.SetFileName(fFileName);
75 
76 	fOwner = owner;
77 
78 	return B_OK;
79 }
80 
81 
82 BString
83 PackageFile::RevisionedName() const
84 {
85 	return BString().SetToFormat("%s-%s", fInfo.Name().String(),
86 		fInfo.Version().ToString().String());
87 }
88 
89 
90 BString
91 PackageFile::RevisionedNameThrows() const
92 {
93 	BString result(RevisionedName());
94 	if (result.IsEmpty())
95 		throw std::bad_alloc();
96 	return result;
97 }
98 
99 
100 void
101 PackageFile::LastReferenceReleased()
102 {
103 	if (fOwner != NULL)
104 		fOwner->RemovePackageFile(this);
105 	delete this;
106 }
107