Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.9 KiB

  1. /* fmove.c - fast copy between two file specs
  2. *
  3. * 5/10/86 daniel lipkie Added frenameNO. fmove uses frenameNO
  4. * 17-Oct-90 w-barry Switched 'C'-runtime function 'rename' for
  5. * private version 'rename' until DosMove
  6. * is completely implemented.
  7. *
  8. */
  9. #include <fcntl.h>
  10. #include <sys\types.h>
  11. #include <sys\stat.h>
  12. #include <malloc.h>
  13. #include <dos.h>
  14. #include <io.h>
  15. #include <stdio.h>
  16. #include <windows.h>
  17. #include <tools.h>
  18. #include <errno.h>
  19. /* extern int errno; */
  20. #define IBUF 10240
  21. /* frenameNO (newname, oldname) renames a file from the oldname to the
  22. * newname. This interface parallels the C rename function in the
  23. * pre version 4.0 of C. The rename function changed the order of the
  24. * params with version 4.0. This interface isolates the change.
  25. * pre-4.0: rename (newname, oldname)
  26. * 4.0: rename (oldname, newname);
  27. */
  28. int frenameNO(strNew, strOld)
  29. char *strNew, *strOld;
  30. {
  31. return( rename(strOld, strNew) ); /* assumes we are compiling with 4.0 lib */
  32. }
  33. /* fmove (source file, destination file) copies the source to the destination
  34. * preserving attributes and filetimes. Returns NULL if OK or a char pointer
  35. * to the corresponding text of the error
  36. */
  37. char *fmove (src,dst)
  38. char *src, *dst;
  39. {
  40. char *result;
  41. HANDLE hSrc;
  42. int Res;
  43. /* Try a simple rename first
  44. */
  45. if ( !rename(src, dst) )
  46. return NULL;
  47. if ( GetFileAttributes(src) == 0xFFFFFFFF ) {
  48. return "Source file does not exist";
  49. }
  50. /* Try to fdelete the destination
  51. */
  52. /* We used to fdelete(dst) unconditionally here.
  53. In case src and dst are the same file, but different filenames (e.g. UNC name vs. local name),
  54. fdelete(dst) will delete src.
  55. To fix this, we will lock the src before deleting dst.
  56. */
  57. hSrc = CreateFile(
  58. src,
  59. GENERIC_READ,
  60. 0, /* don't share src */
  61. NULL,
  62. OPEN_EXISTING,
  63. 0,
  64. NULL
  65. );
  66. Res = fdelete (dst);
  67. if (hSrc != INVALID_HANDLE_VALUE) {
  68. CloseHandle(hSrc);
  69. }
  70. if (Res > 2) {
  71. return "Unable to delete destination";
  72. }
  73. /* Destination is gone. See if we can simply rename again
  74. */
  75. if (rename(src, dst) == -1) {
  76. /* If the error isn't different device then just return
  77. * the error
  78. */
  79. if (errno != EXDEV) {
  80. return error ();
  81. } else
  82. /* Try a copy across devices
  83. */
  84. if ((result = fcopy (src, dst)) != NULL)
  85. return result;
  86. /* Cross-device copy worked. Must delete source
  87. */
  88. fdelete (src);
  89. }
  90. return NULL;
  91. }