** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function. ** Refer to header comments at the top of this file for details. */
| 2574 | ** Refer to header comments at the top of this file for details. |
| 2575 | */ |
| 2576 | static void writefileFunc( |
| 2577 | sqlite3_context *context, |
| 2578 | int argc, |
| 2579 | sqlite3_value **argv |
| 2580 | ){ |
| 2581 | const char *zFile; |
| 2582 | mode_t mode = 0; |
| 2583 | int res; |
| 2584 | sqlite3_int64 mtime = -1; |
| 2585 | |
| 2586 | if( argc<2 || argc>4 ){ |
| 2587 | sqlite3_result_error(context, |
| 2588 | "wrong number of arguments to function writefile()", -1 |
| 2589 | ); |
| 2590 | return; |
| 2591 | } |
| 2592 | |
| 2593 | zFile = (const char*)sqlite3_value_text(argv[0]); |
| 2594 | if( zFile==0 ) return; |
| 2595 | if( argc>=3 ){ |
| 2596 | mode = (mode_t)sqlite3_value_int(argv[2]); |
| 2597 | } |
| 2598 | if( argc==4 ){ |
| 2599 | mtime = sqlite3_value_int64(argv[3]); |
| 2600 | } |
| 2601 | |
| 2602 | res = writeFile(context, zFile, argv[1], mode, mtime); |
| 2603 | if( res==1 && errno==ENOENT ){ |
| 2604 | if( makeDirectory(zFile)==SQLITE_OK ){ |
| 2605 | res = writeFile(context, zFile, argv[1], mode, mtime); |
| 2606 | } |
| 2607 | } |
| 2608 | |
| 2609 | if( argc>2 && res!=0 ){ |
| 2610 | if( S_ISLNK(mode) ){ |
| 2611 | ctxErrorMsg(context, "failed to create symlink: %s", zFile); |
| 2612 | }else if( S_ISDIR(mode) ){ |
| 2613 | ctxErrorMsg(context, "failed to create directory: %s", zFile); |
| 2614 | }else{ |
| 2615 | ctxErrorMsg(context, "failed to write file: %s", zFile); |
| 2616 | } |
| 2617 | } |
| 2618 | } |
| 2619 | |
| 2620 | /* |
| 2621 | ** SQL function: lsmode(MODE) |
nothing calls this directly
no test coverage detected