/ src / core / disk.cc
disk.cc
 1  #include "version.h"
 2  #include "disk.h"
 3  #include "osutils.h"
 4  #include "heuristics.h"
 5  #include "partitions.h"
 6  #include <sys/types.h>
 7  #include <sys/stat.h>
 8  #include <sys/ioctl.h>
 9  #include <fcntl.h>
10  #include <unistd.h>
11  
12  //#include <linux/fs.h>
13  
14  __ID("@(#) $Id$");
15  
16  #ifndef BLKROGET
17  #define BLKROGET   _IO(0x12,94)                   /* get read-only status (0 = read_write) */
18  #endif
19  #ifndef BLKGETSIZE
20  #define BLKGETSIZE _IO(0x12,96)                   /* return device size */
21  #endif
22  #ifndef BLKGETSIZE64
23  #define BLKGETSIZE64 _IOR(0x12,114,size_t)        /* size in bytes */
24  #endif
25  #ifndef BLKSSZGET
26  #define BLKSSZGET  _IO(0x12,104)                  /* get block device sector size */
27  #endif
28  #ifndef BLKPBSZGET
29  #define BLKPBSZGET _IO(0x12,123)
30  #endif
31  
32  bool scan_disk(hwNode & n)
33  {
34    long size = 0;
35    unsigned long long bytes = 0;
36    int sectsize = 0;
37    int physsectsize = 0;
38  
39    if (n.getLogicalName() == "")
40      return false;
41  
42    int fd = open(n.getLogicalName().c_str(), O_RDONLY | O_NONBLOCK);
43  
44    if (fd < 0)
45      return false;
46  
47    if (ioctl(fd, BLKPBSZGET, &physsectsize) != 0)
48      physsectsize = 0;
49    if(physsectsize)
50      n.setConfig("sectorsize", physsectsize);
51  
52    if (ioctl(fd, BLKSSZGET, &sectsize) < 0)
53      sectsize = 0;
54    if (sectsize)
55      n.setConfig("logicalsectorsize", sectsize);
56  
57    if (n.getSize() == 0)
58    {
59      if(ioctl(fd, BLKGETSIZE64, &bytes) == 0)
60      {
61        n.setSize(bytes);
62      }
63      else
64      {
65        if (ioctl(fd, BLKGETSIZE, &size) != 0)
66          size = 0;
67        
68        if ((size > 0) && (sectsize > 0)){
69          n.setSize((unsigned long long) size * (unsigned long long) sectsize);
70        }
71      }
72    }
73  
74    close(fd);
75  
76    if(n.getSize()>=0)
77    {
78      n.addHint("icon", string("disc"));
79      scan_partitions(n);
80    }
81  
82    return true;
83  }