OLD | NEW |
| (Empty) |
1 // Copyright 2007-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 // | |
16 // Saves the arguments passed to this application to saved_arguments.txt in the | |
17 // same directory as the executable. | |
18 | |
19 #include <atlstr.h> | |
20 #include <stdio.h> | |
21 #include <tchar.h> | |
22 #include <windows.h> | |
23 #include "omaha/base/app_util.h" | |
24 #include "omaha/base/scoped_any.h" | |
25 | |
26 namespace { | |
27 | |
28 | |
29 const TCHAR kSavedArgumentsFileName[] = _T("saved_arguments.txt"); | |
30 | |
31 // Reports a Win32 error to the command line and debug output then exits the | |
32 // process. Call immediately after the failed Win32 API call. | |
33 void HandleWin32ErrorAndExit(const TCHAR* method) { | |
34 _ASSERTE(method); | |
35 int res = ::GetLastError(); | |
36 CString error_message; | |
37 error_message.Format(_T("%s failed with error %i."), method, res); | |
38 | |
39 | |
40 _tprintf(_T("%s\n"), error_message); | |
41 | |
42 CString debug_message; | |
43 debug_message.Format(_T("[SaveArguments.exe][%s]"), error_message); | |
44 ::OutputDebugString(debug_message); | |
45 | |
46 exit(res); | |
47 } | |
48 | |
49 // Performs actions that are useful for debugging. | |
50 void DoDebugHelper() { | |
51 // This is useful for debugging Code Red. | |
52 CString message; | |
53 message.AppendFormat(_T("[SaveArguments.exe][Temp directory: %s]"), | |
54 omaha::app_util::GetTempDir()); | |
55 ::OutputDebugString(message); | |
56 } | |
57 | |
58 // Performs actions that are helpful or required for Omaha unit tests. | |
59 void DoUnitTestHelpers() { | |
60 // The following code adapted from mi.cpp allows unit test to verify that the | |
61 // executable file still exists and has not been deleted after the process | |
62 // is created. | |
63 TCHAR file_name[MAX_PATH] = {0}; | |
64 if (!::GetModuleFileName(NULL, file_name, MAX_PATH)) { | |
65 HandleWin32ErrorAndExit(_T("GetModuleFileName")); | |
66 } | |
67 DWORD handle = 0; | |
68 DWORD ver_info_size = ::GetFileVersionInfoSize(file_name, &handle); | |
69 if (ver_info_size == 0) { | |
70 HandleWin32ErrorAndExit(_T("GetFileVersionInfoSize")); | |
71 } | |
72 } | |
73 | |
74 // Writes the provided arguments to the file. | |
75 // Returns whether it was successful. | |
76 int WriteArgsToFile(const CString& arguments) { | |
77 CString file_path(omaha::app_util::GetCurrentModuleDirectory()); | |
78 if (!::PathAppend(CStrBuf(file_path, MAX_PATH), kSavedArgumentsFileName)) { | |
79 _ASSERTE(false); | |
80 return -1; | |
81 } | |
82 | |
83 scoped_hfile file(::CreateFile(file_path, | |
84 GENERIC_READ | GENERIC_WRITE, | |
85 0, // do not share | |
86 NULL, // default security | |
87 CREATE_ALWAYS, // overwrite existing | |
88 FILE_ATTRIBUTE_NORMAL, | |
89 NULL)); // no template | |
90 if (get(file) == INVALID_HANDLE_VALUE) { | |
91 HandleWin32ErrorAndExit(_T("CreateFile")); | |
92 } | |
93 | |
94 DWORD bytes_written = 0; | |
95 if (!::WriteFile(get(file), | |
96 arguments.GetString(), | |
97 arguments.GetLength() * sizeof(TCHAR), | |
98 &bytes_written, | |
99 NULL)) { | |
100 HandleWin32ErrorAndExit(_T("WriteFile")); | |
101 } | |
102 | |
103 return 0; | |
104 } | |
105 | |
106 } // namespace | |
107 | |
108 | |
109 // Returns 0 on success and non-zero otherwise. | |
110 int _tmain(int argc, TCHAR* argv[]) { | |
111 DoDebugHelper(); | |
112 DoUnitTestHelpers(); | |
113 | |
114 CString arguments; | |
115 | |
116 // Skip the first argument, which is the executable path. | |
117 for (int i = 1; i < argc; i++) { | |
118 arguments += argv[i]; | |
119 if (i < argc - 1) { | |
120 arguments += " "; | |
121 } | |
122 } | |
123 | |
124 return WriteArgsToFile(arguments); | |
125 } | |
OLD | NEW |