File: System.pike

package info (click to toggle)
pike8.0 8.0.1956-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 60,580 kB
  • sloc: ansic: 259,734; xml: 36,320; makefile: 3,748; sh: 1,713; cpp: 1,349; awk: 1,036; lisp: 655; javascript: 468; asm: 242; objc: 240; pascal: 157; sed: 34
file content (291 lines) | stat: -rw-r--r-- 7,155 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#pike __REAL_VERSION__

//! Implements an abstraction of the normal filesystem.

inherit Filesystem.Base;

// Some notes about NT code:
//   Paths are assumed to follow one of two different patterns:
//    1. /foo/bar/gazonk     indicates a path on the current drive
//    2. D:/foo/bar/gazonk   a path on the drive indicated by D.
//
//    It's possible to change the root from /foo/bar to d:/foo/bar/something,
//    but it's not possible to change from c:/foo/bar to d:/foo/bar
//
//  Generally speaing, paths will almost always follow the second
//  pattern, since getcwd() is used to get the initial value of the
//  'wd' variable unless a different path is specified.
//
protected Filesystem.Base parent; // parent filesystem

protected string root = ""; // Note: Can now include leading "/"
protected string wd;        // never trailing "/"

//! @decl void create(void|string directory, void|string root, void|int fast, void|Filesystem.Base parent)
//! Instanciate a new object representing the filesystem.
//! @param directory
//! The directory (in the real filesystem) that should become
//! the root of the filesystemobject.
//! @param root
//! Internal
//! @param fast
//! Internal
//! @param parent
//! Internal
protected void create(void|string directory,  // default: cwd
		   void|string _root,   // internal: root
		   void|int fast,       // internal: fast mode (no check)
		   void|Filesystem.Base _parent)
   				 // internal: parent filesystem
{
  if( _root )
  {
#ifdef __NT__
    _root = replace( _root, "\\", "/" );
#endif
    sscanf(reverse(_root), "%*[/]%s", root);
    root = reverse( root ); // do not remove leading '/':es.
  }

  if(!fast)
  {
    Stdio.Stat a;
    if(!directory || directory=="" || directory[0]!='/')
      directory = combine_path(getcwd(), directory||"");
#ifdef __NT__
    directory = replace( directory, "\\", "/" );
    string p;
    if( sizeof(root) )
    {
      if( sscanf( directory, "%s:/%s", p, directory ) )
      {
	if( sizeof( root ) )
	{
	  if( !has_value( root, ":/" ) )
	    root = p+":"+ (root[0] == '/' ?"":"/") + root;
	  else
	    error( "Cannot change directory to above"+root+"\n" );
	}
	else
	  root = p+":/";
      }
    }
#endif
    while( sizeof(directory) && directory[0] == '/' )
      directory = directory[1..];
    while( sizeof(directory) && directory[-1] == '/' )
      directory = directory[..<1];
#ifdef __NT__
    if( sizeof( directory ) != 2 || directory[1] != ':' )
#endif
      if(!(a = file_stat(combine_path("/",root,directory))) || !a->isdir)
	error("Not a directory\n");
  }
  while( sizeof(directory) && directory[0] == '/' )
    directory = directory[1..];
  while( sizeof(directory) && directory[-1] == '/' )
    directory = directory[..<1];
  wd = directory;
}

protected string _sprintf(int t)
{
  return t=='O' && sprintf("%O(/* root=%O, wd=%O */)", this_program, root, wd);
}

Filesystem.Base cd(string directory)
{
  Filesystem.Stat st = stat(directory);
#ifdef __NT__
  directory = replace( directory, "\\", "/" );
#endif
  if(!st) return 0;
  if(st->isdir) // stay
    return this_program(combine_path(wd, directory),
			root, 1, parent);
  return st->cd(); // try something else
}

Filesystem.Base cdup()
{
  return cd("..");
}

string cwd()
{
  return wd;
}

Filesystem.Base chroot(void|string directory)
{
  if(directory)
  {
    Filesystem.Base new = cd(directory);
    if(!new) return 0;
    return new->chroot();
  }
  return this_program("", combine_path("/",root,wd), 1, parent);
}

