getsectsize.c 1.0 KB

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