如何更有效地复制简历中的文件?

时间:2018-06-11 11:27:00

标签: c linux file embedded-linux

我在500毫秒的间隔后连续调用下面的函数。

基本上,当将额外的字节添加到源文件时,此函数会将源文件中的字节复制到目标文件。

我想让我的嵌入式平台更快。

int CopyResume(CHAR8 *src_file, CHAR8 *dst_file)
{
   int    status               = SUCCESS;
   FILE                    *fp_read             = NULL;
   FILE                    *fp_write            = NULL;
   long                    size_src             = 0L;
   long                    size_dst             = 0L;
   long                    size                 = 0L;
   char                    *buffer              = NULL;

   do
   {
      fp_read = fopen(src_file,"r");
      if(NULL == fp_read)
      {
         status = FAIL;
         break;
      }
      fp_write = fopen(dst_file,"a+");
      if(NULL == fp_write)
      {
         status = FAIL;
         break;
      }

      fseek(fp_write, 0L , SEEK_END);
      size_dst = ftell(fp_write);
      fseek(fp_read, 0L, SEEK_END);
      size_src = ftell(fp_read);
      fseek(fp_read, size_dst, SEEK_SET);

      size = (size_src - size_dst);
      if(size <= 0)
      {
         break;
      }

      buffer = (char*) malloc (sizeof(char) * size + 1);
      if (buffer == NULL)
      {
         status = FAIL;
         break;
      }

      if(fread (buffer,1,size, fp_read) != size)
      {
         status = FAIL;
         break;
      }
      // Write Buffer
      if(fwrite(buffer, 1, size, fp_write) != size)
      {
         status = FAIL;
         break;
      }
   }while(0);

   if(NULL != fp_write)
   {
      fflush(fp_write);
      fsync(fileno(fp_write));
      usleep(1000);
      fclose(fp_write);
      fp_write = NULL;
   }
   if(NULL != fp_read)
   {
      fflush(fp_read);
      fsync(fileno(fp_read));
      usleep(1000);
      fclose(fp_read);
      fp_read = NULL;
   }
   if(NULL != buffer)
   {
      free (buffer);
   }
   return status;
}

我的功能需要大约100毫秒才能将115kb的数据从SRC复制到DST。

如何优化此功能以使其更快?

0 个答案:

没有答案