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