Filesystem.Stat stat(string file, int|void lstat)
{
   Stdio.Stat a;
#ifdef __NT__
   file = replace( file, "\\", "/" );
   while( sizeof(file) && file[-1] == '/' )
     file = file[..<1];
#endif
   string full = combine_path(wd, file);
   if ( full!="" && full[0]=='/') full=full[1..];

   if((a = file_stat(combine_path("/",root,full), lstat)))
   {
     Filesystem.Stat s = Filesystem.Stat();
     s->fullpath = sprintf("/%s", full);
     s->name = file;
     s->filesystem = this;
     s->attach_statobject(a);
     return s;
   }
   else
     return 0;
}

array(string) get_dir(void|string directory, void|string|array(string) globs)
{
#ifdef __NT__
  if(directory)
  {
    directory = replace( directory, "\\", "/" );
    while( sizeof(directory) && directory[-1] == '/' )
      directory = directory[..<1];
  }
#endif
  directory = directory ? combine_path(wd, directory) : wd;

  array(string) y = predef::get_dir(combine_path("/",root,directory));
  if(!globs)
    return y;
  else if(stringp(globs))
    return glob(globs, y);
  else
  {
    array(string) p = ({});
    foreach(globs, string g)
    {
      array(string) z;
      p += (z = glob(g, y));
      y -= z;
    }
    return p;
  }
}

array(Filesystem.Stat) get_stats(void|string directory,
				 void|string|array(string) globs)
{
  Filesystem.Base z = this;
#ifdef __NT__
  if(directory)
  {
    directory = replace( directory, "\\", "/" );
    while( sizeof(directory) && directory[-1] == '/' )
      directory = directory[..<1];
  }
#endif
  if(directory &&
     !(z = z->cd(directory)))
    return 0;

  array(string) a = z->get_dir("", globs);
  if(!a) return 0;

  return map(a, z->stat, 1)-({0});
}

Stdio.File open(string filename, string mode)
{
#ifdef __NT__
  filename = replace( filename, "\\", "/" );
#endif
  filename = combine_path(wd, filename);
  if ( filename!="" && filename[0]=='/') filename=filename[1..];
  
  Stdio.File f = Stdio.File();

  if( !f->open( combine_path("/",root,filename), mode) )
    return 0;
  return f;
}

// int access(string filename, string mode)
// {
//   return 1; // sure
// }

int rm(string filename)
{
#ifdef __NT__
  filename = replace( filename, "\\", "/" );
#endif
  filename = combine_path(wd, filename);
  return predef::rm(combine_path("/",root,filename));
}

void chmod(string filename, int|string mode)
{
#ifdef __NT__
  filename = replace( filename, "\\", "/" );
#endif
  filename = combine_path(wd, filename);
  if(stringp(mode))
  {
    Filesystem.Stat st = stat(filename); // call to self
    if(!st) return 0;
    mode = Filesystem.parse_mode(st->mode, mode);
  }
  predef::chmod(combine_path("/",root,filename), mode);
}

void chown(string filename, int|object owner, int|object group)
{
#if constant(chown)
#ifdef __NT__
  filename = replace( filename, "\\", "/" );
#endif
  if(objectp(group))
    error("user objects not supported (yet)\n");
  if(objectp(owner))
    error("user objects not supported (yet)\n");

  filename = combine_path(wd, filename);
  predef::chown(combine_path("/",root,wd), owner, group);
#else
  error("system does not have a chown"); // system does not have a chown()
#endif
}

array find(void|function(Filesystem.Stat, mixed|void...:int) mask,
	   mixed|void ... extra)
{
  array(Filesystem.Stat) res = ({});
  array(Filesystem.Stat) d = get_stats() || ({});
  array(Filesystem.Stat) r = filter(d, d->isdir);

  if(mask)
    res += filter(d-r, mask, @extra);
  else
    res += d-r;

  foreach(r, Filesystem.Stat dir)
  {
    if(!mask || mask(dir, @extra))
      res += ({ dir });

    if(dir->name=="." || dir->name=="..")
      continue;
    res += dir->cd()->find(mask, @extra);
  }

  return res;
}