|
| 1 | +/* |
| 2 | + * Copyright (C) 2012-2013 Citrix Inc |
| 3 | + * |
| 4 | + * Permission to use, copy, modify, and distribute this software for any |
| 5 | + * purpose with or without fee is hereby granted, provided that the above |
| 6 | + * copyright notice and this permission notice appear in all copies. |
| 7 | + * |
| 8 | + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <sys/types.h> |
| 18 | +#include <sys/stat.h> |
| 19 | +#include <sys/ioctl.h> |
| 20 | + |
| 21 | +#include <fcntl.h> |
| 22 | +#include <string.h> |
| 23 | +#include <unistd.h> |
| 24 | +#include <stdint.h> |
| 25 | + |
| 26 | +#include <caml/alloc.h> |
| 27 | +#include <caml/memory.h> |
| 28 | +#include <caml/signals.h> |
| 29 | +#include <caml/fail.h> |
| 30 | +#include <caml/callback.h> |
| 31 | +#include <caml/bigarray.h> |
| 32 | + |
| 33 | +#ifdef __linux__ |
| 34 | +#include <linux/fs.h> |
| 35 | + |
| 36 | +int blkgetsize(int fd, uint64_t *psize) |
| 37 | +{ |
| 38 | +#ifdef BLKGETSIZE64 |
| 39 | + int ret = ioctl(fd, BLKGETSIZE64, psize); |
| 40 | +#elif BLKGETSIZE |
| 41 | + unsigned long sectors = 0; |
| 42 | + int ret = ioctl(fd, BLKGETSIZE, §ors); |
| 43 | + *psize = sectors * 512ULL; |
| 44 | +#else |
| 45 | +# error "Linux configuration error (blkgetsize)" |
| 46 | +#endif |
| 47 | + return ret; |
| 48 | +} |
| 49 | + |
| 50 | +#elif defined(__APPLE__) |
| 51 | +#include <sys/disk.h> |
| 52 | + |
| 53 | +int blkgetsize(int fd, uint64_t *psize) |
| 54 | +{ |
| 55 | + unsigned long blocksize = 0; |
| 56 | + int ret = ioctl(fd, DKIOCGETBLOCKSIZE, &blocksize); |
| 57 | + if (!ret) { |
| 58 | + unsigned long nblocks; |
| 59 | + ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &nblocks); |
| 60 | + if (!ret) |
| 61 | + *psize = (uint64_t)nblocks * blocksize; |
| 62 | + } |
| 63 | + return ret; |
| 64 | +} |
| 65 | + |
| 66 | +#elif defined(__FreeBSD__) |
| 67 | +#include <sys/disk.h> |
| 68 | + |
| 69 | +int blkgetsize(int fd, uint64_t *psize) |
| 70 | +{ |
| 71 | + int ret = ioctl(fd, DIOCGMEDIASIZE, psize); |
| 72 | + return ret; |
| 73 | +} |
| 74 | + |
| 75 | +#else |
| 76 | +# error "Unable to query block device size: unsupported platform, please report." |
| 77 | +#endif |
| 78 | + |
| 79 | +/* ocaml/ocaml/unixsupport.c */ |
| 80 | +extern void uerror(char *cmdname, value cmdarg); |
| 81 | +#define Nothing ((value) 0) |
| 82 | + |
| 83 | +CAMLprim value stub_blkgetsize(value filename){ |
| 84 | + CAMLparam1(filename); |
| 85 | + CAMLlocal1(result); |
| 86 | + uint64_t size_in_bytes; |
| 87 | + int fd; |
| 88 | + int success = -1; |
| 89 | + |
| 90 | + const char *filename_c = strdup(String_val(filename)); |
| 91 | + |
| 92 | + enter_blocking_section(); |
| 93 | + fd = open(filename_c, O_RDONLY, 0); |
| 94 | + if (blkgetsize(fd, &size_in_bytes) == 0) |
| 95 | + success = 0; |
| 96 | + close(fd); |
| 97 | + leave_blocking_section(); |
| 98 | + |
| 99 | + free((void*)filename_c); |
| 100 | + |
| 101 | + if (fd == -1) uerror("open", filename); |
| 102 | + if (success == -1) uerror("BLKGETSIZE", filename); |
| 103 | + |
| 104 | + result = caml_copy_int64(size_in_bytes); |
| 105 | + CAMLreturn(result); |
| 106 | +} |
0 commit comments