schedsvc: Implement SchRpcEnumFolders.
[wine/multimedia.git] / dlls / schedsvc / schedsvc.c
bloba1fa4e0b503e4668d04985ac562d4dce63c73c70
1 /*
2 * Task Scheduler Service
4 * Copyright 2014 Dmitry Timoshkov
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "schrpc.h"
25 #include "taskschd.h"
26 #include "wine/debug.h"
28 #include "schedsvc_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(schedsvc);
32 static const char bom_utf8[] = { 0xef,0xbb,0xbf };
34 HRESULT __cdecl SchRpcHighestVersion(DWORD *version)
36 TRACE("%p\n", version);
38 *version = MAKELONG(3, 1);
39 return S_OK;
42 static WCHAR *get_full_name(const WCHAR *path, WCHAR **relative_path)
44 static const WCHAR tasksW[] = { '\\','t','a','s','k','s','\\',0 };
45 WCHAR *target;
46 int len;
48 len = GetSystemDirectoryW(NULL, 0);
49 len += strlenW(tasksW) + strlenW(path);
51 target = heap_alloc(len * sizeof(WCHAR));
52 if (target)
54 GetSystemDirectoryW(target, len);
55 strcatW(target, tasksW);
56 if (relative_path)
57 *relative_path = target + strlenW(target) - 1;
58 while (*path == '\\') path++;
59 strcatW(target, path);
61 return target;
65 * Recursively create all directories in the path.
67 static HRESULT create_directory(const WCHAR *path)
69 HRESULT hr = S_OK;
70 WCHAR *new_path;
71 int len;
73 new_path = heap_alloc((strlenW(path) + 1) * sizeof(WCHAR));
74 if (!new_path) return E_OUTOFMEMORY;
76 strcpyW(new_path, path);
78 len = strlenW(new_path);
79 while (len && new_path[len - 1] == '\\')
81 new_path[len - 1] = 0;
82 len--;
85 while (!CreateDirectoryW(new_path, NULL))
87 WCHAR *slash;
88 DWORD last_error = GetLastError();
90 if (last_error == ERROR_ALREADY_EXISTS || last_error != ERROR_PATH_NOT_FOUND ||
91 !(slash = strrchrW(new_path, '\\')))
93 hr = HRESULT_FROM_WIN32(last_error);
94 break;
97 len = slash - new_path;
98 new_path[len] = 0;
99 hr = create_directory(new_path);
100 if (hr != S_OK) break;
101 new_path[len] = '\\';
104 heap_free(new_path);
105 return hr;
108 static HRESULT write_xml_utf8(const WCHAR *name, DWORD disposition, const WCHAR *xmlW)
110 static const char comment[] = "<!-- Task definition created by Wine -->\n";
111 HANDLE hfile;
112 DWORD size;
113 char *xml;
114 HRESULT hr = S_OK;
116 hfile = CreateFileW(name, GENERIC_WRITE, 0, NULL, disposition, 0, 0);
117 if (hfile == INVALID_HANDLE_VALUE)
119 if (GetLastError() == ERROR_FILE_EXISTS)
120 return HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS);
122 return HRESULT_FROM_WIN32(GetLastError());
125 size = WideCharToMultiByte(CP_UTF8, 0, xmlW, -1, NULL, 0, NULL, NULL);
126 xml = heap_alloc(size);
127 if (!xml)
129 CloseHandle(hfile);
130 return E_OUTOFMEMORY;
132 WideCharToMultiByte(CP_UTF8, 0, xmlW, -1, xml, size, NULL, NULL);
134 if (!WriteFile(hfile, bom_utf8, sizeof(bom_utf8), &size, NULL))
136 hr = HRESULT_FROM_WIN32(GetLastError());
137 goto failed;
139 if (!WriteFile(hfile, comment, strlen(comment), &size, NULL))
141 hr = HRESULT_FROM_WIN32(GetLastError());
142 goto failed;
145 /* skip XML declaration with UTF-16 specifier */
146 if (!memcmp(xml, "<?xml", 5))
148 const char *p = strchr(xml, '>');
149 if (p++) while (isspace(*p)) p++;
150 else p = xml;
151 if (!WriteFile(hfile, p, strlen(p), &size, NULL))
152 hr = HRESULT_FROM_WIN32(GetLastError());
154 else
156 if (!WriteFile(hfile, xml, strlen(xml), &size, NULL))
157 hr = HRESULT_FROM_WIN32(GetLastError());
160 failed:
161 heap_free(xml);
162 CloseHandle(hfile);
163 return hr;
166 HRESULT __cdecl SchRpcRegisterTask(const WCHAR *path, const WCHAR *xml, DWORD flags, const WCHAR *sddl,
167 DWORD task_logon_type, DWORD n_creds, const TASK_USER_CRED *creds,
168 WCHAR **actual_path, TASK_XML_ERROR_INFO **xml_error_info)
170 WCHAR *full_name, *relative_path;
171 DWORD disposition;
172 HRESULT hr;
174 TRACE("%s,%s,%#x,%s,%u,%u,%p,%p,%p\n", debugstr_w(path), debugstr_w(xml), flags,
175 debugstr_w(sddl), task_logon_type, n_creds, creds, actual_path, xml_error_info);
177 *actual_path = NULL;
178 *xml_error_info = NULL;
180 /* FIXME: assume that validation is performed on the client side */
181 if (flags & TASK_VALIDATE_ONLY) return S_OK;
183 full_name = get_full_name(path, &relative_path);
184 if (!full_name) return E_OUTOFMEMORY;
186 if (strchrW(path, '\\') || strchrW(path, '/'))
188 WCHAR *p = strrchrW(full_name, '/');
189 if (!p) p = strrchrW(full_name, '\\');
190 *p = 0;
191 hr = create_directory(full_name);
192 *p = '\\';
195 switch (flags & (TASK_CREATE | TASK_UPDATE))
197 default:
198 case TASK_CREATE:
199 disposition = CREATE_NEW;
200 break;
202 case TASK_UPDATE:
203 disposition = OPEN_EXISTING;
204 break;
206 case (TASK_CREATE | TASK_UPDATE):
207 disposition = OPEN_ALWAYS;
208 break;
211 hr = write_xml_utf8(full_name, disposition, xml);
212 if (hr == S_OK)
214 *actual_path = heap_strdupW(relative_path);
215 schedsvc_auto_start();
218 heap_free(full_name);
219 return hr;
222 static int detect_encoding(const void *buffer, DWORD size)
224 if (size >= sizeof(bom_utf8) && !memcmp(buffer, bom_utf8, sizeof(bom_utf8)))
225 return CP_UTF8;
226 else
228 int flags = IS_TEXT_UNICODE_SIGNATURE |
229 IS_TEXT_UNICODE_REVERSE_SIGNATURE |
230 IS_TEXT_UNICODE_ODD_LENGTH;
231 IsTextUnicode(buffer, size, &flags);
232 if (flags & IS_TEXT_UNICODE_SIGNATURE)
233 return -1;
234 if (flags & IS_TEXT_UNICODE_REVERSE_SIGNATURE)
235 return -2;
236 return CP_ACP;
240 static HRESULT read_xml(const WCHAR *name, WCHAR **xml)
242 HANDLE hfile;
243 DWORD size;
244 char *src;
245 int cp;
247 hfile = CreateFileW(name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
248 if (hfile == INVALID_HANDLE_VALUE)
249 return HRESULT_FROM_WIN32(GetLastError());
251 size = GetFileSize(hfile, NULL);
252 src = heap_alloc(size + 2);
253 if (!src)
255 CloseHandle(hfile);
256 return E_OUTOFMEMORY;
259 src[size] = 0;
260 src[size + 1] = 0;
262 ReadFile(hfile, src, size, &size, NULL);
263 CloseHandle(hfile);
265 cp = detect_encoding(src, size);
266 if (cp < 0)
268 *xml = (WCHAR *)src;
269 return S_OK;
272 if (cp == CP_UTF8 && size >= sizeof(bom_utf8) && !memcmp(src, bom_utf8, sizeof(bom_utf8)))
273 src += sizeof(bom_utf8);
275 size = MultiByteToWideChar(cp, 0, src, -1, NULL, 0);
276 *xml = heap_alloc(size * sizeof(WCHAR));
277 if (!*xml) return E_OUTOFMEMORY;
278 MultiByteToWideChar(cp, 0, src, -1, *xml, size);
279 return S_OK;
282 HRESULT __cdecl SchRpcRetrieveTask(const WCHAR *path, const WCHAR *languages, ULONG *n_languages, WCHAR **xml)
284 WCHAR *full_name;
285 HRESULT hr;
287 TRACE("%s,%s,%p,%p\n", debugstr_w(path), debugstr_w(languages), n_languages, xml);
289 full_name = get_full_name(path, NULL);
290 if (!full_name) return E_OUTOFMEMORY;
292 hr = read_xml(full_name, xml);
293 if (hr != S_OK) *xml = NULL;
295 heap_free(full_name);
296 return hr;
299 HRESULT __cdecl SchRpcCreateFolder(const WCHAR *path, const WCHAR *sddl, DWORD flags)
301 WCHAR *full_name;
302 HRESULT hr;
304 TRACE("%s,%s,%#x\n", debugstr_w(path), debugstr_w(sddl), flags);
306 if (flags) return E_INVALIDARG;
308 full_name = get_full_name(path, NULL);
309 if (!full_name) return E_OUTOFMEMORY;
311 hr = create_directory(full_name);
313 heap_free(full_name);
314 return hr;
317 HRESULT __cdecl SchRpcSetSecurity(const WCHAR *path, const WCHAR *sddl, DWORD flags)
319 FIXME("%s,%s,%#x: stub\n", debugstr_w(path), debugstr_w(sddl), flags);
320 return E_NOTIMPL;
323 HRESULT __cdecl SchRpcGetSecurity(const WCHAR *path, DWORD flags, WCHAR **sddl)
325 FIXME("%s,%#x,%p: stub\n", debugstr_w(path), flags, sddl);
326 return E_NOTIMPL;
329 static void free_list(TASK_NAMES list, LONG count)
331 LONG i;
333 for (i = 0; i < count; i++)
334 heap_free(list[i]);
336 heap_free(list);
339 static inline BOOL is_directory(const WIN32_FIND_DATAW *data)
341 if (data->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
343 if (data->cFileName[0] == '.')
345 if (!data->cFileName[1] || (data->cFileName[1] == '.' && !data->cFileName[2]))
346 return FALSE;
348 return TRUE;
350 return FALSE;
353 HRESULT __cdecl SchRpcEnumFolders(const WCHAR *path, DWORD flags, DWORD *start_index, DWORD n_requested,
354 DWORD *n_names, TASK_NAMES *names)
356 static const WCHAR allW[] = {'\\','*',0};
357 HRESULT hr = S_OK;
358 WCHAR *full_name;
359 WCHAR pathW[MAX_PATH];
360 WIN32_FIND_DATAW data;
361 HANDLE handle;
362 DWORD allocated, count, index;
363 TASK_NAMES list;
365 TRACE("%s,%#x,%u,%u,%p,%p\n", debugstr_w(path), flags, *start_index, n_requested, n_names, names);
367 *n_names = 0;
368 *names = NULL;
370 if (flags & ~TASK_ENUM_HIDDEN) return E_INVALIDARG;
372 if (!n_requested) n_requested = ~0u;
374 full_name = get_full_name(path, NULL);
375 if (!full_name) return E_OUTOFMEMORY;
377 if (strlenW(full_name) + 2 > MAX_PATH)
379 heap_free(full_name);
380 return HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
383 strcpyW(pathW, full_name);
384 strcatW(pathW, allW);
386 heap_free(full_name);
388 allocated = 64;
389 list = heap_alloc(allocated * sizeof(list[0]));
390 if (!list) return E_OUTOFMEMORY;
392 index = count = 0;
394 handle = FindFirstFileW(pathW, &data);
395 if (handle == INVALID_HANDLE_VALUE)
397 heap_free(list);
398 if (GetLastError() == ERROR_PATH_NOT_FOUND)
399 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
400 return HRESULT_FROM_WIN32(GetLastError());
405 if (is_directory(&data) && index++ >= *start_index)
407 if (count >= allocated)
409 TASK_NAMES new_list;
410 allocated *= 2;
411 new_list = heap_realloc(list, allocated * sizeof(list[0]));
412 if (!new_list)
414 hr = E_OUTOFMEMORY;
415 break;
417 list = new_list;
420 TRACE("adding %s\n", debugstr_w(data.cFileName));
422 list[count] = heap_strdupW(data.cFileName);
423 if (!list[count])
425 hr = E_OUTOFMEMORY;
426 break;
429 count++;
431 if (count >= n_requested)
433 hr = S_FALSE;
434 break;
437 } while (FindNextFileW(handle, &data));
439 FindClose(handle);
441 if (FAILED(hr))
443 free_list(list, count);
444 return hr;
447 *n_names = count;
449 if (count)
451 *names = list;
452 *start_index = index;
453 return hr;
456 heap_free(list);
457 *names = NULL;
458 return *start_index ? S_FALSE : S_OK;
461 HRESULT __cdecl SchRpcEnumTasks(const WCHAR *path, DWORD flags, DWORD *start_index, DWORD n_requested,
462 DWORD *n_names, TASK_NAMES *names)
464 FIXME("%s,%#x,%p,%u,%p,%p: stub\n", debugstr_w(path), flags, start_index, n_requested, n_names, names);
465 return E_NOTIMPL;
468 HRESULT __cdecl SchRpcEnumInstances(const WCHAR *path, DWORD flags, DWORD *n_guids, GUID **guids)
470 FIXME("%s,%#x,%p,%p: stub\n", debugstr_w(path), flags, n_guids, guids);
471 return E_NOTIMPL;
474 HRESULT __cdecl SchRpcGetInstanceInfo(GUID guid, WCHAR **path, DWORD *task_state, WCHAR **action,
475 WCHAR **info, DWORD *n_instances, GUID **instances, DWORD *pid)
477 FIXME("%s,%p,%p,%p,%p,%p,%p,%p: stub\n", wine_dbgstr_guid(&guid), path, task_state, action,
478 info, n_instances, instances, pid);
479 return E_NOTIMPL;
482 HRESULT __cdecl SchRpcStopInstance(GUID guid, DWORD flags)
484 FIXME("%s,%#x: stub\n", wine_dbgstr_guid(&guid), flags);
485 return E_NOTIMPL;
488 HRESULT __cdecl SchRpcStop(const WCHAR *path, DWORD flags)
490 FIXME("%s,%#x: stub\n", debugstr_w(path), flags);
491 return E_NOTIMPL;
494 HRESULT __cdecl SchRpcRun(const WCHAR *path, DWORD n_args, const WCHAR **args, DWORD flags,
495 DWORD session_id, const WCHAR *user, GUID *guid)
497 FIXME("%s,%u,%p,%#x,%#x,%s,%p: stub\n", debugstr_w(path), n_args, args, flags,
498 session_id, debugstr_w(user), guid);
499 return E_NOTIMPL;
502 HRESULT __cdecl SchRpcDelete(const WCHAR *path, DWORD flags)
504 WCHAR *full_name;
505 HRESULT hr = S_OK;
507 TRACE("%s,%#x\n", debugstr_w(path), flags);
509 if (flags) return E_INVALIDARG;
511 while (*path == '\\' || *path == '/') path++;
512 if (!*path) return E_ACCESSDENIED;
514 full_name = get_full_name(path, NULL);
515 if (!full_name) return E_OUTOFMEMORY;
517 if (!RemoveDirectoryW(full_name))
519 hr = HRESULT_FROM_WIN32(GetLastError());
520 if (hr == HRESULT_FROM_WIN32(ERROR_DIRECTORY))
521 hr = DeleteFileW(full_name) ? S_OK : HRESULT_FROM_WIN32(GetLastError());
524 heap_free(full_name);
525 return hr;
528 HRESULT __cdecl SchRpcRename(const WCHAR *path, const WCHAR *name, DWORD flags)
530 FIXME("%s,%s,%#x: stub\n", debugstr_w(path), debugstr_w(name), flags);
531 return E_NOTIMPL;
534 HRESULT __cdecl SchRpcScheduledRuntimes(const WCHAR *path, SYSTEMTIME *start, SYSTEMTIME *end, DWORD flags,
535 DWORD n_requested, DWORD *n_runtimes, SYSTEMTIME **runtimes)
537 FIXME("%s,%p,%p,%#x,%u,%p,%p: stub\n", debugstr_w(path), start, end, flags,
538 n_requested, n_runtimes, runtimes);
539 return E_NOTIMPL;
542 HRESULT __cdecl SchRpcGetLastRunInfo(const WCHAR *path, SYSTEMTIME *last_runtime, DWORD *last_return_code)
544 FIXME("%s,%p,%p: stub\n", debugstr_w(path), last_runtime, last_return_code);
545 return E_NOTIMPL;
548 HRESULT __cdecl SchRpcGetTaskInfo(const WCHAR *path, DWORD flags, DWORD *enabled, DWORD *task_state)
550 FIXME("%s,%#x,%p,%p: stub\n", debugstr_w(path), flags, enabled, task_state);
551 return E_NOTIMPL;
554 HRESULT __cdecl SchRpcGetNumberOfMissedRuns(const WCHAR *path, DWORD *runs)
556 FIXME("%s,%p: stub\n", debugstr_w(path), runs);
557 return E_NOTIMPL;
560 HRESULT __cdecl SchRpcEnableTask(const WCHAR *path, DWORD enabled)
562 FIXME("%s,%u: stub\n", debugstr_w(path), enabled);
563 return E_NOTIMPL;