Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2011
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. /*
  2. * Copyright (C) 2010 Canonical
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. */
  19.  
  20. /*
  21. * Author Colin Ian King, [email protected]
  22. */
  23.  
  24.  
  25.  
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <fcntl.h>
  31.  
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <sys/ioctl.h>
  35.  
  36. #include <linux/fs.h>
  37. #include "fiemap.h"
  38.  
  39. #define FS_IOC_FIEMAP _IOWR('f', 11, struct fiemap)
  40.  
  41. void syntax(char **argv)
  42. {
  43. fprintf(stderr, "%s [filename]...\n",argv[0]);
  44. }
  45.  
  46. struct fiemap *read_fiemap(int fd)
  47. {
  48. struct fiemap *fiemap;
  49. int extents_size;
  50.  
  51. if ((fiemap = (struct fiemap*)malloc(sizeof(struct fiemap))) == NULL) {
  52. fprintf(stderr, "Out of memory allocating fiemap\n");
  53. return NULL;
  54. }
  55. memset(fiemap, 0, sizeof(struct fiemap));
  56.  
  57. fiemap->fm_start = 0;
  58. fiemap->fm_length = 2*1024*1024; /* Lazy */
  59. fiemap->fm_flags = 0;
  60. fiemap->fm_extent_count = 0;
  61. fiemap->fm_mapped_extents = 0;
  62.  
  63. /* Find out how many extents there are */
  64. if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0) {
  65. fprintf(stderr, "fiemap ioctl() failed\n");
  66. return NULL;
  67. }
  68.  
  69. /* Read in the extents */
  70. extents_size = sizeof(struct fiemap_extent) *
  71. (fiemap->fm_mapped_extents);
  72.  
  73. /* Resize fiemap to allow us to read in the extents */
  74. if ((fiemap = (struct fiemap*)realloc(fiemap,sizeof(struct fiemap) +
  75. extents_size)) == NULL) {
  76. fprintf(stderr, "Out of memory allocating fiemap\n");
  77. return NULL;
  78. }
  79.  
  80. memset(fiemap->fm_extents, 0, extents_size);
  81. fiemap->fm_extent_count = fiemap->fm_mapped_extents;
  82. fiemap->fm_mapped_extents = 0;
  83.  
  84. if (ioctl(fd, FS_IOC_FIEMAP, fiemap) < 0) {
  85. fprintf(stderr, "fiemap ioctl() failed\n");
  86. return NULL;
  87. }
  88.  
  89. return fiemap;
  90. }
  91.  
  92. void dump_fiemap(struct fiemap *fiemap, char *filename)
  93. {
  94. int i;
  95.  
  96. printf("File %s has %d extents:\n",filename, fiemap->fm_mapped_extents);
  97.  
  98. printf("#\tLogical Physical Length Flags\n");
  99. for (i=0;i<fiemap->fm_mapped_extents;i++) {
  100. printf("%d:\t%-16.16llx %-16.16llx %-16.16llx %-4.4x\n",
  101. i,
  102. fiemap->fm_extents[i].fe_logical,
  103. fiemap->fm_extents[i].fe_physical,
  104. fiemap->fm_extents[i].fe_length,
  105. fiemap->fm_extents[i].fe_flags);
  106. }
  107. printf("\n");
  108. }
  109.  
  110. int main(int argc, char **argv)
  111. {
  112. int i;
  113.  
  114. if (argc < 2) {
  115. syntax(argv);
  116. exit(EXIT_FAILURE);
  117. }
  118.  
  119. for (i=1;i<argc;i++) {
  120. int fd;
  121.  
  122. if ((fd = open(argv[i], O_RDONLY)) < 0) {
  123. fprintf(stderr, "Cannot open file %s\n", argv[i]);
  124. }
  125. else {
  126. struct fiemap *fiemap;
  127.  
  128. if ((fiemap = read_fiemap(fd)) != NULL)
  129. dump_fiemap(fiemap, argv[i]);
  130. close(fd);
  131. }
  132. }
  133. exit(EXIT_SUCCESS);
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement