Пример #1
0
/*****************************************************************************
* FUNCTION
*	mre_write_line_to_file
* DESCRIPTION
*	Writes line to file 
* PARAMETERS
*	file_name just the name as string in root directory, line as string
* RETURNS
*	MRE_FILE_NOT_WRITTEN if fail, MRE_SCILENT for success
TODO: Remove this. Already defined above.
*****************************************************************************/
VMINT mre_write_line_to_file2 (VMSTR file_name, VMSTR line)
{
	/* local variables */
	VMFILE  file_handle;
	VMCHAR  file_path[MRE_FILE_NAME_SIZE];
	VMWCHAR wfile_name[MRE_FILE_NAME_SIZE];
	VMWCHAR wline[MRE_STR_SIZE_MAX + 1];
    VMUINT written;
	VMUINT  bytes_written;
	VMINT line_bytes;
	
	//const VMINT line_size = sizeof (VMCHAR) * (vm_wstrlen (line)+1); //+2 for new line
	//VMINT line_bytes = sizeof(VMCHAR) * (vm_strlen(line) + 1 )
	//VMSTR line_new[1000];
	//sprintf(line_new,"%s\n", line);

	/* Open file */
	sprintf(file_path,"%c:\\%s",mre_get_drv(),file_name);
	vm_ascii_to_ucs2(wfile_name, MRE_STR_SIZE_MAX, file_path);

	vm_ascii_to_ucs2(wline, MRE_STR_SIZE_MAX, line);
	line_bytes = sizeof(VMWCHAR) *(vm_wstrlen (wline)+1); 

	//g_mre_textbox_text =  (sizeof (VMWCHAR) * (vm_wstrlen (wline)+1));
    //vm_wstrcpy (g_mre_textbox_text, text);

	file_handle = vm_file_open(wfile_name, MODE_APPEND, FALSE);
	if (file_handle < 0){
		file_handle = vm_file_open(wfile_name, MODE_CREATE_ALWAYS_WRITE, FALSE);
	}
	if(file_handle < 0)
	{
		vertical_scrolling_text("Write to file fail....");
	}else{
		/* write to file */
		bytes_written = vm_file_write (file_handle, wline, line_bytes, &written);
		vm_log_debug("%d bytes has been written in file", bytes_written);
		
        if (bytes_written == 0)
        {
            vm_file_close (file_handle);
            return MRE_FILE_NOT_WRITTEN;
        }

	    else 
	    {
		    vm_file_close (file_handle);
			//sprintf (show_text, "Written in file: success");
            //mre_show_text (show_text);
		    return MRE_SCILENT;
	    }
   
	}
	/* Closing file */
	vm_file_close(file_handle);
	return MRE_FILE_NOT_WRITTEN;
}
Пример #2
0
/*****************************************************************************
 * FUNCTION
 *  mre_write_file
 * DESCRIPTION
 *  This function writes on the file given 
 * PARAMETERS
 *  file name				[IN]      name of file
 * RETURNS
 *	result                  [OUT]     some code for error and success   
*****************************************************************************/
VMINT mre_write_file (VMSTR file_name)
{
	VMFILE f_write; 
	VMCHAR f_name[MRE_STR_SIZE_MAX + 1] ;
    VMWCHAR f_wname[MRE_STR_SIZE_MAX + 1];
    VMINT written_result;
    VMUINT written;
    VMCHAR show_text[MRE_STR_SIZE_MAX + 1];
    vm_log_debug ("mre_write_file function starts with file name : %s ", file_name);
    sprintf (f_name, "%c:\\%s", mre_get_drv(), file_name);
    vm_log_debug("file name : %s", f_name);

	/* string format conversion */
	vm_ascii_to_ucs2 (f_wname, MRE_STR_SIZE_MAX, f_name);

	/* file is opened here */
	f_write = vm_file_open (f_wname, MODE_WRITE, FALSE);
    vm_log_debug("file open hdl : %d", f_write);

	if (f_write < 0)
	{
        return MRE_FILE_OPEN_FAILED;
	}

        written_result = vm_file_write (f_write, 
							//vm_gb2312_string (mre_get_textbox_text ()), 
							mre_get_textbox_text (), 
								sizeof (VMWCHAR) * (vm_wstrlen (mre_get_textbox_text ())+1),  
									& written);
        vm_log_debug("file write result : %d", written_result);
        vm_free (mre_get_textbox_text ());
        if (written_result == 0)
        {
            vm_file_close (f_write);
            return MRE_FILE_NOT_WRITTEN;
        }

	    else 
	    {
		    vm_file_close (f_write);
			sprintf (show_text, "Written in file: success");
            mre_show_text (show_text);
		    return MRE_SCILENT;
	    }
   
    //vm_log_debug ("mre_write_file function exits");
    
}
Пример #3
0
/*****************************************************************************
 * FUNCTION DEFINITION
 *  mre_read_file
 * DESCRIPTION
 *  This function reads the file's contents and returns it
 * PARAMETERS
 *  file name				[IN]      name of file
 *	
 * RETURNS
 *	result                  [OUT]     some code for error and success
*****************************************************************************/ 
VMINT mre_read_file(VMSTR file_name, VMCHAR  *data){
	VMFILE  f_read; 
	VMCHAR  f_name[MRE_STR_SIZE_MAX + 1] ;
    VMWCHAR f_wname[MRE_STR_SIZE_MAX + 1];
    //VMCHAR  data[MRE_STR_SIZE_MAX + 1];
	VMCHAR  show_data[MRE_STR_SIZE_MAX + 1 + 10];
	VMWCHAR default_ucs[MRE_STR_SIZE_MAX + 1];
    VMCHAR show_text[MRE_STR_SIZE_MAX + 1];
    VMUINT nread;
    vm_log_debug ("Entering mre_read_file function ");
	sprintf (f_name, "%c:\\%s", mre_get_drv(), file_name);
    vm_log_debug("file name : %s", f_name);

	/* string format conversion */
	vm_ascii_to_ucs2 (f_wname, MRE_STR_SIZE_MAX, f_name);

    /* reading from file */
	f_read = vm_file_open (f_wname, MODE_READ, FALSE);
    vm_log_debug("file read hdl : %d", f_read);
	vm_file_read (f_read, data, MRE_STR_SIZE_MAX,  & nread); 
	data[nread] = '\0';
	vm_file_close (f_read);
    
	if (f_read < 0)
	{
		return MRE_FILE_OPEN_FAILED;
	}
    return MRE_SCILENT;
}
Пример #4
0
int fclose_mre(MZ_FILE *file_handle){
	/*
	MSC:
	int fclose(FILE *file);
	*/ 
	VMINT close_result;
	vm_file_close(*file_handle); //Doesn't return anything (void)
	close_result = 0; //Going to have to override it as always close success.
	return close_result;
}
Пример #5
0
void app_log_file(char *fmt, ...)
{
    va_list args;
    VMINT drv, hdl;
    VMUINT written;
    VMINT ret;
    vm_time_t time = {0};
    char buf[LOG_BUF_SIZE] = {0};
    VMWCHAR wpath[FILE_PATH_SIZE] = {0};
    VMCHAR path[FILE_PATH_SIZE] = {0};
  
    buf[LOG_BUF_SIZE - 2] = '\r';
    buf[LOG_BUF_SIZE - 1] = '\n';

    va_start( args, fmt );
    vm_get_time(&time);
    vm_sprintf(buf+strlen(buf), "[%02d:%02d:%02d]", time.hour, time.min, time.sec);
    vm_vsprintf(buf+strlen(buf), fmt, args);
        
    drv = vm_get_removable_driver() > 0 ? vm_get_removeable_driver() : vm_get_system_driver();
    vm_sprintf(path, "%c:\\%s", drv, BT_NAME".log");

    vm_gb2312_to_ucs2(wpath, sizeof(wpath), path); 
    hdl = vm_file_open(wpath, MODE_APPEND, 0);
    if (hdl < 0)
        hdl = vm_file_open(wpath, MODE_CREATE_ALWAYS_WRITE, 0);
    else
    {
        if (!flag_delete_log)
        {
            vm_file_close(hdl);
            vm_file_delete(wpath);
            flag_delete_log = 1;
            hdl = vm_file_open(wpath, MODE_CREATE_ALWAYS_WRITE, 0);
        }
    }
    
    vm_file_write(hdl, buf, LOG_BUF_SIZE, &written);
    vm_file_close(hdl);

    va_end( args );						
}
Пример #6
0
// Freopen method : The effect of freopen is to reassign a handle to another source. ^VS
MZ_FILE *mz_freopen_mre(const char *pPath, const char *pMode, MZ_FILE *old_file_handle)
{
	/*Do you think we should do something with the old_file_handle ? 
	Maybe close or flush etc ? ^VS */ 
	//closing the old handle
	vm_file_close(*old_file_handle);

	/* Testing : If we can open a zip file with read write when already opened, etc */

	return mz_fopen_mre(pPath, pMode);
}
Пример #7
0
/*****************************************************************************
 * FUNCTION
 *  mre_create_file_for_write
 *
 * DESCRIPTION
 *  it create file in which response data has been written 
 *
 * PARAMETERS
 *  VMSTR	filename
 *  VMINT *layer_hdl
 * RETURNS
 *  VOID
*****************************************************************************/
void mre_create_file_for_write(VMSTR filename, VMINT *layer_hdl)
{
	VMFILE  handle;
	VMWCHAR wfile_name[MRE_FILE_NAME_MAX_SIZE ];
	VMCHAR  file_name[2 * MRE_FILE_NAME_MAX_SIZE];

   	sprintf(file_name,"%c:\\%s",mre_get_drv(),filename);
	vm_ascii_to_ucs2(wfile_name, 100, file_name);
	handle = vm_file_open(wfile_name, MODE_CREATE_ALWAYS_WRITE, TRUE);
	if(handle < 0)
	{
		//mre_display_home_top_screen_ascii("File Handle Fail");
		mre_display_home_top_screen_ascii("File Handle Fail", layer_hdl);
	}
	vm_file_close(handle);
}
boolean vm_hashsum(const char* name, char* digest, vm_che_type type, VMUINT type_size) {
	VMWCHAR src[OTA_MAX_PATH_LEN];
	VMFILE handle;
	VMUINT size, read, done;
	vm_stche che_ctx;
	
	uint8_t buffer[DIGEST_SIZE_BUFFER];
	uint8_t che_hash[DIGEST_SIZE_MAX];
	boolean result = true;

	LOTAUPDATE_DEBUG("vm_md5sum - checking file %s\r\n", name);
	if (vm_ascii_to_ucs2(src, OTA_MAX_PATH_LEN*sizeof(VMWCHAR), (VMSTR) name) < 0) {
		LOTAUPDATE_DEBUG("vm_md5sum - error converting path to VMWCHAR\r\n");
		return false;
	}
	if((handle = vm_file_open(src, MODE_READ, true)) < 0) {
		LOTAUPDATE_DEBUG("vm_md5sum - error opening file\r\n");
		return false;
	}
	if(vm_file_getfilesize(handle, &size) != 0) {
		LOTAUPDATE_DEBUG("vm_md5sum - error getting file size");
		result = false;
		goto vm_md5sum_cleanup;
	}

	// initialize the che context and hash
	memset(che_hash, 0, type_size);
	vm_che_init(&che_ctx, type);
	done = 0;
	while(done < size) {
		vm_file_read(handle, buffer, DIGEST_SIZE_BUFFER, &read);
		done += read;
		vm_che_process(&che_ctx, type, VM_CHE_MODE_NULL, VM_CHE_HASH, buffer, che_hash, read, done == size);
	}

	for(int i=0; i<type_size; i++) {
		sprintf(&digest[i*2], "%02x", che_hash[i]);
	}
	digest[2*type_size] = 0;
	
	result = true;
	vm_che_deinit(&che_ctx);
	
vm_md5sum_cleanup:
	vm_file_close(handle);
	return result;
}
Пример #9
0
/*****************************************************************************
 * FUNCTION
 *  mre_create_file
 * DESCRIPTION
 *  This function creates a file with given name in a drive set earlier 
 * PARAMETERS
 *  file name				[IN]      name of file
 * RETURNS
 *	result                  [OUT]     some code for error and success   
*****************************************************************************/
VMINT mre_create_file (VMSTR file_name)
{
    VMFILE fc;
    VMCHAR f_name[MRE_STR_SIZE_MAX + 1];
    VMWCHAR f_wname[MRE_STR_SIZE_MAX + 1];
    VMCHAR show_text[MRE_STR_SIZE_MAX + 1];
    VMUINT written;
	void *test_ptr;
	int ptr_size;
    vm_log_debug("mre_create_file function starts with file name : %s ", file_name);

	//Testing only. Result: works
	/*
	ptr_size = 1024;
	test_ptr = vm_malloc(ptr_size);
	*/

	sprintf (f_name, "%c:\\%s", mre_get_drv(), file_name);
    vm_log_file("file name\n");
	vm_log_file(f_name);
	vm_log_file("\n");
	

	/* string format conversion */
	vm_ascii_to_ucs2 (f_wname, MRE_STR_SIZE_MAX, f_name);

	/* file is created here */
	fc = vm_file_open (f_wname, MODE_CREATE_ALWAYS_WRITE, FALSE);
	vm_log_debug("file open hdl : %d", fc);
    

    if (fc < 0)
	{
		vm_log_file ("mre_create_file function exits\n");
        return MRE_FILE_CREATE_FAILED;
	}
	else 
	{
		vm_file_write (fc, "", 1,  & written);
        vm_file_close (fc);
		sprintf (show_text, "file created:%s", f_name);
        mre_show_text (show_text);
        vm_log_debug (" %s mre_create_file function exits", show_text);
		return MRE_SCILENT;
	}
    
}
Пример #10
0
/*****************************************************************************
 * FUNCTION
 *  does_this_wfile_exist
 * DESCRIPTION
 *  Checks if the given filename(in VMWSTR) exists 
 * PARAMETERS
 *  VMWSTR wfilename			  [IN]      filename in VMWSTR
 * RETURNS
 *	VMBOOL TRUE if file exists or FALSE if it doesnt
*****************************************************************************/
VMBOOL does_this_wfile_exist(VMWSTR wfilename){
	VMFILE test_handle;
	
	/* Checking: Test that the file exists, we have access to it */
	test_handle = vm_file_open(wfilename, MODE_READ, FALSE);
	
	if(test_handle < 0)
	{
		// -2 is : VM_FILE_OPEN_ERROR
		printf("File doesnt exist - file handle FAIL!\n");
		return FALSE;
	}else{
		printf("File exists- file handle SUCCESS \n");
		vm_file_close(test_handle); //MUST CLOSE FILE> MUST CLOSE FILE <MUST CLOSE FILE
		return TRUE;
	}
	return FALSE;
}
Пример #11
0
/*****************************************************************************
 * FUNCTION
 *  get_file_size
 * DESCRIPTION
 *  Gets the file specified size. 
 * PARAMETERS
 *	f_name			[IN]	VMSTR		Complete file name of the file	
 * RETURNS
 *	file_size		[OUT]	VMINT		The file's size in int
*****************************************************************************/
VMINT get_file_size(VMSTR f_name){
		VMFILE  file_handle; 
		//VMCHAR  f_name[MRE_STR_SIZE_MAX + 1] ;
		VMWCHAR f_wname[MRE_STR_SIZE_MAX + 1];
		VMCHAR  show_data[MRE_STR_SIZE_MAX + 1 + 10];
		VMWCHAR default_ucs[MRE_STR_SIZE_MAX + 1];
		VMCHAR show_text[MRE_STR_SIZE_MAX + 1];
		VMUINT nread;
		VMINT file_size;
		vm_log_debug ("Entering get_file_size function ");
		//sprintf (f_name, "%c:\\%s", mre_get_drv(), file_name);
		vm_log_debug("file name : %s", f_name);

		/* string format conversion */
		vm_ascii_to_ucs2 (f_wname, MRE_STR_SIZE_MAX, f_name);

		file_handle = vm_file_open(f_wname, MODE_WRITE, TRUE); //Mode as Binary cuz image, right?
		vm_file_getfilesize(file_handle, &file_size);
		vm_file_close(file_handle); //Don't need the file no more. Closing it.
		return file_size;
}
Пример #12
0
/*****************************************************************************
 * FUNCTION
 *  mre_dump_to_file
 *
 * DESCRIPTION
 *  Write response data to specified file. Show alert if failed.
 *
 * PARAMETERS
 *  tcp_buffer              [IN]   buffer pointer containing address of received buffer
 *  size                    [IN]   size of received data
 *  VMINT *layer_hdl		[IN]
 *
 * RETURNS
 *  VOID
*****************************************************************************/
void mre_dump_to_file(VMCHAR *tcp_buffer, VMINT size, VMINT *layer_hdl)
{
    VMFILE  handle;
	VMWCHAR wfile_name[MRE_FILE_NAME_MAX_SIZE ];
	VMCHAR  file_name[2 * MRE_FILE_NAME_MAX_SIZE];
	VMUINT length;
	VMINT write_status;

	sprintf(file_name,"%c:\\%s",mre_get_drv(),"TCP.txt");
	vm_ascii_to_ucs2(wfile_name, 100, file_name);
	handle = vm_file_open(wfile_name, MODE_APPEND, TRUE);
	if(handle < 0)
	{
		//mre_display_home_top_screen_ascii("File Handle Fail");
		mre_display_home_top_screen_ascii("File Handle Fail", layer_hdl);
		//vertical_scrolling_text("File Handle Fail....");
	}

	if (handle)
    {
   
	/* log information */
	vm_log_debug("File handle is %d.", handle);
	/* write response to tcp_buffer.txt file */
	write_status =  vm_file_write(handle, tcp_buffer, size, &length);

	if (write_status < 0)
	{
		 vm_log_debug("unable to write");
	}
	/* log information */
    vm_log_debug("%d bytes has been written to file.", length);
    }

	vm_file_close(handle);
}
Пример #13
0
extern int _close( int file )
{
    vm_file_close(file);
    return 0;
}
Пример #14
0
void close_mre(MZ_FILE *file_handle){ //Maybe you have to remove the pointer
	vm_file_close(*file_handle);
}
Пример #15
0
/*
 * Load all data from a file into a given buffer.
 *
 * The file is expected to contain either PEM or DER encoded data.
 * A terminating null byte is always appended. It is included in the announced
 * length only if the data looks like it is PEM encoded.
 */
