Repository navigation
Expand file tree
/
Copy pathread_link.cpp
More file actions
399 lines (363 loc) · 15 KB
/
Copy pathread_link.cpp
File metadata and controls
399 lines (363 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// https://stackoverflow.com/questions/45140281/display-real-path-of-lnk-shortcut-file-using-c
// build: g++ -Wall read_link.cpp -o read_link.exe -lole32 -luuid
// uuid.lib reqd for IID_IPersistFile
//*********************************************************************************************
// Claude AI:
// That clarifies it. Here's the Windows-correct terminology for those two:
//
// .lnk files (like showin.lnk) — these are shortcuts. Not a filesystem link at all — a
// shortcut is just an ordinary file (a COM structured-storage/binary blob) that Explorer
// knows how to interpret, containing a stored path, working directory, icon, etc. Resolving
// one means parsing that file's contents via IShellLink, which is exactly what your
// ResolveShortcut() in read_link.cpp does.
// Links created via mklink — these are symbolic links (or junctions, if created with mklink
// /J, which is directory-only). Both are true filesystem-level constructs implemented as
// reparse points — the OS itself resolves them transparently at the
// CreateFile()/FindFirstFile() level, no parsing required on your part.
//
// So the real distinction isn't "soft vs. hard" — it's shortcut (an ordinary file
// Explorer/your code interprets) vs. symbolic link/junction (an OS-level reparse point the
// filesystem resolves). That maps cleanly onto the two code paths already in your ndir.md
// notes: ResolveShortcut() for .lnk files, and the GetLinkTarget()/reparse-tag logic for
// mklink-created links.
//*********************************************************************************************
// #define STAND_ALONE 1
#include <windows.h>
#include <stdio.h>
#include <shlobj.h> /* For IShellLink */
#include <string>
#include <tchar.h>
//lint -e10 Expecting '}'
//lint -esym(534, lstrcpynA)
#ifndef STAND_ALONE
#include "common.h"
#include "ndir32.h"
#endif
#ifdef STAND_ALONE
typedef unsigned int uint ;
//**********************************************************************
//lint -esym(714, strip_newlines)
//lint -esym(759, strip_newlines)
//lint -esym(765, strip_newlines)
void strip_newlines(char *rstr)
{
int slen = (int) _tcslen(rstr) ;
while (1) {
if (slen == 0)
break;
if (*(rstr+slen-1) == '\n' || *(rstr+slen-1) == '\r') {
slen-- ;
*(rstr+slen) = 0 ;
} else {
break;
}
}
}
//*************************************************************
//lint -esym(714, get_system_message)
//lint -esym(759, get_system_message)
//lint -esym(765, get_system_message)
char *get_system_message(DWORD errcode)
{
// static char msg[261] ;
static char msg[1024] ;
// int slen ;
int result = (int) errcode ;
if (result < 0) {
result = -result ;
errcode = (DWORD) result ;
// wsprintfA(msg, "Win32: unknown error code %d", result) ;
// return msg;
}
// if (errcode >= IP_STATUS_BASE && errcode <= MAX_ICMP_ERR_STRING) {
// return aszSendEchoErr[errcode - IP_STATUS_BASE];
// }
// if (errcode >= SNMP_ERROR_FIRST && errcode <= SNMP_ERROR_LAST) {
// return snmp_error_msgs[errcode - SNMP_ERROR_FIRST];
// }
LPVOID lpMsgBuf;
DWORD dresult = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
errcode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPSTR) &lpMsgBuf,
0, 0);
// Process any inserts in lpMsgBuf.
// ...
// Display the string.
if (dresult == 0) {
DWORD glError = GetLastError() ;
if (glError == 317) { // see comment at start of function
sprintf(msg, "FormatMessage(): no message for error code %d", result) ;
} else {
sprintf(msg, "FormatMessage() failed: [%u], errcode %d", (uint) GetLastError(), result) ;
}
} else
if (lpMsgBuf == NULL) {
sprintf(msg, "NULL buffer in response from FormatMessage() [%u]", (uint) GetLastError()) ;
} else
{
_tcsncpy(msg, (char *) lpMsgBuf, sizeof(msg)) ;
// Free the buffer.
LocalFree( lpMsgBuf ); //lint !e534
}
// trim the newline off the message before copying it...
strip_newlines(msg) ;
return msg;
}
//*************************************************************
// each subsequent call to this function overwrites
// the previous report.
//*************************************************************
char *get_system_message(void)
{
return get_system_message(GetLastError());
}
#endif
/*********************************************************************
* Function......: ResolveShortcut
* Parameters....: lpszShortcutPath - string holding path and file name of a shortcut
* lpszFilePath - string that will hold a file name
* lpszArgsPath - string that will hold a arguments string
* Returns.......: S_OK on success, error code on failure
* Description...: Resolves a Shell link object (shortcut)
*********************************************************************/
HRESULT ResolveShortcut(/*in*/ LPCTSTR lpszShortcutPath,
/*out*/ LPTSTR lpszFilePath,
/*out*/ LPTSTR lpszArgsPath
)
{
IShellLink* psl = NULL;
// buffer that receives the null-terminated string
// for the drive and path
TCHAR szPath[MAX_PATH];
// buffer that receives the null-terminated
// string for the description
// TCHAR szDesc[MAX_PATH];
TCHAR szArgs[MAX_PATH];
// structure that receives the information about the shortcut
WIN32_FIND_DATA wfd;
WCHAR wszTemp[MAX_PATH];
lpszFilePath[0] = '\0';
lpszArgsPath[0] = '\0';
// Get a pointer to the IShellLink interface
HRESULT hRes = CoCreateInstance(CLSID_ShellLink,
NULL,
CLSCTX_INPROC_SERVER, //lint !e641
IID_IShellLink,
(void**)&psl); //lint !e838
if (SUCCEEDED(hRes)) {
// Get a pointer to the IPersistFile interface
IPersistFile* ppf = NULL;
psl->QueryInterface(IID_IPersistFile, (void **) &ppf); //lint !e534
// IPersistFile is using LPCOLESTR,
// so make sure that the string is Unicode
#ifndef _UNICODE
MultiByteToWideChar(CP_ACP, 0, lpszShortcutPath, -1, wszTemp, MAX_PATH); //lint !e534
#else
wcsncpy(wszTemp, lpszShortcutPath, MAX_PATH);
#endif
// Open the shortcut file and initialize it from its contents
hRes = ppf->Load(wszTemp, STGM_READ);
if (SUCCEEDED(hRes)) {
// Try to find the target of a shortcut,
// even if it has been moved or renamed
// hRes = psl->Resolve(NULL, SLR_UPDATE); //lint !e641
// even if it has been moved or renamed.
// NOTE: SLR_UPDATE alone can make Resolve() pop up a
// "browse for target" dialog when the target is missing,
// and since this is a console app with no message pump,
// that dialog silently blocks forever. SLR_NO_UI suppresses
// it; the timeout in the high word of fFlags (here 500ms)
// bounds how long Resolve() will silently search before
// giving up and returning a failure code instead of hanging.
hRes = psl->Resolve(NULL, SLR_UPDATE | SLR_NO_UI | (500 << 16));
if (SUCCEEDED(hRes)) {
// Get the path to the shortcut target.
// NOTE: deliberately NOT passing SLGP_RAWPATH here.
// Per docs, SLGP_RAWPATH returns "the raw path name...
// something that might not exist" - i.e. it just echoes
// back whatever path is stored in the .lnk's binary data,
// with no filesystem verification at all. Combined with
// Resolve() above being able to return S_OK even when it
// could not actually verify/relocate the target (it just
// gives up cleanly under SLR_NO_UI), that raw-echo path
// is not trustworthy proof the target exists.
hRes = psl->GetPath(szPath, MAX_PATH, &wfd, 0); //lint !e641
if (FAILED(hRes)) {
// printf("GetPath error\n");
goto error_exit;
// return hRes;
}
// Copy the path out to the caller BEFORE the existence
// check below. Even if the target turns out to be missing,
// the caller still gets the resolved path so it can be
// reported (e.g. tagged as a dead/"LOST" link) rather than
// discarded just because hRes ends up indicating failure.
lstrcpyn(lpszFilePath, szPath, MAX_PATH);
// Belt-and-suspenders: explicitly confirm the resolved
// path exists on disk rather than trusting the HRESULT
// chain above. GetFileAttributesW is the standard WinAPI
// call for this.
if (GetFileAttributes(szPath) == INVALID_FILE_ATTRIBUTES) {
hRes = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
goto error_exit;
}
// printf("path: %s\n", szPath);
// Get the description of the target
// hRes = psl->GetDescription(szDesc, MAX_PATH);
// if (FAILED(hRes))
// return hRes;
// printf("desc: %s\n", szDesc);
// Get the arguments of the target
hRes = psl->GetArguments(szArgs, MAX_PATH);
if (FAILED(hRes)) {
// printf("GetArguments error\n");
goto error_exit;
}
// printf("args: %s\n", szArgs);
lstrcpyn(lpszArgsPath, szArgs, MAX_PATH);
}
// else {
// printf("Resolve error\n");
// }
}
// else {
// printf("Load error\n");
// }
}
// else {
// printf("CoCreateInstance error\n");
// }
error_exit:
return hRes;
}
//*********************************************************************
#ifdef STAND_ALONE
int main(int argc, char** argv)
{
char lnkfile[MAX_PATH+1] = "" ;
int idx ;
for (idx=1; idx<argc; idx++) {
char *p = argv[idx];
_tcsncpy(lnkfile, p, MAX_PATH);
lnkfile[MAX_PATH] = 0 ;
}
if (lnkfile[0] == 0) {
printf("Usage: read_link link_filename\n");
return 1;
}
char szFilePath[MAX_PATH];
char szArgsPath[MAX_PATH];
CoInitialize(NULL); //lint !e534
// NOTE: HRESULT is an unsigned value!!
// The HRESULT data type is the same as the SCODE data type.
// An HRESULT value consists of the following fields:
// > A 1-bit code indicating severity, where zero represents success and 1 represents failure.
// Note: this means that this bit means HRESULT as signed int is negative on error.
// > A 4-bit reserved value.
// > An 11-bit code indicating responsibility for the error or warning,
// also known as a facility code.
// > A 16-bit code describing the error or warning.
HRESULT hRes = ResolveShortcut(lnkfile, szFilePath, szArgsPath);
CoUninitialize(); //lint !e534
if (hRes == 0) {
if (szArgsPath[0] == 0) {
printf("szFilePath: %s\n", szFilePath);
}
// see if destination should be extracted from arguments
// szArgsPath: /e,"D:\photos (all)\photos"
else if (_tcsncmp(szArgsPath, "/e,", 3) == 0) {
// printf("szArgsPath: %s\n", szArgsPath);
// see if quotes need to be removed from path
char *fptr = &szArgsPath[3] ;
if (*fptr == '"') {
// printf("found a quote char\n");
fptr++ ;
char *tl = _tcsrchr(fptr, '"');
if (tl != NULL) {
*tl = 0 ;
}
}
printf("szFilePath: %s\n", fptr);
}
else {
printf("szFilePath: %s\n", szFilePath);
}
}
else {
// HRESULT: 0x80070002
// printf("HRESULT: 0x%08X\n", (unsigned) hRes);
printf("%s: %s\n", lnkfile, get_system_message(hRes & 0xFFFF)) ;
}
puts("");
return 0;
} //lint !e818
#else
//*********************************************************************
bool read_shortcut_file(ffdata * fptr, TCHAR *shortcut_path)
{
bool target_exists = false ;
TCHAR szFilePath[MAX_PATH];
TCHAR szArgsPath[MAX_PATH];
*shortcut_path = 0 ; // initially empty string
// we need to build a full path+filename string here
TCHAR szFullSrcPath[MAX_PATH];
_stprintf(szFullSrcPath, _T("%s\\%s"), base_path.c_str(), fptr->filename.c_str()); //lint !e560: argument no. 4 should be a pointer (and it is...)
// syslog(_T("[%s]\n"), szFullSrcPath);
CoInitialize(NULL); //lint !e534
// NOTE: HRESULT is an unsigned value!!
// The HRESULT data type is the same as the SCODE data type.
// An HRESULT value consists of the following fields:
// > A 1-bit code indicating severity, where zero represents success and 1 represents failure.
// Note: this means that this bit means HRESULT as signed int is negative on error.
// > A 4-bit reserved value.
// > An 11-bit code indicating responsibility for the error or warning,
// also known as a facility code.
// > A 16-bit code describing the error or warning.
HRESULT hRes = ResolveShortcut(szFullSrcPath, szFilePath, szArgsPath);
CoUninitialize(); //lint !e534
if (hRes == 0) {
if (szArgsPath[0] == 0) {
// printf("szFilePath: %s\n", szFilePath);
_tcscpy(shortcut_path, szFilePath);
}
// see if destination should be extracted from arguments
// szArgsPath: /e,"D:\photos (all)\photos"
else if (_tcsncmp(szArgsPath, _T("/e,"), 3) == 0) {
// printf("szArgsPath: %s\n", szArgsPath);
// see if quotes need to be removed from path
TCHAR *sptr = &szArgsPath[3] ;
if (*sptr == '"') {
// printf("found a quote char\n");
sptr++ ;
TCHAR *tl = _tcsrchr(sptr, '"');
if (tl != NULL) {
*tl = 0 ;
}
}
// printf("szFilePath: %s\n", sptr);
_tcscpy(shortcut_path, sptr);
}
else {
// printf("szFilePath: %s\n", szFilePath);
_tcscpy(shortcut_path, szFilePath);
}
target_exists = true ;
// syslog("link: %s\n", shortcut_path);
}
else if (szFilePath[0] != 0) {
// Resolve/GetPath ran far enough to produce a path, but the
// target failed the existence check (or some other step after
// that failed) - hand the dead path back anyway so the caller
// can report it (e.g. tagged "LOST") instead of losing the
// information that a target used to be here.
_tcscpy(shortcut_path, szFilePath);
// target_exists stays false
}
return target_exists ;
}
#endif