File: mountserver.pike

package info (click to toggle)
roxen 1.2beta2-3
  • links: PTS
  • area: contrib
  • in suites: slink
  • size: 16,920 kB
  • ctags: 8,589
  • sloc: ansic: 89,632; asm: 8,431; sh: 2,915; makefile: 1,784; cpp: 377
file content (244 lines) | stat: -rw-r--r-- 5,096 bytes parent folder | download | duplicates (4)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// This is a roxen module. Copyright  1996 - 1998, Idonex AB.

string cvs_version = "$Id: mountserver.pike,v 1.4 1998/03/11 19:42:45 neotron Exp $";
// Mounts a virtual server on a location in the virtual filesystem of
// another one (or, infact, the same one, but that is probably quite
// useless).
#include <module.h>

#define TYPE_SERVER TYPE_STRING
#define DEBUG 

inherit "module";

mixed *register_module()
{
  return ({ 
    MODULE_LOCATION,
    "Server as a filesystem", 
    ("This module enables you to mount a virtual server as a filesystem. "
     "")
      });
}



void create()
{
  defvar("server", "NONE", "Server", TYPE_SERVER, 
	 "The virtual server to mount.");

  defvar("mountpoint", "NONE", "Mount point", TYPE_LOCATION,
	 "The mountpoint in the namespace of this virtual server.");
}

string fmts;
object config;

string query_location() { return QUERY(mountpoint); }

object find_configuration( string what )
{
  object c;
  foreach(roxen->configurations, c)
    if(objectp(c))
      if(lower_case(c->name) == lower_case(what))
	return c;
}

array find_dir( string f, object id )
{
  mixed res;
  object oc;
  string oq;

  if(!config)  config = find_configuration( query("server") );
  oq = id->not_query;
  oc = id->conf;
  id->not_query = f;
  id->conf = config;
  res = roxen->find_dir( f, id );
  id->not_query = oq;
  id->conf = oc;
#ifdef DEBUG
  if(res)
    perror(sprintf("find_dir:: %s == %{%s, %}\n",f,res));
  else
    perror(sprintf("find_dir:: %s No such dir.\n",f));
    
#endif
  return res;
}

array stat_file( string f, object id )
{
  mixed res;
  object oc;
  string oq;
  if(!config) 
    config = find_configuration( query("server") );
  oq = id->not_query;
  oc = id->conf;
  id->not_query = f;
  id->conf = config;
  res = roxen->stat_file( f, id );
  id->not_query = oq;
  id->conf = oc;
#ifdef DEBUG
  if(arrayp(res))
    perror(sprintf("stat_file:: %s; res = %{%O, %}\n",f,res));
  else
    perror("stat_file:: "+f+", no such file\n");
#endif
  return res;
}

array real_file( string f, object id )
{
  mixed res;
  object oc;
  string oq;
  if(!config) 
    config = find_configuration( query("server") );
  oq = id->not_query;
  oc = id->conf;
  id->not_query = f;
  id->conf = config;
  res = roxen->real_file( f, id );
  id->not_query = oq;
  id->conf = oc;
#ifdef DEBUG
  perror(sprintf("real_file:: Location = %s; LocalLocation = %s; res = %O\n",oq,f,res));
#endif
  return res;
}

string mp;

void start()
{
  if(strlen(mp=query("mountpoint")))
  {
    if(query("mountpoint")[-1] == '/')
    {
      mp = mp[..strlen(mp)-2];
      set("mountpoint", mp);
    }
  }
  config = find_configuration( query("server") );
}

inline nomask private static string tags(mapping from)
{
  string t, res="";
  foreach(indices(from), t)
    res += " " + t+"=\""+from[t]+"\"";
  return res;
}

inline nomask private static string fix_it(string from)
{
  string pre;
  if(strlen(from) && from[0]=='/' && search(from, mp))
  {
    if(sscanf(from, "/<%s>%s", pre, from)==2)
    {
      if(search(from, mp))
      {
	if(pre)
	  return "/<"+pre+">" + mp + from;
      } else
	from = "/<"+pre+">/" + from;
    }
    if(sscanf(from, "/(%s)%s", pre, from)==2)
    {
      if(search(from, mp))
      {
	if(pre)
	  return "/("+pre+")" + mp + from;
      } else
	from = "/("+pre+")/" + from;
    }
    return QUERY(mountpoint) + from;
  }
  return from;
}

string do_href(string t, mapping m) 
{
  if(m->__parsed) return 0;
  if(!m->href) return 0;
  m->__parsed="yes";
  m->href = fix_it(m->href);
  return "<"+t+tags(m)+">";
}

string do_src(string t, mapping m)
{
  if(m->__parsed)    return 0;
  if(!m->src) return 0;
  m->__parsed="yes";
  m->src = fix_it(m->src);
  return "<"+t+tags(m)+">";
}

string do_action(string t, mapping m)
{
  if(m->__parsed)    return 0;
  if(!m->action) return 0;
  m->__parsed="yes";
  m->action = fix_it(m->action);
  return "<"+t+tags(m)+">";
}

string do_background(string t, mapping m)
{
  if(m->__parsed)    return 0;
  if(!m->background) return 0;
  m->__parsed="yes";
  m->background = fix_it(m->background);
  return "<"+t+tags(m)+">";
}

string fix_absolute(string from)
{
  string data;
  if ((data = parse_html(from, ([ "base":do_href ]), ([]))) == from) {
    return parse_html(from, ([ "a":do_href, "img":do_src, "form":do_action, 
			       "input":do_src, "body":do_background  ]), ([]));
  } else {
    return data;
  }
}

mapping find_file(string f, object id)
{
  mapping res;
  object oc;
  string oq;
  if(!config) 
    config = find_configuration( query("server") );

  if(!config) return 0;

  oq = id->not_query;
  oc = id->conf;
  id->not_query = f;
  id->conf = config;
  res = roxen->get_file( id, 1 );
  id->not_query = oq;
  id->conf = oc;
#ifdef DEBUG
  perror(sprintf("find_file:: Location = %s; LocalLocation = %s; res = %O\n",oq,f,res));
#endif
  if(intp(res)) return res;
  if(mappingp(res) && res->data)
    res->data = fix_absolute( res->data );
  roxen->current_configuration = oc;
  return res;
}

string comment()
{
  return "The server "+QUERY(server)+" mounted on "+QUERY(mountpoint);
}