Logo Search packages:      
Sourcecode: gaffitter version File versions  Download package

Params::UBigInt DiskUsage::GetSize ( const char *  filename  )  [static]

Returns the size of the given file or directory.

If the given file/directory could not be read then GetSize returns "0". Note: 0-sized files returns m_block_size.

Definition at line 59 of file DiskUsage.cc.

References AllocationSize(), DepthFirstTraversal(), and m_max_path_name.

{
   struct stat st;
   if (lstat(filename, &st) != 0)
   {
      cerr << "> Could not read: " << filename << endl;
      return 0;
   }
      
   if (S_ISDIR(st.st_mode))
   {
      char saved_path[m_max_path_name], tmp_path[m_max_path_name];

      // saves current path before chdir to 'filename' directory
      if (getcwd(saved_path, m_max_path_name-1) == NULL)
      {
         cerr << "> Path name too long; maximum " << m_max_path_name-1 
              << " chars." << endl;
         return 0;
      }

      if (chdir(filename) != 0) 
      {
         cerr << "> Could not chdir to: " << filename << endl;
         return 0;
      }

      if (getcwd(tmp_path, m_max_path_name-1) == NULL)
      {
         cerr << "> Path name too long; maximum " << m_max_path_name-1 
              << " chars." << endl;
         return 0;
      }

      if (lstat(tmp_path, &st) != 0)
      {
         cerr << "> Could not read dir: " << tmp_path << endl;
         return 0;
      }

      Params::UBigInt total = 
                AllocationSize(static_cast<Params::UBigInt>(st.st_size)) 
                                         + DepthFirstTraversal(tmp_path);

      // return to original path (important!)
      chdir(saved_path);

      return total;
   }
   else // ISREG, ISLNK, ISFIFO, ISBLK, ...
      return AllocationSize(static_cast<Params::UBigInt>(st.st_size));
}


Generated by  Doxygen 1.6.0   Back to index