MCPcopy Create free account
hub / github.com/GaijinEntertainment/daScript / writeFile

Function writeFile

modules/dasSQLITE/sqlite/shell.c:5826–5935  ·  view source on GitHub ↗

** This function does the work for the writefile() UDF. Refer to ** header comments at the top of this file for details. */

Source from the content-addressed store, hash-verified

5824** header comments at the top of this file for details.
5825*/
5826static int writeFile(
5827 sqlite3_context *pCtx, /* Context to return bytes written in */
5828 const char *zFile, /* File to write */
5829 sqlite3_value *pData, /* Data to write */
5830 mode_t mode, /* MODE parameter passed to writefile() */
5831 sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */
5832){
5833 if( zFile==0 ) return 1;
5834#if !defined(_WIN32) && !defined(WIN32)
5835 if( S_ISLNK(mode) ){
5836 const char *zTo = (const char*)sqlite3_value_text(pData);
5837 if( zTo==0 || symlink(zTo, zFile)<0 ) return 1;
5838 }else
5839#endif
5840 {
5841 if( S_ISDIR(mode) ){
5842 if( mkdir(zFile, mode) ){
5843 /* The mkdir() call to create the directory failed. This might not
5844 ** be an error though - if there is already a directory at the same
5845 ** path and either the permissions already match or can be changed
5846 ** to do so using chmod(), it is not an error. */
5847 struct stat sStat;
5848 if( errno!=EEXIST
5849 || 0!=fileStat(zFile, &sStat)
5850 || !S_ISDIR(sStat.st_mode)
5851 || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
5852 ){
5853 return 1;
5854 }
5855 }
5856 }else{
5857 sqlite3_int64 nWrite = 0;
5858 const char *z;
5859 int rc = 0;
5860 FILE *out = fopen(zFile, "wb");
5861 if( out==0 ) return 1;
5862 z = (const char*)sqlite3_value_blob(pData);
5863 if( z ){
5864 sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
5865 nWrite = sqlite3_value_bytes(pData);
5866 if( nWrite!=n ){
5867 rc = 1;
5868 }
5869 }
5870 fclose(out);
5871 if( rc==0 && mode && chmod(zFile, mode & 0777) ){
5872 rc = 1;
5873 }
5874 if( rc ) return 2;
5875 sqlite3_result_int64(pCtx, nWrite);
5876 }
5877 }
5878
5879 if( mtime>=0 ){
5880#if defined(_WIN32)
5881#if !SQLITE_OS_WINRT
5882 /* Windows */
5883 FILETIME lastAccess;

Callers 1

writefileFuncFunction · 0.85

Calls 1

fileStatFunction · 0.85

Tested by

no test coverage detected