"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 2 | /* |
| 3 | * getsectsize.c --- get the sector size of a device. |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 4 | * |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 5 | * Copyright (C) 1995, 1995 Theodore Ts'o. |
| 6 | * Copyright (C) 2003 VMware, Inc. |
| 7 | * |
| 8 | * %Begin-Header% |
| 9 | * This file may be redistributed under the terms of the GNU Public |
| 10 | * License. |
| 11 | * %End-Header% |
| 12 | */ |
| 13 | |
| 14 | #include <stdio.h> |
| 15 | #if HAVE_UNISTD_H |
| 16 | #include <unistd.h> |
| 17 | #endif |
| 18 | #if HAVE_ERRNO_H |
| 19 | #include <errno.h> |
| 20 | #endif |
| 21 | #include <fcntl.h> |
| 22 | #ifdef HAVE_LINUX_FD_H |
| 23 | #include <sys/ioctl.h> |
| 24 | #include <linux/fd.h> |
| 25 | #endif |
| 26 | |
Mike Frysinger | 874af85 | 2006-03-08 07:03:27 +0000 | [diff] [blame] | 27 | #if defined(__linux__) && defined(_IO) && !defined(BLKSSZGET) |
Mike Frysinger | 1fd98e0 | 2005-05-09 22:10:42 +0000 | [diff] [blame] | 28 | #define BLKSSZGET _IO(0x12,104)/* get block device sector size */ |
| 29 | #endif |
| 30 | |
| 31 | #include "ext2_fs.h" |
| 32 | #include "ext2fs.h" |
| 33 | |
| 34 | /* |
| 35 | * Returns the number of blocks in a partition |
| 36 | */ |
| 37 | errcode_t ext2fs_get_device_sectsize(const char *file, int *sectsize) |
| 38 | { |
| 39 | int fd; |
| 40 | |
| 41 | #ifdef CONFIG_LFS |
| 42 | fd = open64(file, O_RDONLY); |
| 43 | #else |
| 44 | fd = open(file, O_RDONLY); |
| 45 | #endif |
| 46 | if (fd < 0) |
| 47 | return errno; |
| 48 | |
| 49 | #ifdef BLKSSZGET |
| 50 | if (ioctl(fd, BLKSSZGET, sectsize) >= 0) { |
| 51 | close(fd); |
| 52 | return 0; |
| 53 | } |
| 54 | #endif |
| 55 | *sectsize = 0; |
| 56 | close(fd); |
| 57 | return 0; |
| 58 | } |