Make UEFI boot-platform build again
[haiku.git] / src / servers / launch / Utility.cpp
blobb936ef31cba7834f22ed977d1e5469fb78dd0adb
1 /*
2 * Copyright 2015, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include "Utility.h"
9 #include <errno.h>
10 #include <stdio.h>
11 #include <string.h>
13 #include <device/scsi.h>
14 #include <DiskDevice.h>
15 #include <DiskDeviceRoster.h>
16 #include <fs_info.h>
17 #include <Volume.h>
20 namespace {
23 status_t
24 IssueDeviceCommand(const char* path, int opcode, void* buffer,
25 size_t bufferSize)
27 fs_info info;
28 if (fs_stat_dev(dev_for_path(path), &info) == B_OK) {
29 if (strcmp(info.fsh_name, "devfs") != 0)
30 path = info.device_name;
33 int device = open(path, O_RDONLY);
34 if (device < 0)
35 return device;
37 status_t status = B_OK;
39 if (ioctl(device, opcode, buffer, bufferSize) != 0) {
40 fprintf(stderr, "Failed to process %d on %s: %s\n", opcode, path,
41 strerror(errno));
42 status = errno;
44 close(device);
45 return status;
49 } // private namespace
52 namespace Utility {
55 bool
56 IsReadOnlyVolume(dev_t device)
58 BVolume volume;
59 status_t status = volume.SetTo(device);
60 if (status != B_OK) {
61 fprintf(stderr, "Failed to get BVolume for device %" B_PRIdDEV
62 ": %s\n", device, strerror(status));
63 return false;
66 BDiskDeviceRoster roster;
67 BDiskDevice diskDevice;
68 BPartition* partition;
69 status = roster.FindPartitionByVolume(volume, &diskDevice, &partition);
70 if (status != B_OK) {
71 fprintf(stderr, "Failed to get partition for device %" B_PRIdDEV
72 ": %s\n", device, strerror(status));
73 return false;
76 return partition->IsReadOnly();
80 bool
81 IsReadOnlyVolume(const char* path)
83 return IsReadOnlyVolume(dev_for_path(path));
87 status_t
88 BlockMedia(const char* path, bool block)
90 return IssueDeviceCommand(path, B_SCSI_PREVENT_ALLOW, &block,
91 sizeof(block));
95 status_t
96 EjectMedia(const char* path)
98 return IssueDeviceCommand(path, B_EJECT_DEVICE, NULL, 0);
102 BString
103 TranslatePath(const char* originalPath)
105 BString path = originalPath;
107 // TODO: get actual home directory!
108 const char* home = "/boot/home";
109 path.ReplaceAll("$HOME", home);
110 path.ReplaceAll("${HOME}", home);
111 if (path.StartsWith("~/"))
112 path.ReplaceFirst("~", home);
114 return path;
118 } // namespace Utility