my_copy.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:3k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  2.    
  3.    This library is free software; you can redistribute it and/or
  4.    modify it under the terms of the GNU Library General Public
  5.    License as published by the Free Software Foundation; either
  6.    version 2 of the License, or (at your option) any later version.
  7.    
  8.    This library is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11.    Library General Public License for more details.
  12.    
  13.    You should have received a copy of the GNU Library General Public
  14.    License along with this library; if not, write to the Free
  15.    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  16.    MA 02111-1307, USA */
  17. #define USES_TYPES /* sys/types is included */
  18. #include "mysys_priv.h"
  19. #include <sys/stat.h>
  20. #include <m_string.h>
  21. #if defined(HAVE_SYS_UTIME_H)
  22. #include <sys/utime.h>
  23. #elif defined(HAVE_UTIME_H)
  24. #include <utime.h>
  25. #elif !defined(HPUX)
  26. #include <time.h>
  27. struct utimbuf {
  28.   time_t actime;
  29.   time_t modtime;
  30. };
  31. #endif
  32. /*
  33.   Ordinary ownership and accesstimes are copied from 'from-file'
  34.   if MyFlags & MY_HOLD_ORIGINAL_MODES is set and to-file exists then
  35.   the modes of to-file isn't changed
  36.   Dont set MY_FNABP or MY_NABP bits on when calling this function !
  37.   */
  38. int my_copy(const char *from, const char *to, myf MyFlags)
  39. {
  40.   uint Count;
  41.   int new_file_stat;
  42.   File from_file,to_file;
  43.   char buff[IO_SIZE];
  44.   struct stat stat_buff,new_stat_buff;
  45.   DBUG_ENTER("my_copy");
  46.   DBUG_PRINT("my",("from %s to %s MyFlags %d", from, to, MyFlags));
  47.   from_file=to_file= -1;
  48.   new_file_stat=0;
  49.   if (MyFlags & MY_HOLD_ORIGINAL_MODES) /* Copy stat if possible */
  50.     new_file_stat=stat((char*) to, &new_stat_buff);
  51.   if ((from_file=my_open(from,O_RDONLY,MyFlags)) >= 0)
  52.   {
  53.     if (stat(from,&stat_buff))
  54.     {
  55.       my_errno=errno;
  56.       goto err;
  57.     }
  58.     if (MyFlags & MY_HOLD_ORIGINAL_MODES && !new_file_stat)
  59.       stat_buff=new_stat_buff;
  60.     if ((to_file=  my_create(to,(int) stat_buff.st_mode,
  61.      O_WRONLY | O_TRUNC | O_BINARY,
  62.      MyFlags)) < 0)
  63.       goto err;
  64.     while ((Count=my_read(from_file,buff,IO_SIZE,MyFlags)) != 0)
  65. if (Count == (uint) -1 ||
  66.     my_write(to_file,buff,Count,MYF(MyFlags | MY_NABP)))
  67. goto err;
  68.     if (my_close(from_file,MyFlags) | my_close(to_file,MyFlags))
  69.       DBUG_RETURN(-1); /* Error on close */
  70.     /* Copy modes if possible */
  71.     if (MyFlags & MY_HOLD_ORIGINAL_MODES && new_file_stat)
  72. DBUG_RETURN(0); /* File copyed but not stat */
  73.     VOID(chmod(to, stat_buff.st_mode & 07777)); /* Copy modes */
  74. #if !defined(MSDOS) && !defined(__WIN__) && !defined(__EMX__)
  75.     VOID(chown(to, stat_buff.st_uid,stat_buff.st_gid)); /* Copy ownership */
  76. #endif
  77. #if !defined(VMS) && !defined(__ZTC__)
  78.     if (MyFlags & MY_COPYTIME)
  79.     {
  80.       struct utimbuf timep;
  81.       timep.actime  = stat_buff.st_atime;
  82.       timep.modtime = stat_buff.st_mtime;
  83.       VOID(utime((char*) to, &timep)); /* last accessed and modified times */
  84.     }
  85. #endif
  86.     DBUG_RETURN(0);
  87.   }
  88. err:
  89.   if (from_file >= 0) VOID(my_close(from_file,MyFlags));
  90.   if (to_file >= 0)   VOID(my_close(to_file,MyFlags));
  91.   DBUG_RETURN(-1);
  92. } /* my_copy */