~ubuntu-branches/ubuntu/vivid/cctools/vivid

« back to all changes in this revision

Viewing changes to parrot/src/pfs_dircache.cc

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2011-05-07 09:05:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110507090500-lqpmdtwndor6e7os
Tags: upstream-3.3.2
ImportĀ upstreamĀ versionĀ 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 2009- The University of Notre Dame
 
3
This software is distributed under the GNU General Public License.
 
4
See the file COPYING for details.
 
5
*/
 
6
 
 
7
#include "pfs_dircache.h"
 
8
#include "pfs_dir.h"
 
9
#include "pfs_types.h"
 
10
 
 
11
extern "C" {
 
12
#include "hash_table.h"
 
13
#include "stringtools.h"
 
14
#include "xmalloc.h"
 
15
}
 
16
 
 
17
#include <errno.h>
 
18
#include <stdio.h>
 
19
#include <stdlib.h>
 
20
#include <sys/mman.h>
 
21
#include <string.h>
 
22
 
 
23
pfs_dircache::pfs_dircache() 
 
24
{
 
25
        dircache_table = 0;
 
26
        dircache_path  = 0;
 
27
}
 
28
 
 
29
pfs_dircache::~pfs_dircache() 
 
30
{
 
31
        invalidate();
 
32
 
 
33
        if (dircache_table)
 
34
                hash_table_delete(dircache_table);
 
35
 
 
36
        if (dircache_path)
 
37
                free(dircache_path);
 
38
}
 
39
 
 
40
void pfs_dircache::invalidate()
 
41
{
 
42
        char *key;
 
43
        void *value;
 
44
 
 
45
        if (dircache_table) {
 
46
                hash_table_firstkey(dircache_table);
 
47
                while (hash_table_nextkey(dircache_table, &key, &value)) {
 
48
                        hash_table_remove(dircache_table, key);
 
49
                        free(value);
 
50
                }
 
51
        }
 
52
 
 
53
        if (dircache_path) {
 
54
                free(dircache_path);
 
55
                dircache_path = 0;
 
56
        }
 
57
}
 
58
 
 
59
void pfs_dircache::begin( const char *path )
 
60
{
 
61
        invalidate();
 
62
        dircache_path = xstrdup(path);
 
63
}
 
64
 
 
65
void pfs_dircache::insert( const char *name, struct pfs_stat *buf, pfs_dir *dir )
 
66
{
 
67
        char path[PFS_PATH_MAX];
 
68
        struct pfs_stat *copy;
 
69
        
 
70
        if (!dircache_table) dircache_table = hash_table_create(0, 0);
 
71
 
 
72
        dir->append(name);
 
73
 
 
74
        copy = (struct pfs_stat *)xxmalloc(sizeof(struct pfs_stat));
 
75
        *copy = *buf;
 
76
 
 
77
        sprintf(path, "%s/%s", dircache_path, string_basename(name));
 
78
        hash_table_insert(dircache_table, path, copy);
 
79
}
 
80
 
 
81
int pfs_dircache::lookup( const char *path, struct pfs_stat *buf )
 
82
{
 
83
        struct pfs_stat *value;
 
84
        int result = 0;
 
85
 
 
86
        if (!dircache_table) dircache_table = hash_table_create(0, 0);
 
87
 
 
88
        value = (struct pfs_stat *)hash_table_lookup(dircache_table, path);
 
89
        if (value) {
 
90
                *buf = *value;
 
91
                hash_table_remove(dircache_table, path);
 
92
                free(value);
 
93
                result = 1;
 
94
        }
 
95
 
 
96
        return (result);
 
97
}
 
98
 
 
99
// vim: ts=8 st=8 sw=8 ft=cpp