diskutil_fbsd.c (4385B)
1 /* see LICENSE file for copyright and license details */ 2 #include "../diskutil.h" 3 #include "../util.h" 4 5 #if defined(__FreeBSD__) && !defined(ARUU_FORCE_POSIX) 6 #include <dirent.h> 7 #include <errno.h> 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <string.h> 11 #include <sys/disk.h> 12 #include <sys/ioctl.h> 13 #include <sys/mount.h> 14 #include <sys/param.h> 15 #include <sys/sysctl.h> 16 #include <sys/types.h> 17 18 void 19 blockdev_sys_size(struct BlockDev *dev) 20 { 21 off_t size_bytes = 0; 22 unsigned int sec_size = 0; 23 24 if (ioctl(dev->fd, DIOCGMEDIASIZE, &size_bytes) >= 0) 25 dev->size = size_bytes; 26 if (ioctl(dev->fd, DIOCGSECTORSIZE, &sec_size) >= 0) 27 dev->sec_size = sec_size; 28 } 29 30 void 31 blockdev_sys_reread(struct BlockDev *dev) 32 { 33 (void)dev; 34 } 35 36 struct BlockDevInfo * 37 blockdev_get_list(void) 38 { 39 char disks[1024]; 40 char devpath[128]; 41 char *tok; 42 char *ptr; 43 struct BlockDevInfo *head = NULL; 44 struct BlockDevInfo *tail = NULL; 45 struct BlockDevInfo *info; 46 struct BlockDevInfo *part; 47 struct BlockDevInfo *p; 48 struct BlockDev dev; 49 DIR *dir; 50 struct dirent *de; 51 size_t disk_len; 52 size_t len; 53 struct statfs *mntbuf; 54 int mntsize; 55 int i; 56 57 len = sizeof(disks); 58 if (sysctlbyname("kern.disks", disks, &len, NULL, 0) < 0) 59 return NULL; 60 61 ptr = disks; 62 while ((tok = strsep(&ptr, " \t")) != NULL) { 63 if (!*tok) 64 continue; 65 66 info = ecalloc(1, sizeof(*info)); 67 68 strlcpy(info->name, tok, sizeof(info->name)); 69 snprintf(info->path, sizeof(info->path), "%s/%s", ARUU_PATH_DEV, tok); 70 strlcpy(info->type, "disk", sizeof(info->type)); 71 72 snprintf(devpath, sizeof(devpath), "%s/%s", ARUU_PATH_DEV, tok); 73 if (blockdev_open(&dev, devpath, 0) == 0) { 74 info->size = dev.size; 75 blockdev_close(&dev); 76 } 77 78 dir = opendir(ARUU_PATH_DEV); 79 if (dir) { 80 disk_len = strlen(tok); 81 while ((de = readdir(dir)) != NULL) { 82 if (strncmp(de->d_name, tok, disk_len) == 0) { 83 char c = de->d_name[disk_len]; 84 if (c == 's' || c == 'p') { 85 part = ecalloc(1, sizeof(*part)); 86 strlcpy(part->name, de->d_name, sizeof(part->name)); 87 snprintf(part->path, sizeof(part->path), "%s/%s", ARUU_PATH_DEV, de->d_name); 88 strlcpy(part->type, "part", sizeof(part->type)); 89 snprintf(devpath, sizeof(devpath), "%s/%s", ARUU_PATH_DEV, de->d_name); 90 if (blockdev_open(&dev, devpath, 0) == 0) { 91 part->size = dev.size; 92 blockdev_close(&dev); 93 } 94 p = info->parts; 95 if (!p) { 96 info->parts = part; 97 } else { 98 while (p->next) 99 p = p->next; 100 p->next = part; 101 } 102 } 103 } 104 } 105 closedir(dir); 106 } 107 108 if (!head) { 109 head = info; 110 } else { 111 tail->next = info; 112 } 113 tail = info; 114 } 115 116 mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); 117 for (i = 0; i < mntsize; i++) { 118 char *frompath = mntbuf[i].f_mntfromname; 119 char *devname = frompath; 120 size_t dev_len = strlen(ARUU_PATH_DEV); 121 if (strncmp(frompath, ARUU_PATH_DEV, dev_len) == 0 && frompath[dev_len] == '/') 122 devname = frompath + dev_len + 1; 123 struct BlockDevInfo *curr; 124 for (curr = head; curr; curr = curr->next) { 125 if (strcmp(curr->name, devname) == 0) { 126 strlcpy(curr->mountpoint, mntbuf[i].f_mntonname, sizeof(curr->mountpoint)); 127 strlcpy(curr->fstype, mntbuf[i].f_fstypename, sizeof(curr->fstype)); 128 } 129 struct BlockDevInfo *part; 130 for (part = curr->parts; part; part = part->next) { 131 if (strcmp(part->name, devname) == 0) { 132 strlcpy(part->mountpoint, mntbuf[i].f_mntonname, sizeof(part->mountpoint)); 133 strlcpy(part->fstype, mntbuf[i].f_fstypename, sizeof(part->fstype)); 134 } 135 } 136 } 137 } 138 139 return head; 140 } 141 142 int 143 blockdev_mount(const char *dev, const char *dir, const char *fstype, int flags, const char *data) 144 { 145 (void)dev; 146 (void)dir; 147 (void)fstype; 148 (void)flags; 149 (void)data; 150 errno = ENOSYS; 151 return -1; 152 } 153 154 int 155 blockdev_unmount(const char *dir) 156 { 157 (void)dir; 158 errno = ENOSYS; 159 return -1; 160 } 161 162 int 163 blockdev_poweroff(const char *disk) 164 { 165 (void)disk; 166 errno = ENOSYS; 167 return -1; 168 } 169 #endif