Back to index

lshw  02.16
disk.cc
Go to the documentation of this file.
00001 #include "version.h"
00002 #include "disk.h"
00003 #include "osutils.h"
00004 #include "heuristics.h"
00005 #include "partitions.h"
00006 #include <sys/types.h>
00007 #include <sys/stat.h>
00008 #include <sys/ioctl.h>
00009 #include <fcntl.h>
00010 #include <unistd.h>
00011 
00012 //#include <linux/fs.h>
00013 
00014 __ID("@(#) $Id: disk.cc 2433 2012-01-10 22:01:30Z lyonel $");
00015 
00016 #ifndef BLKROGET
00017 #define BLKROGET   _IO(0x12,94)                   /* get read-only status (0 = read_write) */
00018 #endif
00019 #ifndef BLKGETSIZE
00020 #define BLKGETSIZE _IO(0x12,96)                   /* return device size */
00021 #endif
00022 #ifndef BLKGETSIZE64
00023 #define BLKGETSIZE64 _IOR(0x12,114,size_t)        /* size in bytes */
00024 #endif
00025 #ifndef BLKSSZGET
00026 #define BLKSSZGET  _IO(0x12,104)                  /* get block device sector size */
00027 #endif
00028 #ifndef BLKPBSZGET
00029 #define BLKPBSZGET _IO(0x12,123)
00030 #endif
00031 
00032 bool scan_disk(hwNode & n)
00033 {
00034   long size = 0;
00035   unsigned long long bytes = 0;
00036   int sectsize = 0;
00037   int physsectsize = 0;
00038 
00039   if (n.getLogicalName() == "")
00040     return false;
00041 
00042   int fd = open(n.getLogicalName().c_str(), O_RDONLY | O_NONBLOCK);
00043 
00044   if (fd < 0)
00045     return false;
00046 
00047   if (ioctl(fd, BLKPBSZGET, &physsectsize) != 0)
00048     physsectsize = 0;
00049   if(physsectsize)
00050     n.setConfig("sectorsize", physsectsize);
00051 
00052   if (n.getSize() == 0)
00053   {
00054     if(ioctl(fd, BLKGETSIZE64, &bytes) == 0)
00055     {
00056       n.setSize(bytes);
00057     }
00058     else
00059     {
00060       if (ioctl(fd, BLKGETSIZE, &size) != 0)
00061         size = 0;
00062       if (ioctl(fd, BLKSSZGET, &sectsize) != 0)
00063         sectsize = 0;
00064 
00065       if ((size > 0) && (sectsize > 0))
00066         n.setSize((unsigned long long) size * (unsigned long long) sectsize);
00067     }
00068   }
00069 
00070   close(fd);
00071 
00072   if(n.getSize()>=0)
00073   {
00074     n.addHint("icon", string("disc"));
00075     scan_partitions(n);
00076   }
00077 
00078   return true;
00079 }