Switch to unified view

a/src/utils/pathut.cpp b/src/utils/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(":");
...
...
875
#include <iostream>
855
#include <iostream>
876
using namespace std;
856
using namespace std;
877
857
878
#include "pathut.h"
858
#include "pathut.h"
879
859
880
void path_to_thumb(const string& _input)
881
{
882
    string input(_input);
883
    // Make absolute path if needed
884
    if (input[0] != '/') {
885
        input = path_absolute(input);
886
    }
887
888
    input = string("file://") + path_canon(input);
889
890
    string path;
891
    //path = url_encode(input, 7);
892
    thumbPathForUrl(input, 7, path);
893
    cout << path << endl;
894
}
895
896
const char *tstvec[] = {"", "/", "/dir", "/dir/", "/dir1/dir2",
860
const char *tstvec[] = {"", "/", "/dir", "/dir/", "/dir1/dir2",
897
                        "/dir1/dir2",
861
                        "/dir1/dir2",
898
                        "./dir", "./dir1/", "dir", "../dir", "/dir/toto.c",
862
                        "./dir", "./dir1/", "dir", "../dir", "/dir/toto.c",
899
                        "/dir/.c", "/dir/toto.txt", "toto.txt1"
863
                        "/dir/.c", "/dir/toto.txt", "toto.txt1"
900
                       };
864
                       };
...
...
990
    pidfile.close();
954
    pidfile.close();
991
    pidfile.remove();
955
    pidfile.remove();
992
#endif
956
#endif
993
957
994
#if 0
958
#if 0
995
    if (argc > 1) {
996
        cerr <<  "Usage: thumbpath <filepath>" << endl;
997
        exit(1);
998
    }
999
    string input;
1000
    if (argc == 1) {
1001
        input = *argv++;
1002
        if (input.empty())  {
1003
            cerr << "Usage: thumbpath <filepath>" << endl;
1004
            exit(1);
1005
        }
1006
        path_to_thumb(input);
1007
    } else {
1008
        while (getline(cin, input)) {
1009
            path_to_thumb(input);
1010
        }
1011
    }
1012
1013
1014
    exit(0);
1015
#endif
1016
1017
#if 0
1018
    if (argc != 1) {
959
    if (argc != 1) {
1019
        cerr << "Usage: trpathut <filename>" << endl;
960
        cerr << "Usage: trpathut <filename>" << endl;
1020
        exit(1);
961
        exit(1);
1021
    }
962
    }
1022
    string fn = *argv++;
963
    string fn = *argv++;
...
...
1024
    string ext = path_suffix(fn);
965
    string ext = path_suffix(fn);
1025
    cout << "Suffix: [" << ext << "]" << endl;
966
    cout << "Suffix: [" << ext << "]" << endl;
1026
    return 0;
967
    return 0;
1027
#endif
968
#endif
1028
969
1029
#if 1
970
#if 0
1030
    if (argc != 1) {
971
    if (argc != 1) {
1031
        cerr << "Usage: trpathut url" << endl;
972
        cerr << "Usage: trpathut url" << endl;
1032
        exit(1);
973
        exit(1);
1033
    }
974
    }
1034
    string url = *argv++;
975
    string url = *argv++;
...
...
1036
977
1037
    cout << "File: [" << fileurltolocalpath(url) << "]\n";
978
    cout << "File: [" << fileurltolocalpath(url) << "]\n";
1038
    return 0;
979
    return 0;
1039
#endif
980
#endif
1040
981
982
#if 1
983
    if (argc != 1) {
984
        cerr << "Usage: trpathut path" << endl;
985
        exit(1);
986
    }
987
    string path = *argv++;
988
    argc--;
989
990
    int pc;
991
    long long avmbs;
992
    if (fsocc(path, &pc, &avmbs)) {
993
        cout << "Percent occup " << pc << " avmbs " << avmbs << endl;
994
        return 0;
995
    } else {
996
        cerr << "fsocc failed\n";
997
        return 1;
998
    }
999
#endif
1000
1001
1041
1002
1042
}
1003
}
1043
1004
1044
#endif // TEST_PATHUT
1005
#endif // TEST_PATHUT
1045
1006