summaryrefslogtreecommitdiffstats
path: root/ksim/monitors/filesystem/filesystemstats.cpp
blob: 5c1cf3a5a7f0bef3fc562b0e6b6676c1eb8207fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*  ksim - a system monitor for kde
 *
 *  Copyright (C) 2001  Robbie Ward <linuxphreak@gmx.co.uk>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "filesystemstats.h"

#include <tqglobal.h>
#include <tqfile.h>
#include <tqstringlist.h>

#include <kdebug.h>

#include <config.h>

#include <sys/types.h>
#include <sys/param.h>

#if defined(HAVE_SYS_STATVFS_H)
#include <sys/statvfs.h>
#elif defined( HAVE_SYS_STATFS_H )
#include <sys/statfs.h>
#endif
#ifdef HAVE_SYS_VFS_H
#include <sys/vfs.h>
#endif
#ifdef HAVE_SYS_MOUNT_H
#include <sys/mount.h>
#endif
#ifdef HAVE_MNTENT_H
#include <mntent.h>
#ifdef _HPUX_SOURCE
#define _PATH_MOUNTED MNTTAB
#endif
#endif
#ifdef HAVE_SYS_UCRED_H
#include <sys/ucred.h>
#endif
#ifdef HAVE_SYS_MNTTAB_H
#include <sys/mnttab.h>
#undef HAVE_MNTENT_H
#define _PATH_MOUNTED MNTTAB
#endif

#include <stdio.h>
#include <unistd.h>

#if defined(HAVE_STATVFS)
typedef struct statvfs ksim_statfs;
#elif defined( HAVE_STATFS )
typedef struct statfs ksim_statfs;
#else
typedef struct // fall back for (possibly) non-supported systems
{
  int f_blocks;
  int f_bfree;
} ksim_statfs;
#endif

int fsystemStats( const char * file, ksim_statfs & stats )
{
#if defined(HAVE_STATVFS)
  return statvfs( file, &stats );
#elif defined( HAVE_STATFS )
  return statfs( file, &stats );
#else // fall back for (possibly) non-supported systems
  (void)file;

  stats.f_blocks = 0;
  stats.f_bfree = 0;
  return -1;
#endif
}

#ifdef HAVE_SYS_MNTTAB_H
#define USE_MNTENT

typedef struct
{
  const char * mnt_dir;
  const char * mnt_fsname;
  const char * mnt_type;
} ksim_mntent;

FILE * setmntent( const char * mtab, const char * mode )
{
  return fopen( mtab, mode );
}

int endmntent( FILE * file )
{
  return fclose( file );
}

ksim_mntent * ksim_getmntent( FILE * file )
{
  ksim_mntent * entry;
  struct mnttab tab;

  if ( getmntent( file, &tab ) != 0 )
    return NULL;

  entry = new ksim_mntent;
  entry->mnt_dir = tab.mnt_mountp;
  entry->mnt_fsname = tab.mnt_special;
  entry->mnt_type = tab.mnt_fstype;
  return entry;
}

#define delete_mntent( x ) delete x
#elif defined( HAVE_MNTENT_H )
#define USE_MNTENT

// Dummy setup
typedef struct mntent ksim_mntent;
ksim_mntent * ksim_getmntent( FILE * file )
{
  return getmntent( file );
}

#define delete_mntent( x )
#elif defined( HAVE_GETMNTINFO )
#define USE_MNTINFO
#else
#define USE_FAILSAFE
#endif

FilesystemStats::List FilesystemStats::readEntries()
{
  List list;

#ifdef USE_MNTENT
  FILE * fp = setmntent( _PATH_MOUNTED, "r" );
  ksim_mntent * point;

  while ( ( point = ksim_getmntent( fp ) ) != 0 )
  {
    Entry entry;
    entry.dir = point->mnt_dir;
    entry.fsname = point->mnt_fsname;
    entry.type = point->mnt_type;
    list.append( entry );

    delete_mntent( point );
  }

  endmntent( fp );
#endif

#ifdef USE_MNTINFO
# ifdef GETMNTINFO_USES_STATVFS
  struct statvfs *sfs;
# else
  struct statfs *sfs;
# endif
  int fs_count;
  if ( ( fs_count = getmntinfo( &sfs, 0 ) ) != -1 )
  {
    for ( int i = 0; i < fs_count; i++ )
    {
      Entry entry;
      entry.dir = sfs[i].f_mntonname;
      entry.fsname = sfs[i].f_mntfromname;

#ifdef __osf__
      entry.type = getvfsbynumber(sfs[i].f_type);
#else
      entry.type = sfs[i].f_fstypename;
#endif

      list.append( entry );
    }
  }
#endif

#ifdef USE_FAILSAFE
  TQFile file( TQString::fromLatin1( _PATH_MOUNTED ) );

  if ( !file.open( IO_ReadOnly ) )
    return list;

  TQTextStream stream( &file );

  while ( !stream.atEnd() )
  {
    TQStringList line = TQStringList::split( " ", stream.readLine() );

    Entry entry;
    entry.dir = line[1].stripWhiteSpace();
    entry.fsname = line[0].stripWhiteSpace();
    entry.type = line[2].stripWhiteSpace();
    list.append( entry );
  }
#endif

  return list;
}

bool FilesystemStats::readStats( const TQString & mntPoint, int & totalBlocks, int & freeBlocks )
{
  ksim_statfs sysStats;
  if ( fsystemStats( TQFile::encodeName( mntPoint ).data(), sysStats ) < 0 )
  {
    kdError() << "While reading filesystem information for " << mntPoint << endl;
    totalBlocks = 0;
    freeBlocks = 0;
  }

  totalBlocks = sysStats.f_blocks;
  freeBlocks = sysStats.f_bfree;

  // Return true if our filesystem is statable
  return totalBlocks > 0;
}