int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
{
    
    int ret = 0;
    long size;

    VM_FS_HANDLE file_handle;
    VMWCHAR file_name[100 + 1];
    VMSTR cert_paths[2];
    
    VMUINT8* cert_buffer;
    VMUINT read_size;
    VMINT drv;
    VMWCHAR wpath[FILE_PATH_SIZE] = {0};
    VMCHAR path_cert[FILE_PATH_SIZE] = {0};

    cert_paths[0] = (VMSTR)path;

    memset(file_name, 0, sizeof(file_name));

    vm_ascii_to_ucs2(file_name, SSL_CERT_PATH_MAX_LENGTH, cert_paths[0]);

    file_handle = vm_file_open(file_name, MODE_READ, VM_TRUE);

    if(!(VM_IS_SUCCEEDED(file_handle)))
    {
        return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
    }

    ret = vm_file_seek(file_handle, 0, BASE_END);
    if (ret < 0){
        return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
    }
    size = vm_file_tell(file_handle);
    if (size < 0){
        return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
    }
    ret = vm_file_seek(file_handle, 0, BASE_BEGIN);
    if (ret < 0){
        return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
    }

    *n = (size_t) size;
    
    if( *n + 1 == 0 ||
        ( *buf = mbedtls_calloc( *n + 1 ) ) == NULL )
    {
        vm_file_close( file_handle );
        return( MBEDTLS_ERR_PK_ALLOC_FAILED );
    }

    vm_file_read(file_handle, *buf, *n, &read_size);
    if(read_size != *n)
    {
        vm_file_close( file_handle );
        vm_free( *buf );
        return( MBEDTLS_ERR_X509_FILE_IO_ERROR ); 
    }

    vm_file_close( file_handle );

    (*buf)[*n] = '\0';

    if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
        ++*n;

    return( 0 );

    // FILE *f;
    // long size;

    // if( ( f = fopen( path, "rb" ) ) == NULL )
    //     return( MBEDTLS_ERR_PK_FILE_IO_ERROR );

    // fseek( f, 0, SEEK_END );
    // if( ( size = ftell( f ) ) == -1 )
    // {
    //     fclose( f );
    //     return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
    // }
    // fseek( f, 0, SEEK_SET );

    // *n = (size_t) size;

    // if( *n + 1 == 0 ||
    //     ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
    // {
    //     fclose( f );
    //     return( MBEDTLS_ERR_PK_ALLOC_FAILED );
    // }

    // if( fread( *buf, 1, *n, f ) != *n )
    // {
    //     fclose( f );
    //     mbedtls_free( *buf );
    //     return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
    // }

    // fclose( f );

    // (*buf)[*n] = '\0';

    // if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
    //     ++*n;

    // return( 0 );
}
Пример #16
0
static void _vm_preload_read_data(vm_preload_ctx_t * ctx_p)
{
    VMCHAR *body = (VMCHAR *)_vm_kernel_calloc(BUFFER_LEN);
    //VMCHAR http_buffer[BUFFER_LEN] = {0};
    VMINT len = 0, i = 0;
    VMINT http_header_received = 0;
    VMINT first_downloaded = 0;
    VMINT http_header_len = 0;
    VMINT file_handle = -1;
    VMINT ret = 0;
    VMBYTE * buf = NULL;
    VMFILE f_handle = -1;
    VMUINT written = 0;
    vm_preload_recv_data_t data = {{E_PRELOAD_QUERYING, NULL}, 0, 0}; 

    
    data.head.user_data = ctx_p->user_data;

    //memset(buf, 0, sizeof(2*BUFFER_LEN));

    MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3358 , ctx_p->g_http_content_length);       

    // read the header
    if (0 == ctx_p->g_http_content_length)
    {
        
        buf = (VMBYTE *)_vm_kernel_malloc(BUFFER_LEN);
        if (NULL == buf)
        {
            MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_PRELOAD_E1, 7, __LINE__);
            return;
        }
        memset(buf, 0, sizeof(BUFFER_LEN));
        while ((ret = vm_tcp_read (ctx_p->soc_id, buf + len, BUFFER_LEN - len)) > 0)
        {
            len += ret;
            MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3369 , ret, len);         
        }
        
        for(i = 1; i < len; i++)
        {
            if (buf[i] == LF && buf[i - 1] == CR)
            {
                if (app_strnicmp((kal_char *)&buf[http_header_len], (kal_char *)CONTENT_LENGTH, strlen(CONTENT_LENGTH)) == 0)
                {
                    buf[i - 1] = '\0';
                    first_downloaded = 0;
                    ctx_p->g_http_content_length = atoi((const char *)&buf[http_header_len] + strlen(CONTENT_LENGTH) + 1);
                    ctx_p->need_recv_len = ctx_p->g_http_content_length;
                    //data.total = http_content_length;I
                    
                    ctx_p->update = 1;
                }

                http_header_len = i + 1;

                if ((i + 2) < len && 
                (buf[i + 2] == LF && buf[i + 1] == CR))
                {
					http_header_len += 2;
                    first_downloaded = len - http_header_len;					
					break;
                }
            }
        }
        
        //strncpy(body, buf + 2, first_downloaded);
        memcpy(body, &buf[http_header_len], first_downloaded);
        _vm_kernel_free(buf);
        if (!vm_preload_is_url_valid(body, ctx_p, 0))
        {
            _vm_preload_try_download(ctx_p);
            ctx_p->g_http_content_length = ctx_p->need_recv_len = 0;
            //ctx_p->update = 0;
            _vm_kernel_free(body);
            return;
        }
        
        // preparing file
        f_handle = vm_file_open(ctx_p->path, MODE_READ, 1);
        if (0 <= f_handle)
        {
            vm_file_close(f_handle);
            MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3411 );
            vm_file_delete(ctx_p->path);
        }
        f_handle = vm_file_open(ctx_p->path, MODE_CREATE_ALWAYS_WRITE, 1);
    }

    // preparing file
    if (0 > f_handle)
    {
        f_handle = vm_file_open(ctx_p->path, MODE_APPEND, 1);
    }

    if (0 > f_handle)
    {
        VMCHAR buf[260];
        vm_ucs2_to_ascii(buf, 260, ctx_p->path);
        MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3429 , buf);
        data.head.state = E_PRELOAD_ERR_PATH;
        ctx_p->status = E_PRELOAD_ERR_PATH;
        _vm_preload_clean_and_free_ctx(ctx_p, &data);
        _vm_kernel_free(body);
        return;
    }

    // no disk space
    {
        VMWCHAR drv_wname[4] = {0};  
        VMUINT size = 0;
        vm_wstrncpy(drv_wname, ctx_p->path, 1);
        size = vm_get_disk_free_space(drv_wname);

        if (size < (VMUINT)ctx_p->g_http_content_length)
        {
            MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3429 , (VMCHAR*)drv_wname);
            data.head.state = E_PRELOAD_FAILURE;
            ctx_p->status = E_PRELOAD_FAILURE;
            if (0 <= f_handle)
            {
                vm_file_close(f_handle);
				vm_file_delete(ctx_p->path);
            }               
            _vm_preload_clean_and_free_ctx(ctx_p, &data);
            _vm_kernel_free(body);
            return;
        }
     
    }


    do
    {
        len = first_downloaded;

        while ((ret = vm_tcp_read (ctx_p->soc_id, body + len, 
            (ctx_p->g_http_content_length > BUFFER_LEN ? BUFFER_LEN : ctx_p->g_http_content_length) - len)) > 0)
        {
            // would block
            if (0 == ret)
            {
                MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3448 );
                break;
            }
            len += ret;
            if (BUFFER_LEN == len)
            {
                MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3454 );
                break;
            }
        }
        
        ctx_p->g_http_content_length -= len;
        first_downloaded = 0;
    
        MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3462 , len);            
        data.head.state = E_PRELOAD_DOWNLOADING;
        ctx_p->status = (VMINT)E_PRELOAD_DOWNLOADING;
        data.total = ctx_p->need_recv_len;
        data.received = ctx_p->need_recv_len - ctx_p->g_http_content_length;
        //data.buf = body;
        //data.size = len;
        
        MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3469 , data.received, data.total);
        MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3470 );

        vm_file_write(f_handle, body, len, &written);
    
        PRELOAD_PMNG_WRAP_CALLBACK(ctx_p->p_hdl, 
            ctx_p->cb, ctx_p->res_id, (void*)&data);
        
    } while (BUFFER_LEN == len && 0 != ret); // would block break

    vm_file_close(f_handle);

    if (0 == ctx_p->g_http_content_length)
    {
        data.head.state = E_PRELOAD_DOWNLOADED;
        ctx_p->status = (VMINT)E_PRELOAD_DOWNLOADED;
        data.total = ctx_p->g_http_content_length;
        MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3485 , data.total);
        MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3486 );
        _vm_preload_clean_and_free_ctx(ctx_p, &data);
    }
    #if 0
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
    #endif
    
    _vm_kernel_free(body);
    MMI_TRACE(MMI_MRE_TRC_MOD_VMSOCK, TRC_MRE_VMSOCK_3582 );            
}