2 * Copyright 2015, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
13 #include <device/scsi.h>
14 #include <DiskDevice.h>
15 #include <DiskDeviceRoster.h>
24 IssueDeviceCommand(const char* path
, int opcode
, void* buffer
,
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
);
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
,
49 } // private namespace
56 IsReadOnlyVolume(dev_t device
)
59 status_t status
= volume
.SetTo(device
);
61 fprintf(stderr
, "Failed to get BVolume for device %" B_PRIdDEV
62 ": %s\n", device
, strerror(status
));
66 BDiskDeviceRoster roster
;
67 BDiskDevice diskDevice
;
68 BPartition
* partition
;
69 status
= roster
.FindPartitionByVolume(volume
, &diskDevice
, &partition
);
71 fprintf(stderr
, "Failed to get partition for device %" B_PRIdDEV
72 ": %s\n", device
, strerror(status
));
76 return partition
->IsReadOnly();
81 IsReadOnlyVolume(const char* path
)
83 return IsReadOnlyVolume(dev_for_path(path
));
88 BlockMedia(const char* path
, bool block
)
90 return IssueDeviceCommand(path
, B_SCSI_PREVENT_ALLOW
, &block
,
96 EjectMedia(const char* path
)
98 return IssueDeviceCommand(path
, B_EJECT_DEVICE
, NULL
, 0);
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
);
118 } // namespace Utility