|
20 | 20 | #include "jsvar.h" |
21 | 21 | #include "jsparse.h" |
22 | 22 | #include "jsinteractive.h" |
| 23 | +#include "jswrap_date.h" |
23 | 24 |
|
24 | 25 | #ifndef LINUX |
25 | 26 | #include "ff.h" // filesystem stuff |
26 | 27 | #else |
27 | 28 | #include <stdio.h> |
| 29 | +#include <sys/stat.h> |
28 | 30 | #include <dirent.h> // for readdir |
29 | 31 | #endif |
30 | 32 |
|
@@ -266,7 +268,7 @@ Delete the given file |
266 | 268 |
|
267 | 269 | NOTE: Espruino does not yet support Async file IO, so this function behaves like the 'Sync' version. |
268 | 270 | */ |
269 | | - /*JSON{ |
| 271 | +/*JSON{ |
270 | 272 | "type" : "staticmethod", |
271 | 273 | "class" : "fs", |
272 | 274 | "name" : "unlinkSync", |
@@ -300,3 +302,66 @@ bool jswrap_fs_unlink(JsVar *path) { |
300 | 302 | return true; |
301 | 303 | } |
302 | 304 |
|
| 305 | +/*JSON{ |
| 306 | + "type" : "staticmethod", |
| 307 | + "class" : "fs", |
| 308 | + "name" : "statSync", |
| 309 | + "ifndef" : "SAVE_ON_FLASH", |
| 310 | + "generate" : "jswrap_fs_stat", |
| 311 | + "params" : [ |
| 312 | + ["path","JsVar","The path of the file to get information on"] |
| 313 | + ], |
| 314 | + "return" : ["JsVar","An object describing the file, or undefined on failure"] |
| 315 | +} |
| 316 | +Return information on the given file. This returns an object with the following |
| 317 | +fields: |
| 318 | +
|
| 319 | +size: size in bytes |
| 320 | +dir: a boolean specifying if the file is a directory or not |
| 321 | +mtime: A Date structure specifying the time the file was last modified |
| 322 | +*/ |
| 323 | +JsVar *jswrap_fs_stat(JsVar *path) { |
| 324 | + char pathStr[JS_DIR_BUF_SIZE] = ""; |
| 325 | + if (!jsvIsUndefined(path)) |
| 326 | + jsvGetString(path, pathStr, JS_DIR_BUF_SIZE); |
| 327 | + |
| 328 | +#ifndef LINUX |
| 329 | + FRESULT res = 0; |
| 330 | + if (jsfsInit()) { |
| 331 | + FILINFO info; |
| 332 | + res = f_stat(pathStr, &info); |
| 333 | + if (res==0 /*ok*/) { |
| 334 | + JsVar *obj = jsvNewWithFlags(JSV_OBJECT); |
| 335 | + if (!obj) return 0; |
| 336 | + jsvUnLock(jsvObjectSetChild(obj, "size", jsvNewFromInteger((JsVarInt)info.fsize))); |
| 337 | + jsvUnLock(jsvObjectSetChild(obj, "dir", jsvNewFromBool(info.fattrib & AM_DIR))); |
| 338 | + |
| 339 | + CalendarDate date; |
| 340 | + date.year = 1980+(int)((info.fdate>>9)&127); |
| 341 | + date.month = (int)((info.fdate>>5)&15); |
| 342 | + date.day = (int)((info.fdate)&31); |
| 343 | + TimeInDay td; |
| 344 | + td.daysSinceEpoch = fromCalenderDate(&date); |
| 345 | + td.hour = (int)((info.ftime>>11)&31); |
| 346 | + td.min = (int)((info.ftime>>5)&63); |
| 347 | + td.sec = (int)((info.ftime)&63); |
| 348 | + td.ms = 0; |
| 349 | + td.zone = 0; |
| 350 | + jsvUnLock(jsvObjectSetChild(obj, "mtime", jswrap_date_from_milliseconds(fromTimeInDay(&td)))); |
| 351 | + return obj; |
| 352 | + } |
| 353 | + } |
| 354 | +#else |
| 355 | + struct stat info; |
| 356 | + if (stat(pathStr, &info)==0 /*ok*/) { |
| 357 | + JsVar *obj = jsvNewWithFlags(JSV_OBJECT); |
| 358 | + if (!obj) return 0; |
| 359 | + jsvUnLock(jsvObjectSetChild(obj, "size", jsvNewFromInteger((JsVarInt)info.st_size))); |
| 360 | + jsvUnLock(jsvObjectSetChild(obj, "dir", jsvNewFromBool(S_ISDIR(info.st_mode)))); |
| 361 | + jsvUnLock(jsvObjectSetChild(obj, "mtime", jswrap_date_from_milliseconds((JsVarFloat)info.st_mtime*1000.0))); |
| 362 | + return obj; |
| 363 | + } |
| 364 | +#endif |
| 365 | + |
| 366 | + return 0; |
| 367 | +} |
0 commit comments