Switch to unified view

a/src/pathut.cpp b/src/pathut.cpp
...
...
21
#else
21
#else
22
#include "config.h"
22
#include "config.h"
23
#endif
23
#endif
24
24
25
#include <stdio.h>
25
#include <stdio.h>
26
#include <math.h>
27
#include <errno.h>
28
26
#ifdef _WIN32
29
#ifdef _WIN32
27
#include "dirent.h"
30
#include "dirent.h"
28
#include "safefcntl.h"
31
#include "safefcntl.h"
29
#include "safeunistd.h"
32
#include "safeunistd.h"
30
#include "safewindows.h"
33
#include "safewindows.h"
31
#include "safesysstat.h"
34
#include "safesysstat.h"
32
#else
35
36
#else // Not windows ->
33
#include <fcntl.h>
37
#include <fcntl.h>
34
#include <unistd.h>
38
#include <unistd.h>
35
#include <sys/param.h>
39
#include <sys/param.h>
36
#include <pwd.h>
40
#include <pwd.h>
37
#include <sys/file.h>
41
#include <sys/file.h>
38
#include <sys/stat.h>
42
#include <sys/stat.h>
39
#include <dirent.h>
43
#include <dirent.h>
40
#endif
44
#include <sys/statvfs.h>
41
#include <math.h>
42
#include <errno.h>
43
#include <sys/types.h>
45
#include <sys/types.h>
44
46
45
// Let's include all files where statfs can be defined and hope for no
46
// conflict...
47
#ifdef HAVE_SYS_MOUNT_H
48
#include <sys/mount.h>
49
#endif
50
#ifdef HAVE_SYS_STATFS_H
51
#include <sys/statfs.h>
52
#endif
53
#ifdef HAVE_SYS_STATVFS_H
54
#include <sys/statvfs.h>
55
#endif
56
#ifdef HAVE_SYS_VFS_H
57
#include <sys/vfs.h>
58
#endif
47
#endif
59
48
60
#include <cstdlib>
49
#include <cstdlib>
61
#include <cstring>
50
#include <cstring>
62
#include <iostream>
51
#include <iostream>
...
...
99
    }
88
    }
100
    return false;
89
    return false;
101
}
90
}
102
#endif
91
#endif
103
92
104
#if defined(HAVE_SYS_MOUNT_H) || defined(HAVE_SYS_STATFS_H) || \
105
    defined(HAVE_SYS_STATVFS_H) || defined(HAVE_SYS_VFS_H) || defined(_WIN32)
106
bool fsocc(const string& path, int *pc, long long *avmbs)
93
bool fsocc(const string& path, int *pc, long long *avmbs)
107
{
94
{
108
    static const int FSOCC_MB = 1024 * 1024;
95
    static const int FSOCC_MB = 1024 * 1024;
109
#ifdef _WIN32
96
#ifdef _WIN32
110
    ULARGE_INTEGER freebytesavail;
97
    ULARGE_INTEGER freebytesavail;
...
...
118
    }
105
    }
119
    if (avmbs) {
106
    if (avmbs) {
120
        *avmbs = int(totalbytes.QuadPart / FSOCC_MB);
107
        *avmbs = int(totalbytes.QuadPart / FSOCC_MB);
121
    }
108
    }
122
    return true;
109
    return true;
123
#else
110
#else // not windows ->
124
#ifdef sun
111
125
    struct statvfs buf;
112
    struct statvfs buf;
126
    if (statvfs(path.c_str(), &buf) != 0) {
113
    if (statvfs(path.c_str(), &buf) != 0) {
127
        return false;
114
        return false;
128
    }
115
    }
129
#else
130
    struct statfs buf;
131
    if (statfs(path.c_str(), &buf) != 0) {
132
        return false;
133
    }
134
#endif
135
116
136
    // used blocks
137
    double fpc = 0.0;
