copy.c 6.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright 2021 Hugh McMaster
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

19
#include <stdio.h>
20 21
#include "reg.h"

22 23
struct key {
    HKEY root;      /* system key */
24
    WCHAR *subkey;  /* relative path to subkey */
25
    HKEY hkey;      /* handle to opened or created key */
26 27 28 29 30 31 32
    WCHAR *path;    /* full path to subkey */
};

enum operation {
    COPY_NO,
    COPY_YES,
    COPY_ALL
33 34 35 36 37 38 39 40 41 42
};

static void output_error(LONG rc)
{
    if (rc == ERROR_FILE_NOT_FOUND)
        output_message(STRING_KEY_NONEXIST);
    else
        output_message(STRING_ACCESS_DENIED);
}

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
static enum operation ask_overwrite_value(WCHAR *path, WCHAR *value)
{
    HMODULE hmod;
    static WCHAR Ybuffer[4];
    static WCHAR Nbuffer[4];
    static WCHAR Abuffer[4];
    static WCHAR defval[32];
    WCHAR answer[MAX_PATH];
    WCHAR *str;
    DWORD count;

    hmod = GetModuleHandleW(NULL);
    LoadStringW(hmod, STRING_YES, Ybuffer, ARRAY_SIZE(Ybuffer));
    LoadStringW(hmod, STRING_NO,  Nbuffer, ARRAY_SIZE(Nbuffer));
    LoadStringW(hmod, STRING_ALL, Abuffer, ARRAY_SIZE(Abuffer));
    LoadStringW(hmod, STRING_DEFAULT_VALUE, defval, ARRAY_SIZE(defval));

    str = (value && *value) ? value : defval;

    while (1)
    {
        output_message(STRING_COPY_CONFIRM, path, str);
        output_message(STRING_YESNOALL);

        ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), answer, ARRAY_SIZE(answer), &count, NULL);

        *answer = towupper(*answer);

        if (*answer == *Ybuffer)
            return COPY_YES;
        if (*answer == *Nbuffer)
            return COPY_NO;
        if (*answer == *Abuffer)
            return COPY_ALL;
    }
}

