File: open.cc

package info (click to toggle)
dnprogs 2.43.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,936 kB
  • ctags: 3,872
  • sloc: ansic: 24,686; cpp: 10,608; makefile: 769; sh: 551; awk: 13
file content (191 lines) | stat: -rw-r--r-- 4,728 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
    open.cc from librms

    Copyright (C) 1999-2001 Patrick Caulfield       patrick@tykepenguin.cix.co.uk

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <netdnet/dn.h>
#include <netdnet/dnetdb.h>
#include "connection.h"
#include "protocol.h"
#include "logging.h"
#include "rms.h"
#include "rmsp.h"

static char *open_error = NULL;

static void build_access_message(dap_access_message *acc, struct FAB *fab)
{
    if (fab->fab$b_fac) acc->set_fac(fab->fab$b_fac);
    if (fab->fab$b_shr) acc->set_shr(fab->fab$b_shr);
}

static void build_attrib_message(dap_attrib_message *att, struct FAB *fab)
{
    if (fab->fab$b_rfm) att->set_rfm((int)fab->fab$b_rfm);
    if (fab->fab$b_rat) att->set_rat((int)fab->fab$b_rat);
    //att->set_fop((int)fab->fab$l_fop);
}

RMSHANDLE rms_open(char *name, int mode, struct FAB *fab)
{
    struct accessdata_dn accessdata;
    char fname[256];
    char user[256];
    char node[256];
    char password[256];
    int verbose = 0;

    if (getenv("LIBRMS_VERBOSE"))
    {
        verbose = atoi(getenv("LIBRMS_VERBOSE"));
        if (!verbose) verbose = 1;
  	init_logging("librms", 'e', false);
    }

    dap_connection *conn = new dap_connection(verbose);
  
    errno = ENOENT;
    memset(&accessdata, 0, sizeof(accessdata));
    if (!conn->parse(name, accessdata, node, fname)) 
    {
	open_error = conn->get_error();
	delete conn;
	return NULL;
    }
  
    strcpy(user, (char *)accessdata.acc_user);
    strcpy(password, (char *)accessdata.acc_pass);


    if (!conn->connect(node, user, password, dap_connection::FAL_OBJECT)) 
    {
	open_error = conn->get_error();
	delete conn;
	return NULL;
    }
    if (!conn->exchange_config())
    {
	open_error = conn->get_error();
	delete conn;
	return NULL;
    }
    rms_conn *rc = new rms_conn(conn);

    // VMS needs an attrib message so give it one.
    dap_attrib_message att;
    att.set_rfm(dap_attrib_message::FB$VAR);
    if (fab) build_attrib_message(&att, fab);

    conn->set_blocked(true);     
    if (!att.write(*conn))
    {
	delete conn;
	return NULL;
    }

    dap_alloc_message all;
    if (!all.write(*conn))
    {
        delete conn;
        return NULL;
    }
    conn->set_blocked(false); 

    // Set up the access method
    dap_access_message acc;
    acc.set_accfunc(dap_access_message::OPEN);
    acc.set_display(dap_access_message::DISPLAY_MAIN_MASK);
    if (mode & O_CREAT) acc.set_accfunc(dap_access_message::CREATE);

    if (mode & O_RDWR || mode & O_CREAT)
	acc.set_fac(1<<dap_access_message::FB$PUT |
		    1<<dap_access_message::FB$GET |
		    1<<dap_access_message::FB$UPD |
		    1<<dap_access_message::FB$DEL);
    if (mode & O_WRONLY)
	acc.set_fac(1<<dap_access_message::FB$PUT |
		    1<<dap_access_message::FB$UPD |
		    1<<dap_access_message::FB$DEL);
    

    if (fab) build_access_message(&acc, fab);
    acc.set_filespec(fname);
    if (!acc.write(*conn))
    {
	delete conn;
	return NULL;
    }

    // Get reply to OPEN
    dap_message *m;
    int r = rms_getreply(rc, 1, fab, &m);
    if (r < 0) 
    {
	if (r == -2) delete m;
	open_error = rms_lasterror(rc);
	delete conn;
	delete rc;
	return NULL;
    }

  
    // Connect RAB
    dap_control_message ctl;
    ctl.set_ctlfunc(dap_control_message::CONNECT);
    ctl.write(*conn);

    // Get reply to CONNECT
    r = rms_getreply(rc, 1, fab, &m);
    if (r < 0) 
    {
	if (r == -2) delete m;
	open_error = rms_lasterror(rc);
	delete conn;
	delete rc;
	return NULL;
    }

    // Return new struct
    return (RMSHANDLE)rc;
}

// Open with text arguments
RMSHANDLE rms_t_open(char *name, int mode, char *options, ...)
{
    struct FAB fab;
    struct RAB rab;
    va_list ap;

    va_start(ap, options);
    if (!parse_options(NULL, options, &fab, &rab, ap)) return NULL;
    
    return rms_open(name, mode, &fab);
}

char *rms_openerror()
{
    return open_error;
}