138
#define FSOCC_USED (double(buf.f_blocks - buf.f_bfree))
139
#define FSOCC_TOTAVAIL (FSOCC_USED + double(buf.f_bavail))
140
    if (FSOCC_TOTAVAIL > 0) {
141
        fpc = 100.0 * FSOCC_USED / FSOCC_TOTAVAIL;
142
    }
143
    if (pc) {
117
    if (pc) {
118
        double fsocc_used = double(buf.f_blocks - buf.f_bfree);
119
        double fsocc_totavail = fsocc_used + double(buf.f_bavail);
120
        double fpc = 100.0;
121
        if (fsocc_totavail > 0) {
122
            fpc = 100.0 * fsocc_used / fsocc_totavail;
123
        }
144
        *pc = int(fpc);
124
        *pc = int(fpc);
145
    }
125
    }
146
    if (avmbs) {
126
    if (avmbs) {
147
        *avmbs = 0;
127
        *avmbs = 0;
148
        if (buf.f_bsize > 0) {
128
        if (buf.f_bsize > 0) {
149
            int ratio = buf.f_bsize > FSOCC_MB ? buf.f_bsize / FSOCC_MB :
129
            int ratio = buf.f_frsize > FSOCC_MB ? buf.f_frsize / FSOCC_MB :
150
                        FSOCC_MB / buf.f_bsize;
130
                        FSOCC_MB / buf.f_frsize;
151
131
152
            *avmbs = buf.f_bsize > FSOCC_MB ?
132
            *avmbs = buf.f_frsize > FSOCC_MB ?
153
                     ((long long)buf.f_bavail) * ratio :
133
                     ((long long)buf.f_bavail) * ratio :
154
                     ((long long)buf.f_bavail) / ratio;
134
                     ((long long)buf.f_bavail) / ratio;
155
        }
135
        }
156
    }
136
    }
157
    return true;
137
    return true;
158
#endif
138
#endif
159
}
139
}
160
#endif // we have found an appropriate include file
140
161
141
162
string path_PATHsep()
142
string path_PATHsep()
163
{
143
{
164
    static const string w(";");
144
    static const string w(";");
165
    static const string u(":");
145
    static const string u(":");
...
...
1024
    string ext = path_suffix(fn);
1004
    string ext = path_suffix(fn);
1025
    cout << "Suffix: [" << ext << "]" << endl;
1005
    cout << "Suffix: [" << ext << "]" << endl;
1026
    return 0;
1006
    return 0;
1027
#endif
1007
#endif
1028
1008
1029
#if 1
1009
#if 0
1030
    if (argc != 1) {
1010
    if (argc != 1) {
1031
        cerr << "Usage: trpathut url" << endl;
1011
        cerr << "Usage: trpathut url" << endl;
1032
        exit(1);
1012
        exit(1);
1033
    }
1013
    }
1034
    string url = *argv++;
1014
    string url = *argv++;
...
...
1036
1016
1037
    cout << "File: [" << fileurltolocalpath(url) << "]\n";
1017
    cout << "File: [" << fileurltolocalpath(url) << "]\n";
1038
    return 0;
1018
    return 0;
1039
#endif
1019
#endif
1040
1020
1021
#if 1
1022
    if (argc != 1) {
1023
        cerr << "Usage: trpathut path" << endl;
1024
        exit(1);
1025
    }
1026
    string path = *argv++;
1027
    argc--;
1028
1029
    int pc;
1030
    long long avmbs;
1031
    if (fsocc(path, &pc, &avmbs)) {
1032
        cout << "Percent occup " << pc << " avmbs " << avmbs << endl;
1033
        return 0;
1034
    } else {
1035
        cerr << "fsocc failed\n";
1036
        return 1;
1037
    }
1038
#endif
1039
1040
1041
1041
1042
}
1042
}
1043
1043
1044
#endif // TEST_PATHUT
1044
#endif // TEST_PATHUT
1045
1045