1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS FS utility tool
4  * FILE:            base/applications/cmdutils/hardlink.c
5  * PURPOSE:         FSutil hard links handling
6  * PROGRAMMERS:     Pierre Schweitzer <pierre@reactos.org>
7  */
8 
9 #include "fsutil.h"
10 
11 /* Add handlers here for subcommands */
12 static HandlerProc CreateMain;
13 static HandlerItem HandlersList[] =
14 {
15     /* Proc, name, help */
16     { CreateMain, _T("create"), _T("Create a new hard link") },
17 };
18 
19 static int
20 CreateMain(int argc, const TCHAR *argv[])
21 {
22     TCHAR Source[MAX_PATH], Target[MAX_PATH];
23 
24     /* We need a source and a destination */
25     if (argc < 3)
26     {
27         _ftprintf(stderr, _T("Usage: fsutil hardlink create <new> <existing>\n"));
28         _ftprintf(stderr, _T("\tFor example: fsutil hardlink create c:\\target.txt c:\\source.txt\n"));
29         return 1;
30     }
31 
32     /* Get full names */
33     if (GetFullPathName(argv[1], MAX_PATH, Target, NULL) == 0)
34     {
35         PrintErrorMessage(GetLastError());
36         return 1;
37     }
38 
39     if (GetFullPathName(argv[2], MAX_PATH, Source, NULL) == 0)
40     {
41         PrintErrorMessage(GetLastError());
42         return 1;
43     }
44 
45     /* Simply delegate to kernel32 */
46     if (!CreateHardLink(Target, Source, NULL))
47     {
48         PrintErrorMessage(GetLastError());
49         return 1;
50     }
51 
52     /* Print the status */
53     _ftprintf(stdout, _T("Hard link created for %s <=> %s\n"), Target, Source);
54 
55     return 0;
56 }
57 
58 static void
59 PrintUsage(const TCHAR * Command)
60 {
61     PrintDefaultUsage(_T(" HARDLINK "), Command, (HandlerItem *)&HandlersList,
62                       (sizeof(HandlersList) / sizeof(HandlersList[0])));
63 }
64 
65 int
66 HardLinkMain(int argc, const TCHAR *argv[])
67 {
68     return FindHandler(argc, argv, (HandlerItem *)&HandlersList,
69                        (sizeof(HandlersList) / sizeof(HandlersList[0])),
70                        PrintUsage);
71 }
72