vfs: check userland buffers before reading them.
[haiku.git] / src / apps / drivesetup / DriveSetup.cpp
blobe3220c1ece2062acbcd7e12a61aa0d9d057aef83
1 /*
2 * Copyright 2002-2013 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT license.
5 * Authors:
6 * Erik Jaesler <ejakowatz@users.sourceforge.net>
7 * Ithamar R. Adema <ithamar@unet.nl>
8 * Stephan Aßmus <superstippi@gmx.de>
9 */
11 #include "DriveSetup.h"
12 #include "MainWindow.h"
14 #include <stdio.h>
15 #include <string.h>
17 #include <File.h>
18 #include <FindDirectory.h>
19 #include <Locale.h>
20 #include <Path.h>
23 DriveSetup::DriveSetup()
24 : BApplication("application/x-vnd.Haiku-DriveSetup"),
25 fWindow(NULL),
26 fSettings((uint32)0)
31 DriveSetup::~DriveSetup()
36 void
37 DriveSetup::ReadyToRun()
39 fWindow = new MainWindow();
40 if (_RestoreSettings() != B_OK)
41 fWindow->ApplyDefaultSettings();
42 fWindow->Show();
46 bool
47 DriveSetup::QuitRequested()
49 _StoreSettings();
51 if (fWindow->Lock()) {
52 fWindow->Quit();
53 fWindow = NULL;
56 return true;
60 // #pragma mark -
63 status_t
64 DriveSetup::_StoreSettings()
66 status_t ret = B_ERROR;
67 if (fWindow->Lock()) {
68 ret = fWindow->StoreSettings(&fSettings);
69 fWindow->Unlock();
72 if (ret < B_OK) {
73 fprintf(stderr, "failed to store settings: %s\n", strerror(ret));
74 return ret;
77 BFile file;
78 ret = _GetSettingsFile(file, true);
79 if (ret < B_OK)
80 return ret;
82 ret = fSettings.Flatten(&file);
83 if (ret < B_OK) {
84 fprintf(stderr, "failed to flatten settings: %s\n", strerror(ret));
85 return ret;
88 return B_OK;
92 status_t
93 DriveSetup::_RestoreSettings()
95 BFile file;
96 status_t ret = _GetSettingsFile(file, false);
97 if (ret < B_OK)
98 return ret;
100 ret = fSettings.Unflatten(&file);
101 if (ret < B_OK) {
102 fprintf(stderr, "failed to unflatten settings: %s\n", strerror(ret));
103 return ret;
106 ret = fWindow->RestoreSettings(&fSettings);
107 if (ret < B_OK) {
108 fprintf(stderr, "failed to restore settings: %s\n", strerror(ret));
109 return ret;
112 return B_OK;
116 status_t
117 DriveSetup::_GetSettingsFile(BFile& file, bool forWriting) const
119 BPath path;
120 status_t ret = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
121 if (ret != B_OK) {
122 fprintf(stderr, "failed to get user settings folder: %s\n",
123 strerror(ret));
124 return ret;
127 ret = path.Append("DriveSetup");
128 if (ret != B_OK) {
129 fprintf(stderr, "failed to construct path: %s\n", strerror(ret));
130 return ret;
133 uint32 writeFlags = B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY;
134 uint32 readFlags = B_READ_ONLY;
136 ret = file.SetTo(path.Path(), forWriting ? writeFlags : readFlags);
137 if (ret != B_OK) {
138 if (forWriting) {
139 // Only inform of an error if the file was supposed to be written.
140 fprintf(stderr, "failed to init settings file: %s\n",
141 strerror(ret));
143 return ret;
146 return B_OK;
150 // #pragma mark -
154 main(int, char**)
156 DriveSetup app;
157 app.Run();
158 return 0;