hardlink.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with this
  4. * work for additional information regarding copyright ownership. The ASF
  5. * licenses this file to you under the Apache License, Version 2.0 (the
  6. * "License"); you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. #include "winutils.h"
  18. // List of different hardlink related command line options supported by
  19. // winutils.
  20. typedef enum HardLinkCommandOptionType
  21. {
  22. HardLinkInvalid,
  23. HardLinkCreate,
  24. HardLinkStat
  25. } HardLinkCommandOption;
  26. //----------------------------------------------------------------------------
  27. // Function: ParseCommandLine
  28. //
  29. // Description:
  30. // Parses the given command line. On success, out param 'command' contains
  31. // the user specified command.
  32. //
  33. // Returns:
  34. // TRUE: If the command line is valid
  35. // FALSE: otherwise
  36. static BOOL ParseCommandLine(__in int argc,
  37. __in wchar_t *argv[],
  38. __out HardLinkCommandOption *command)
  39. {
  40. *command = HardLinkInvalid;
  41. if (argc != 3 && argc != 4) {
  42. return FALSE;
  43. }
  44. if (argc == 3) {
  45. if (wcscmp(argv[0], L"hardlink") != 0 || wcscmp(argv[1], L"stat") != 0)
  46. {
  47. return FALSE;
  48. }
  49. *command = HardLinkStat;
  50. }
  51. if (argc == 4) {
  52. if (wcscmp(argv[0], L"hardlink") != 0 || wcscmp(argv[1], L"create") != 0)
  53. {
  54. return FALSE;
  55. }
  56. *command = HardLinkCreate;
  57. }
  58. assert(*command != HardLinkInvalid);
  59. return TRUE;
  60. }
  61. //----------------------------------------------------------------------------
  62. // Function: HardlinkStat
  63. //
  64. // Description:
  65. // Computes the number of hard links for a given file.
  66. //
  67. // Returns:
  68. // ERROR_SUCCESS: On success
  69. // error code: otherwise
  70. static DWORD HardlinkStat(__in LPCWSTR fileName, __out DWORD *puHardLinkCount)
  71. {
  72. BY_HANDLE_FILE_INFORMATION fileInformation;
  73. DWORD dwErrorCode = ERROR_SUCCESS;
  74. PWSTR longFileName = NULL;
  75. // First convert input paths to long paths
  76. //
  77. dwErrorCode = ConvertToLongPath(fileName, &longFileName);
  78. if (dwErrorCode != ERROR_SUCCESS)
  79. {
  80. goto HardlinkStatExit;
  81. }
  82. // Get file information which contains the hard link count
  83. //
  84. dwErrorCode = GetFileInformationByName(longFileName, FALSE, &fileInformation);
  85. if (dwErrorCode != ERROR_SUCCESS)
  86. {
  87. goto HardlinkStatExit;
  88. }
  89. *puHardLinkCount = fileInformation.nNumberOfLinks;
  90. HardlinkStatExit:
  91. LocalFree(longFileName);
  92. return dwErrorCode;
  93. }
  94. //----------------------------------------------------------------------------
  95. // Function: HardlinkCreate
  96. //
  97. // Description:
  98. // Creates a hard link for a given file under the given name.
  99. //
  100. // Returns:
  101. // ERROR_SUCCESS: On success
  102. // error code: otherwise
  103. static DWORD HardlinkCreate(__in LPCWSTR linkName, __in LPCWSTR fileName)
  104. {
  105. PWSTR longLinkName = NULL;
  106. PWSTR longFileName = NULL;
  107. DWORD dwErrorCode = ERROR_SUCCESS;
  108. // First convert input paths to long paths
  109. //
  110. dwErrorCode = ConvertToLongPath(linkName, &longLinkName);
  111. if (dwErrorCode != ERROR_SUCCESS)
  112. {
  113. goto HardlinkCreateExit;
  114. }
  115. dwErrorCode = ConvertToLongPath(fileName, &longFileName);
  116. if (dwErrorCode != ERROR_SUCCESS)
  117. {
  118. goto HardlinkCreateExit;
  119. }
  120. // Create the hard link
  121. //
  122. if (!CreateHardLink(longLinkName, longFileName, NULL))
  123. {
  124. dwErrorCode = GetLastError();
  125. }
  126. HardlinkCreateExit:
  127. LocalFree(longLinkName);
  128. LocalFree(longFileName);
  129. return dwErrorCode;
  130. }
  131. //----------------------------------------------------------------------------
  132. // Function: Hardlink
  133. //
  134. // Description:
  135. // Creates a hard link for a given file under the given name. Outputs the
  136. // appropriate information to stdout on success, or stderr on failure.
  137. //
  138. // Returns:
  139. // EXIT_SUCCESS: On success
  140. // EXIT_FAILURE: otherwise
  141. int Hardlink(int argc, wchar_t *argv[])
  142. {
  143. DWORD dwErrorCode = ERROR_SUCCESS;
  144. int ret = EXIT_FAILURE;
  145. HardLinkCommandOption command = HardLinkInvalid;
  146. if (!ParseCommandLine(argc, argv, &command)) {
  147. dwErrorCode = ERROR_INVALID_COMMAND_LINE;
  148. fwprintf(stderr, L"Incorrect command line arguments.\n\n");
  149. HardlinkUsage();
  150. goto HardLinkExit;
  151. }
  152. if (command == HardLinkStat)
  153. {
  154. // Compute the number of hard links
  155. //
  156. DWORD uHardLinkCount = 0;
  157. dwErrorCode = HardlinkStat(argv[2], &uHardLinkCount);
  158. if (dwErrorCode != ERROR_SUCCESS)
  159. {
  160. ReportErrorCode(L"HardlinkStat", dwErrorCode);
  161. goto HardLinkExit;
  162. }
  163. // Output the result
  164. //
  165. fwprintf(stdout, L"%d\n", uHardLinkCount);
  166. } else if (command == HardLinkCreate)
  167. {
  168. // Create the hard link
  169. //
  170. dwErrorCode = HardlinkCreate(argv[2], argv[3]);
  171. if (dwErrorCode != ERROR_SUCCESS)
  172. {
  173. ReportErrorCode(L"HardlinkCreate", dwErrorCode);
  174. goto HardLinkExit;
  175. }
  176. // Output the success message
  177. //
  178. fwprintf(stdout, L"Hardlink created for %s <<===>> %s\n", argv[2], argv[3]);
  179. } else
  180. {
  181. // Should not happen
  182. //
  183. assert(FALSE);
  184. }
  185. ret = EXIT_SUCCESS;
  186. HardLinkExit:
  187. return ret;
  188. }
  189. void HardlinkUsage()
  190. {
  191. fwprintf(stdout, L"\
  192. Usage: hardlink create [LINKNAME] [FILENAME] |\n\
  193. hardlink stat [FILENAME]\n\
  194. Creates a new hardlink on the existing file or displays the number of links\n\
  195. for the given file\n");
  196. }