Switch to unified view

a/src/utils/pathut.cpp b/src/utils/pathut.cpp
1
#ifndef lint
1
#ifndef lint
2
static char rcsid[] = "@(#$Id: pathut.cpp,v 1.15 2007-02-06 14:16:43 dockes Exp $ (C) 2004 J.F.Dockes";
2
static char rcsid[] = "@(#$Id: pathut.cpp,v 1.16 2007-06-08 15:30:01 dockes Exp $ (C) 2004 J.F.Dockes";
3
#endif
3
#endif
4
/*
4
/*
5
 *   This program is free software; you can redistribute it and/or modify
5
 *   This program is free software; you can redistribute it and/or modify
6
 *   it under the terms of the GNU General Public License as published by
6
 *   it under the terms of the GNU General Public License as published by
7
 *   the Free Software Foundation; either version 2 of the License, or
7
 *   the Free Software Foundation; either version 2 of the License, or
...
...
21
#ifndef TEST_PATHUT
21
#ifndef TEST_PATHUT
22
#include <unistd.h>
22
#include <unistd.h>
23
#include <fcntl.h>
23
#include <fcntl.h>
24
#include <sys/param.h>
24
#include <sys/param.h>
25
#include <pwd.h>
25
#include <pwd.h>
26
#include <math.h>
26
27
27
#include <iostream>
28
#include <iostream>
28
#include <list>
29
#include <list>
29
#include <stack>
30
#include <stack>
30
#ifndef NO_NAMESPACES
31
#ifndef NO_NAMESPACES
...
...
32
using std::list;
33
using std::list;
33
using std::stack;
34
using std::stack;
34
#endif /* NO_NAMESPACES */
35
#endif /* NO_NAMESPACES */
35
36
36
#include "pathut.h"
37
#include "pathut.h"
38
39
#ifndef STATFS_INCLUDE
40
#define STATFS_INCLUDE <sys/vfs.h>
41
#endif
42
43
#include STATFS_INCLUDE
44
45
bool fsocc(const string &path, int *pc, long *blocks)
46
{
47
    struct statfs buf;
48
    if (statfs(path.c_str(), &buf) != 0) {
49
  return false;
50
    }
51
    // used blocks
52
    double fpc = 0.0;
53
#define FSOCC_USED (double(buf.f_blocks - buf.f_bfree))
54
#define FSOCC_TOTAVAIL (FSOCC_USED + double(buf.f_bavail))
55
    if (FSOCC_TOTAVAIL > 0) {
56
  fpc = 100.0 * FSOCC_USED / FSOCC_TOTAVAIL;
57
    }
58
    *pc = int(fpc);
59
    if (blocks) {
60
  *blocks = 0;
61
#define FSOCC_MB (1024*1024)
62
  if (buf.f_bsize > 0) {
63
      int ratio = buf.f_bsize > FSOCC_MB ? buf.f_bsize / FSOCC_MB :
64
      FSOCC_MB / buf.f_bsize;
65
66
      *blocks = buf.f_bsize > FSOCC_MB ? long(buf.f_bavail) * ratio :
67
      long(buf.f_bavail) / ratio;
68
  }
69
    }
70
    return true;
71
}
37
72
38
static const char *tmplocation()
73
static const char *tmplocation()
39
{
74
{
40
    const char *tmpdir = getenv("RECOLL_TMPDIR");
75
    const char *tmpdir = getenv("RECOLL_TMPDIR");
41
    if (!tmpdir)
76
    if (!tmpdir)
...
...
350
385
351
const string ttvec[] = {"/dir", "", "~", "~/sub", "~root", "~root/sub",
386
const string ttvec[] = {"/dir", "", "~", "~/sub", "~root", "~root/sub",
352
         "~nosuch", "~nosuch/sub"};
387
         "~nosuch", "~nosuch/sub"};
353
int nttvec = sizeof(ttvec) / sizeof(string);
388
int nttvec = sizeof(ttvec) / sizeof(string);
354
389
390
const char *thisprog;
391
355
int main(int argc, const char **argv)
392
int main(int argc, const char **argv)
356
{
393
{
394
    thisprog = *argv++;argc--;
395
357
    string s;
396
    string s;
358
    list<string>::const_iterator it;
397
    list<string>::const_iterator it;
359
#if 0
398
#if 0
360
    for (unsigned int i = 0;i < sizeof(tstvec) / sizeof(char *); i++) {
399
    for (unsigned int i = 0;i < sizeof(tstvec) / sizeof(char *); i++) {
361
    cout << tstvec[i] << " Father " << path_getfather(tstvec[i]) << endl;
400
    cout << tstvec[i] << " Father " << path_getfather(tstvec[i]) << endl;
...
...
386
    for (unsigned int i = 0; i < nttvec; i++) {
425
    for (unsigned int i = 0; i < nttvec; i++) {
387
    cout << "canon: '" << canontst[i] << "' -> '" << 
426
    cout << "canon: '" << canontst[i] << "' -> '" << 
388
        path_canon(canontst[i]) << "'" << endl;
427
        path_canon(canontst[i]) << "'" << endl;
389
    }
428
    }
390
#endif    
429
#endif    
391
#if 1
430
#if 0
392
    if (argc != 3) {
431
    if (argc != 2) {
393
    fprintf(stderr, "Usage: trpathut <dir> <pattern>\n");
432
    fprintf(stderr, "Usage: trpathut <dir> <pattern>\n");
394
    exit(1);
433
    exit(1);
395
    }
434
    }
396
    string dir=argv[1], pattern=argv[2];
435
    string dir = *argv++;argc--;
436
    string pattern =  *argv++;argc--;
397
    list<string> matched = path_dirglob(dir, pattern);
437
    list<string> matched = path_dirglob(dir, pattern);
398
    for (it = matched.begin(); it != matched.end();it++) {
438
    for (it = matched.begin(); it != matched.end();it++) {
399
    cout << *it << endl;
439
    cout << *it << endl;
400
    }
440
    }
401
#endif
441
#endif
402
442
443
#if 1
444
    if (argc != 1) {
445
  fprintf(stderr, "Usage: fsocc: trpathut <path>\n");
446
  exit(1);
447
    }
448
  string path = *argv++;argc--;
449
450
  int pc;
451
  long blocks;
452
  if (!fsocc(path, &pc, &blocks)) {
453
      fprintf(stderr, "fsocc failed\n");
454
      return 1;
455
  }
456
  printf("pc %d, megabytes %ld\n", pc, blocks);
457
#endif
403
    return 0;
458
    return 0;
404
}
459
}
405
460
406
#endif // TEST_PATHUT
461
#endif // TEST_PATHUT
407
462