getsectsize.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. #include <stdio.h>
  13. #if HAVE_UNISTD_H
  14. #include <unistd.h>
  15. #endif
  16. #if HAVE_ERRNO_H
  17. #include <errno.h>
  18. #endif
  19. #include <fcntl.h>
  20. #ifdef HAVE_LINUX_FD_H
  21. #include <sys/ioctl.h>
  22. #include <linux/fd.h>
  23. #endif
  24. #if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
  25. #define BLKSSZGET _IO(0x12,104)/* get block device sector size */
  26. #endif
  27. #include "ext2_fs.h"
  28. #include "ext2fs.h"
  29. /*
  30. * Returns the number of blocks in a partition
  31. */
  32. errcode_t ext2fs_get_device_sectsize(const char *file, int *sectsize)
  33. {
  34. int fd;
  35. #ifdef CONFIG_LFS
  36. fd = open64(file, O_RDONLY);
  37. #else
  38. fd = open(file, O_RDONLY);
  39. #endif
  40. if (fd < 0)
  41. return errno;
  42. #ifdef BLKSSZGET
  43. if (ioctl(fd, BLKSSZGET, sectsize) >= 0) {
  44. close(fd);
  45. return 0;
  46. }
  47. #endif
  48. *sectsize = 0;
  49. close(fd);
  50. return 0;
  51. }