** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function. ** Refer to header comments at the top of this file for details. */
| 5939 | ** Refer to header comments at the top of this file for details. |
| 5940 | */ |
| 5941 | static void writefileFunc( |
| 5942 | sqlite3_context *context, |
| 5943 | int argc, |
| 5944 | sqlite3_value **argv |
| 5945 | ){ |
| 5946 | const char *zFile; |
| 5947 | mode_t mode = 0; |
| 5948 | int res; |
| 5949 | sqlite3_int64 mtime = -1; |
| 5950 | |
| 5951 | if( argc<2 || argc>4 ){ |
| 5952 | sqlite3_result_error(context, |
| 5953 | "wrong number of arguments to function writefile()", -1 |
| 5954 | ); |
| 5955 | return; |
| 5956 | } |
| 5957 | |
| 5958 | zFile = (const char*)sqlite3_value_text(argv[0]); |
| 5959 | if( zFile==0 ) return; |
| 5960 | if( argc>=3 ){ |
| 5961 | mode = (mode_t)sqlite3_value_int(argv[2]); |
| 5962 | } |
| 5963 | if( argc==4 ){ |
| 5964 | mtime = sqlite3_value_int64(argv[3]); |
| 5965 | } |
| 5966 | |
| 5967 | res = writeFile(context, zFile, argv[1], mode, mtime); |
| 5968 | if( res==1 && errno==ENOENT ){ |
| 5969 | if( makeDirectory(zFile)==SQLITE_OK ){ |
| 5970 | res = writeFile(context, zFile, argv[1], mode, mtime); |
| 5971 | } |
| 5972 | } |
| 5973 | |
| 5974 | if( argc>2 && res!=0 ){ |
| 5975 | if( S_ISLNK(mode) ){ |
| 5976 | ctxErrorMsg(context, "failed to create symlink: %s", zFile); |
| 5977 | }else if( S_ISDIR(mode) ){ |
| 5978 | ctxErrorMsg(context, "failed to create directory: %s", zFile); |
| 5979 | }else{ |
| 5980 | ctxErrorMsg(context, "failed to write file: %s", zFile); |
| 5981 | } |
| 5982 | } |
| 5983 | } |
| 5984 | |
| 5985 | /* |
| 5986 | ** SQL function: lsmode(MODE) |
nothing calls this directly
no test coverage detected