80
static int run_copy(struct key *src, struct key *dest, REGSAM sam, BOOL recurse, BOOL force)
81 82 83 84 85
{
    LONG rc;
    DWORD max_subkey_len;
    DWORD max_name_len, name_len;
    DWORD max_data_size, data_size;
86
    DWORD type, dispos, i;
87 88 89
    WCHAR *name = NULL;
    BYTE *data = NULL;

90
    if ((rc = RegOpenKeyExW(src->root, src->subkey, 0, KEY_READ|sam, &src->hkey)))
91 92 93 94 95 96
    {
        output_error(rc);
        return 1;
    }

    if ((rc = RegCreateKeyExW(dest->root, dest->subkey, 0, NULL, REG_OPTION_NON_VOLATILE,
97
                              KEY_READ|KEY_WRITE|sam, NULL, &dest->hkey, &dispos)))
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
    {
        RegCloseKey(src->hkey);
        output_error(rc);
        return 1;
    }

    rc = RegQueryInfoKeyW(src->hkey, NULL, NULL, NULL, NULL, &max_subkey_len, NULL,
                          NULL, &max_name_len, &max_data_size, NULL, NULL);
    if (rc) goto cleanup;

    max_name_len = max(max_subkey_len, max_name_len) + 1;

    if (!(name = malloc(max_name_len * sizeof(WCHAR))))
    {
        rc = ERROR_NOT_ENOUGH_MEMORY;
        goto cleanup;
    }

    if (!(data = malloc(max_data_size)))
    {
        rc = ERROR_NOT_ENOUGH_MEMORY;
        goto cleanup;
    }

    for (i = 0; ; i++)
    {
        name_len = max_name_len;
        data_size = max_data_size;

        rc = RegEnumValueW(src->hkey, i, name, &name_len, NULL, &type, data, &data_size);
        if (rc == ERROR_NO_MORE_ITEMS) break;
        if (rc) goto cleanup;

131 132 133 134 135 136 137 138 139 140 141 142
        if (!force && dispos == REG_OPENED_EXISTING_KEY)
        {
            if (!RegQueryValueExW(dest->hkey, name, NULL, NULL, NULL, NULL))
            {
                enum operation op;

                op = ask_overwrite_value(src->path, name);
                if (op == COPY_NO) continue;
                if (op == COPY_ALL) force = TRUE;
            }
        }

143 144 145 146 147 148 149
        if ((rc = RegSetValueExW(dest->hkey, name, 0, type, data, data_size)))
        {
            output_error(rc);
            goto cleanup;
        }
    }

150 151 152
    for (i = 0; recurse; i++)
    {
        struct key subkey_src, subkey_dest;
153
        size_t path_len;
154 155 156 157 158 159 160 161 162 163 164 165

        name_len = max_name_len;

        rc = RegEnumKeyExW(src->hkey, i, name, &name_len, NULL, NULL, NULL, NULL);
        if (rc) break;

        subkey_src.root = src->hkey;
        subkey_src.subkey = name;

        subkey_dest.root = dest->hkey;
        subkey_dest.subkey = name;

166 167 168 169 170 171 172 173 174 175
        path_len = lstrlenW(src->path) + name_len + 2;

        if (!(subkey_src.path = malloc(path_len * sizeof(WCHAR))))
        {
            rc = ERROR_NOT_ENOUGH_MEMORY;
            goto cleanup;
        }

        swprintf(subkey_src.path, path_len, L"%s\\%s", src->path, name);

176
        rc = run_copy(&subkey_src, &subkey_dest, sam, TRUE, force);
177 178 179

        free(subkey_src.path);

180 181 182
        if (rc) goto cleanup;
    }

183 184 185 186 187 188 189 190 191 192
cleanup:
    free(name);
    free(data);

    RegCloseKey(src->hkey);
    RegCloseKey(dest->hkey);

    return rc != ERROR_NO_MORE_ITEMS;
}

193 194
int reg_copy(int argc, WCHAR *argvW[])
{
195 196
    struct key src, dest;
    BOOL recurse = FALSE, force = FALSE;
197
    REGSAM sam = 0;
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    int i;

    if (argc == 3)
        goto invalid;

    if (!parse_registry_key(argvW[2], &src.root, &src.subkey))
        return 1;

    if (!parse_registry_key(argvW[3], &dest.root, &dest.subkey))
        return 1;

    for (i = 4; i < argc; i++)
    {
        WCHAR *str;

        if (argvW[i][0] != '/' && argvW[i][0] != '-')
            goto invalid;

        str = &argvW[i][1];

218 219 220 221 222 223 224 225 226 227
        if (!lstrcmpiW(str, L"reg:32"))
        {
            if (sam & KEY_WOW64_32KEY) goto invalid;
            sam |= KEY_WOW64_32KEY;
            continue;
        }
        else if (!lstrcmpiW(str, L"reg:64"))
        {
            if (sam & KEY_WOW64_64KEY) goto invalid;
            sam |= KEY_WOW64_64KEY;
228
            continue;
229
        }
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
        else if (!str[0] || str[1])
            goto invalid;

        switch (towlower(*str))
        {
        case 's':
            if (recurse) goto invalid;
            recurse = TRUE;
            break;
        case 'f':
            if (force) goto invalid;
            force = TRUE;
            break;
        default:
            goto invalid;
        }
    }

248 249 250
    if (sam == (KEY_WOW64_32KEY|KEY_WOW64_64KEY))
        goto invalid;

251 252 253 254 255 256
    if (src.root == dest.root && !lstrcmpiW(src.subkey, dest.subkey))
    {
        output_message(STRING_COPY_SRC_DEST_SAME);
        return 1;
    }

257 258
    src.path = src.subkey;

259
    return run_copy(&src, &dest, sam, recurse, force);
260 261 262 263

invalid:
    output_message(STRING_INVALID_SYNTAX);
    output_message(STRING_FUNC_HELP, wcsupr(argvW[1]));
264 265
    return 1;
}