1616#define SGX_ERROR_FILE_LOWEST_ERROR_ID SGX_ERROR_FILE_BAD_STATUS
1717#define SGX_ERROR_FILE_HIGHEST_ERROR_ID SGX_ERROR_FILE_CLOSE_FAILED
1818
19+ // Internal buffer filled with zeroes and used when extending the size of
20+ // protected files.
21+ #define ZEROES_PADDING_LENGTH 32 * 1024
22+ char zeroes_padding [ZEROES_PADDING_LENGTH ] = { 0 };
23+
1924// The mapping between file descriptors and IPFS file pointers.
2025static HashMap * ipfs_file_list ;
2126
@@ -78,6 +83,27 @@ ipfs_file_destroy(void *sgx_file)
7883 sgx_fclose (sgx_file );
7984}
8085
86+ // Writes a given number of zeroes in file at the current offset.
87+ // The return value is zero if successful; otherwise non-zero.
88+ static int
89+ ipfs_write_zeroes (void * sgx_file , size_t len )
90+ {
91+ int min_count ;
92+
93+ while (len > 0 ) {
94+ min_count = len < ZEROES_PADDING_LENGTH ? len : ZEROES_PADDING_LENGTH ;
95+
96+ if (sgx_fwrite (zeroes_padding , 1 , min_count , sgx_file ) == 0 ) {
97+ errno = convert_sgx_errno (sgx_ferror (sgx_file ));
98+ return -1 ;
99+ }
100+
101+ len -= min_count ;
102+ }
103+
104+ return 0 ;
105+ }
106+
81107int
82108ipfs_init ()
83109{
@@ -104,7 +130,7 @@ ipfs_posix_fallocate(int fd, off_t offset, size_t len)
104130
105131 // The wrapper for fseek takes care of extending the file if sought beyond
106132 // the end
107- if (ipfs_lseek (fd , offset + len , SEEK_CUR ) == -1 ) {
133+ if (ipfs_lseek (fd , offset + len , SEEK_SET ) == -1 ) {
108134 return errno ;
109135 }
110136
@@ -354,7 +380,7 @@ ipfs_fflush(int fd)
354380off_t
355381ipfs_lseek (int fd , off_t offset , int nwhence )
356382{
357- off_t new_offset ;
383+ off_t cursor_current_location ;
358384 void * sgx_file = fd2file (fd );
359385 if (!sgx_file ) {
360386 errno = EBADF ;
@@ -364,20 +390,20 @@ ipfs_lseek(int fd, off_t offset, int nwhence)
364390 // Optimization: if the offset is 0 and the whence is SEEK_CUR,
365391 // this is equivalent of a call to ftell.
366392 if (offset == 0 && nwhence == SEEK_CUR ) {
367- int64_t ftell_result = (off_t )sgx_ftell (sgx_file );
393+ cursor_current_location = (off_t )sgx_ftell (sgx_file );
368394
369- if (ftell_result == -1 ) {
395+ if (cursor_current_location == -1 ) {
370396 errno = convert_sgx_errno (sgx_ferror (sgx_file ));
371397 return -1 ;
372398 }
373399
374- return ftell_result ;
400+ return cursor_current_location ;
375401 }
376402
377403 int fseek_result = sgx_fseek (sgx_file , offset , nwhence );
378404
379405 if (fseek_result == 0 ) {
380- new_offset = (__wasi_filesize_t )sgx_ftell (sgx_file );
406+ off_t new_offset = (off_t )sgx_ftell (sgx_file );
381407
382408 if (new_offset == -1 ) {
383409 errno = convert_sgx_errno (sgx_ferror (sgx_file ));
@@ -405,17 +431,39 @@ ipfs_lseek(int fd, off_t offset, int nwhence)
405431 // manually.
406432
407433 // Assume the error is raised because the cursor is moved beyond the end
408- // of the file. Try to move the cursor at the end of the file.
434+ // of the file.
435+
436+ // If the whence is the current cursor location, retrieve it
437+ if (nwhence == SEEK_CUR ) {
438+ cursor_current_location = (off_t )sgx_ftell (sgx_file );
439+ }
440+
441+ // Move the cursor at the end of the file
409442 if (sgx_fseek (sgx_file , 0 , SEEK_END ) == -1 ) {
410443 errno = convert_sgx_errno (sgx_ferror (sgx_file ));
411444 return -1 ;
412445 }
413446
447+ // Compute the number of zeroes to append.
448+ int64_t number_of_zeroes ;
449+ switch (nwhence ) {
450+ case SEEK_SET :
451+ number_of_zeroes = offset - sgx_ftell (sgx_file );
452+ break ;
453+ case SEEK_END :
454+ number_of_zeroes = offset ;
455+ break ;
456+ case SEEK_CUR :
457+ number_of_zeroes =
458+ cursor_current_location + offset - sgx_ftell (sgx_file );
459+ break ;
460+ default :
461+ errno = EINVAL ;
462+ return -1 ;
463+ }
464+
414465 // Write the missing zeroes
415- char zero = 0 ;
416- int64_t number_of_zeroes = offset - sgx_ftell (sgx_file );
417- if (sgx_fwrite (& zero , 1 , number_of_zeroes , sgx_file ) == 0 ) {
418- errno = convert_sgx_errno (sgx_ferror (sgx_file ));
466+ if (ipfs_write_zeroes (sgx_file , number_of_zeroes ) != 0 ) {
419467 return -1 ;
420468 }
421469
@@ -468,9 +516,7 @@ ipfs_ftruncate(int fd, off_t len)
468516
469517 // Increasing the size is equal to writing from the end of the file
470518 // with null bytes.
471- char null_byte = 0 ;
472- if (sgx_fwrite (& null_byte , 1 , len - file_size , sgx_file ) == 0 ) {
473- errno = convert_sgx_errno (sgx_ferror (sgx_file ));
519+ if (ipfs_write_zeroes (sgx_file , len - file_size ) != 0 ) {
474520 return -1 ;
475521 }
476522
0 commit comments