00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 module mango.io.FileProxy;
00037
00038 private import mango.base.System;
00039
00040 private import mango.io.Utf8,
00041 mango.io.FilePath,
00042 mango.io.FileStyle,
00043 mango.io.Exception;
00044
00045 version (linux)
00046 private extern (C) int strlen (char *s);
00047
00048 version (Win32)
00049 private extern (C) int wcslen (wchar *s);
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 class FileProxy
00061 {
00062 private FilePath path;
00063
00064
00065
00066
00067
00068
00069
00070 this (FilePath path)
00071 {
00072 this.path = path;
00073 }
00074
00075
00076
00077
00078
00079
00080
00081 this (char[] path)
00082 {
00083 this (new FilePath (path));
00084 }
00085
00086
00087
00088
00089
00090
00091
00092 FilePath getPath ()
00093 {
00094 return path;
00095 }
00096
00097
00098
00099
00100
00101
00102
00103 char[] toString ()
00104 {
00105 return path.toString();
00106 }
00107
00108
00109
00110
00111
00112
00113
00114 bool isExisting ()
00115 {
00116 try {
00117 getSize();
00118 return true;
00119 } catch (IOException){}
00120 return false;
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 FilePath[] toList ()
00137 {
00138 bool accept (FilePath fp) {return true;}
00139
00140 return toList (&accept);
00141 }
00142
00143
00144
00145
00146
00147 version (Win32)
00148 {
00149 private import std.c.windows.windows;
00150
00151
00152
00153
00154
00155
00156
00157 private void exception ()
00158 {
00159 throw new IOException (path.toString ~ ": " ~ System.error);
00160 }
00161
00162
00163
00164
00165
00166
00167
00168 private uint getInfo (void delegate (WIN32_FIND_DATAW info) dg)
00169 {
00170 WIN32_FIND_DATAW info;
00171
00172 HANDLE h = FindFirstFileW (path.toUtf16, &info);
00173 if (h == INVALID_HANDLE_VALUE)
00174 exception ();
00175
00176 if (dg)
00177 dg (info);
00178 FindClose (h);
00179
00180 return info.dwFileAttributes;
00181 }
00182
00183
00184
00185
00186
00187
00188
00189 long getSize ()
00190 {
00191 long _size;
00192
00193 void size (WIN32_FIND_DATAW info)
00194 {
00195 _size = (cast(ulong) info.nFileSizeHigh << 32) +
00196 info.nFileSizeLow;
00197 }
00198
00199 getInfo (&size);
00200 return _size;
00201 }
00202
00203
00204
00205
00206
00207
00208
00209 bool isWritable ()
00210 {
00211 return (getInfo(null) & FILE_ATTRIBUTE_READONLY) == 0;
00212 }
00213
00214
00215
00216
00217
00218
00219
00220 bool isDirectory ()
00221 {
00222 return (getInfo(null) & FILE_ATTRIBUTE_DIRECTORY) != 0;
00223 }
00224
00225
00226
00227
00228
00229
00230
00231 long getModifiedTime ()
00232 {
00233 long _time;
00234
00235 void time (WIN32_FIND_DATAW info)
00236 {
00237 _time = (cast(ulong) info.ftLastWriteTime.dwHighDateTime << 32) +
00238 info.ftLastWriteTime.dwLowDateTime;
00239 }
00240
00241 getInfo (&time);
00242 return _time;
00243 }
00244
00245
00246
00247
00248
00249
00250
00251 void remove ()
00252 {
00253 if (isDirectory ())
00254 {
00255 if (! RemoveDirectoryW (path.toUtf16))
00256 exception();
00257 }
00258 else
00259 {
00260 if (! DeleteFileW (path.toUtf16))
00261 exception();
00262 }
00263 }
00264
00265
00266
00267
00268
00269
00270
00271 FileProxy createFile ()
00272 {
00273 HANDLE h;
00274
00275 h = CreateFileW (path.toUtf16, GENERIC_WRITE, 0, null,
00276 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, null);
00277
00278 if (h == INVALID_HANDLE_VALUE)
00279 exception();
00280
00281 if (! CloseHandle (h))
00282 exception();
00283
00284 return this;
00285 }
00286
00287
00288
00289
00290
00291
00292
00293 FileProxy createDirectory ()
00294 {
00295 if (! CreateDirectoryW (path.toUtf16, null))
00296 exception();
00297
00298 return this;
00299 }
00300
00301
00302
00303
00304
00305
00306
00307
00308 FilePath[] toList (bool delegate(FilePath fp) filter)
00309 {
00310 int i;
00311 wchar[] c;
00312 HANDLE h;
00313 FilePath[] list;
00314 WIN32_FIND_DATAW fileinfo;
00315
00316 list = new FilePath[50];
00317
00318 c = path.toUtf16 ~ cast(wchar[]) "\\*\0";
00319 h = FindFirstFileW (c, &fileinfo);
00320
00321 if (h != INVALID_HANDLE_VALUE)
00322 try {
00323 do {
00324 int len = wcslen (fileinfo.cFileName);
00325
00326
00327 FilePath fp = new FilePath (Utf8.encode(fileinfo.cFileName [0 .. len]));
00328
00329 if (i >= list.length)
00330 list.length = list.length * 2;
00331
00332 if (filter (fp))
00333 {
00334 list[i] = fp;
00335 ++i;
00336 }
00337 } while (FindNextFileW (h, &fileinfo));
00338 } finally {
00339 FindClose (h);
00340 }
00341 list.length = i;
00342 return list;
00343 }
00344 }
00345
00346
00347
00348
00349
00350 version (linux)
00351 {
00352 private import std.c.linux.linux;
00353
00354
00355
00356
00357
00358
00359
00360 private void exception ()
00361 {
00362 throw new IOException (path.toString ~ ": " ~ System.error);
00363 }
00364
00365
00366
00367
00368
00369
00370
00371 private uint getInfo (void delegate (struct_stat info) dg)
00372 {
00373 struct_stat stats;
00374
00375 if (std.c.linux.linux.stat (path.toUtf8, &stats))
00376 exception();
00377
00378 if (dg)
00379 dg (stats);
00380
00381 return stats.st_mode;
00382 }
00383
00384
00385
00386
00387
00388
00389
00390 long getSize ()
00391 {
00392 long _size;
00393
00394 void size (struct_stat info)
00395 {
00396 _size = cast(ulong) info.st_size;
00397 }
00398
00399 getInfo (&size);
00400 return _size;
00401 }
00402
00403
00404
00405
00406
00407
00408
00409 bool isWritable ()
00410 {
00411 return (getInfo(null) & O_RDONLY) == 0;
00412 }
00413
00414
00415
00416
00417
00418
00419
00420 bool isDirectory ()
00421 {
00422 return (getInfo(null) & S_IFDIR) != 0;
00423 }
00424
00425
00426
00427
00428
00429
00430
00431 long getModifiedTime ()
00432 {
00433 long _time;
00434
00435 void time (struct_stat info)
00436 {
00437 _time = cast(ulong) info.st_mtime;
00438 }
00439
00440 getInfo (&time);
00441 return _time;
00442 }
00443
00444
00445
00446
00447
00448
00449
00450 void remove ()
00451 {
00452 if (isDirectory())
00453 {
00454 if (std.c.linux.linux.rmdir (path.toUtf8))
00455 exception ();
00456 }
00457 else
00458 if (std.c.stdio.remove (path.toUtf8) == -1)
00459 exception ();
00460 }
00461
00462
00463
00464
00465
00466
00467
00468 FileProxy createFile ()
00469 {
00470 int fd;
00471
00472 fd = std.c.linux.linux.open (path.toUtf8, O_CREAT | O_WRONLY | O_TRUNC, 0660);
00473 if (fd == -1)
00474 exception();
00475
00476 if (std.c.linux.linux.close(fd) == -1)
00477 exception();
00478
00479 return this;
00480 }
00481
00482
00483
00484
00485
00486
00487
00488 FileProxy createDirectory ()
00489 {
00490 if (std.c.linux.linux.mkdir (path.toUtf8, 0777))
00491 exception();
00492
00493 return this;
00494 }
00495
00496
00497
00498
00499
00500
00501
00502
00503 FilePath[] toList (bool delegate(FilePath fp) filter)
00504 {
00505 int i;
00506 DIR* dir;
00507 dirent* entry;
00508 FilePath[] list;
00509
00510 dir = opendir (path.toUtf8);
00511 if (! dir)
00512 exception();
00513
00514 list = new FilePath [50];
00515 while ((entry = readdir(dir)) != null)
00516 {
00517 int len = strlen (entry.d_name);
00518
00519
00520 FilePath fp = new FilePath (entry.d_name[0 ..len].dup);
00521
00522 if (i >= list.length)
00523 list.length = list.length * 2;
00524
00525 if (filter (fp))
00526 {
00527 list[i] = fp;
00528 ++i;
00529 }
00530 }
00531
00532 list.length = i;
00533 closedir (dir);
00534 return list;
00535 }
00536
00537 }
00538 }