xref: /haiku/src/add-ons/kernel/file_systems/userlandfs/kernel_add_on/FileSystemInitializer.cpp (revision 1acbe440b8dd798953bec31d18ee589aa3f71b73)
1 // FileSystemInitializer.cpp
2 
3 #include "FileSystemInitializer.h"
4 
5 #include <new>
6 
7 #include "FileSystem.h"
8 #include "RequestAllocator.h"
9 #include "RequestPort.h"
10 #include "Requests.h"
11 #include "SingleReplyRequestHandler.h"
12 
13 using std::nothrow;
14 
15 // constructor
16 FileSystemInitializer::FileSystemInitializer(const char* name,
17 	RequestPort* initPort)
18 	: fName(name),
19 	  fInitPort(initPort),
20 	  fFileSystem(NULL)
21 {
22 	// Note: We don't copy the name. It's only needed in FirstTimeInit() and
23 	// the UserlandFS makes sure it is valid until then.
24 }
25 
26 // destructor
27 FileSystemInitializer::~FileSystemInitializer()
28 {
29 	delete fFileSystem;
30 }
31 
32 // FirstTimeInit
33 status_t
34 FileSystemInitializer::FirstTimeInit()
35 {
36 	// prepare the request
37 	RequestAllocator allocator(fInitPort->GetPort());
38 	FSConnectRequest* request;
39 	status_t error = AllocateRequest(allocator, &request);
40 	if (error != B_OK)
41 		RETURN_ERROR(error);
42 	error = allocator.AllocateString(request->fsName, fName);
43 	if (error != B_OK)
44 		RETURN_ERROR(error);
45 
46 	// send the request
47 	SingleReplyRequestHandler handler(FS_CONNECT_REPLY);
48 	FSConnectReply* reply;
49 	error = fInitPort->SendRequest(&allocator, &handler, (Request**)&reply);
50 	if (error != B_OK)
51 		RETURN_ERROR(error);
52 	RequestReleaser requestReleaser(fInitPort, reply);
53 
54 	// process the reply
55 	if (reply->error != B_OK)
56 		RETURN_ERROR(reply->error);
57 
58 	// get the port infos
59 	int32 count = reply->portInfoCount;
60 	if (count < 2)
61 		RETURN_ERROR(B_BAD_DATA);
62 	if (reply->portInfos.GetSize() != count * (int32)sizeof(Port::Info))
63 		RETURN_ERROR(B_BAD_DATA);
64 	Port::Info* infos = (Port::Info*)reply->portInfos.GetData();
65 
66 	// create and init the FileSystem
67 	fFileSystem = new(nothrow) FileSystem();
68 	if (!fFileSystem)
69 		return B_NO_MEMORY;
70 
71 	error = fFileSystem->Init(fName, infos, count, reply->capabilities);
72 	if (error != B_OK)
73 		return B_ERROR;
74 
75 	return B_OK;
76 }
77