getsectsize.c revision efc6f628e15de95bcd13e4f0ee223cb42115d520
1/* 2 * getsectsize.c --- get the sector size of a device. 3 * 4 * Copyright (C) 1995, 1995 Theodore Ts'o. 5 * Copyright (C) 2003 VMware, Inc. 6 * 7 * %Begin-Header% 8 * This file may be redistributed under the terms of the GNU Public 9 * License. 10 * %End-Header% 11 */ 12 13#define _LARGEFILE_SOURCE 14#define _LARGEFILE64_SOURCE 15 16#include <stdio.h> 17#if HAVE_UNISTD_H 18#include <unistd.h> 19#endif 20#if HAVE_ERRNO_H 21#include <errno.h> 22#endif 23#include <fcntl.h> 24#ifdef HAVE_LINUX_FD_H 25#include <sys/ioctl.h> 26#include <linux/fd.h> 27#endif 28 29#if defined(__linux__) && defined(_IO) && !defined(BLKSSZGET) 30#define BLKSSZGET _IO(0x12,104)/* get block device sector size */ 31#endif 32 33#include "ext2_fs.h" 34#include "ext2fs.h" 35 36/* 37 * Returns the number of blocks in a partition 38 */ 39errcode_t ext2fs_get_device_sectsize(const char *file, int *sectsize) 40{ 41 int fd; 42 43#ifdef HAVE_OPEN64 44 fd = open64(file, O_RDONLY); 45#else 46 fd = open(file, O_RDONLY); 47#endif 48 if (fd < 0) 49 return errno; 50 51#ifdef BLKSSZGET 52 if (ioctl(fd, BLKSSZGET, sectsize) >= 0) { 53 close(fd); 54 return 0; 55 } 56#endif 57 *sectsize = 0; 58 close(fd); 59 return 0; 60} 61