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

« back to all changes in this revision

Viewing changes to ftsh/src/buffer.c

  • 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) 2003-2004 Douglas Thain and the University of Wisconsin
 
3
Copyright (C) 2005- The University of Notre Dame
 
4
This software is distributed under the GNU General Public License.
 
5
See the file COPYING for details.
 
6
*/
 
7
 
 
8
#include "hash_table.h"
 
9
#include "copy_stream.h"
 
10
#include "ftsh_error.h"
 
11
#include "full_io.h"
 
12
 
 
13
#include <stdlib.h>
 
14
#include <stdio.h>
 
15
#include <unistd.h>
 
16
#include <fcntl.h>
 
17
#include <errno.h>
 
18
#include <string.h>
 
19
#include <limits.h>
 
20
 
 
21
static struct hash_table *table=0;
 
22
 
 
23
static int buffer_init()
 
24
{
 
25
        if(!table) {
 
26
                table = hash_table_create( 127, hash_string );
 
27
                if(!table) return 0;
 
28
        }
 
29
        return 1;
 
30
}
 
31
 
 
32
int buffer_open_input( const char *tag )
 
33
{
 
34
        int fd;
 
35
 
 
36
        if(!buffer_init()) return 0;
 
37
 
 
38
        fd = (PTRINT_T) hash_table_lookup(table,tag);
 
39
        if(fd==0) {
 
40
                errno = ENOENT;
 
41
                return -1;
 
42
        }
 
43
 
 
44
        lseek(fd,0,SEEK_SET);
 
45
 
 
46
        return fd;
 
47
}
 
48
 
 
49
static int buffer_open( const char *tag, int do_truncate )
 
50
{
 
51
        char path[PATH_MAX];
 
52
        long fd;
 
53
        int old=0;
 
54
 
 
55
        if(!buffer_init()) return 0;
 
56
 
 
57
        if(!do_truncate) {
 
58
                fd = (long) hash_table_lookup(table,tag);
 
59
                if(fd!=0) {
 
60
                        lseek(fd,0,SEEK_END);
 
61
                        return fd;
 
62
                }
 
63
        }
 
64
 
 
65
        strcpy(path,"/tmp/ftsh.XXXXXX");
 
66
 
 
67
        fd = mkstemp(path);
 
68
        if(fd<0) return -1;
 
69
 
 
70
        unlink(path);
 
71
 
 
72
        old = (PTRINT_T) hash_table_remove(table,tag);
 
73
        if(old) close(old);
 
74
 
 
75
        if(!hash_table_insert(table,tag,(void*)fd)) {
 
76
                ftsh_fatal(0,"out of memory");
 
77
        }
 
78
 
 
79
 
 
80
        return fd;
 
81
}
 
82
 
 
83
 
 
84
int buffer_open_output( const char *tag )
 
85
{
 
86
        return buffer_open(tag,1);
 
87
}
 
88
 
 
89
int buffer_open_append( const char *tag )
 
90
{
 
91
        return buffer_open(tag,0);
 
92
}
 
93
 
 
94
char * buffer_load( const char *tag )
 
95
{
 
96
        FILE *stream=0;
 
97
        char *buffer;
 
98
        int nfd=-1, fd=-1;
 
99
        int save_errno;
 
100
 
 
101
        fd = buffer_open_input(tag);
 
102
        if(fd<0) goto failure;
 
103
 
 
104
        nfd = dup(fd);
 
105
        if(nfd<0) goto failure;
 
106
 
 
107
        stream = fdopen(nfd,"r");
 
108
        if(!stream) goto failure;
 
109
        if(!copy_stream_to_buffer(stream,&buffer)) goto failure;
 
110
        fclose(stream);
 
111
 
 
112
        return buffer;
 
113
 
 
114
        failure:
 
115
        save_errno = errno;
 
116
        if(stream) {
 
117
                fclose(stream);
 
118
        } else {
 
119
                if(nfd>=0) close(nfd);
 
120
        }
 
121
        errno = save_errno;
 
122
        return 0;
 
123
}
 
124
 
 
125
int buffer_save( const char *tag, const char *data )
 
126
{
 
127
        int fd;
 
128
 
 
129
        fd = buffer_open_output(tag);
 
130
        if(fd<0) return 0;
 
131
 
 
132
        if(full_write(fd,data,strlen(data))==strlen(data)) {
 
133
                return 1;
 
134
        } else {
 
135
                return 0;
 
136
        }
 
137
}
 
138
 
 
139
int buffer_delete( const char *tag )
 
140
{
 
141
        long fd;
 
142
 
 
143
        if(!buffer_init()) return 0;
 
144
 
 
145
        fd = (long) hash_table_remove(table,tag);
 
146
        if(fd!=0) close(fd);
 
147
 
 
148
        return 1;
 
149
}
 
150
 
 
151