src/jdk.incubator.jpackage/windows/native/libjpackage/FileUtils.cpp
author herrick
Fri, 08 Nov 2019 14:53:03 -0500
branchJDK-8200758-branch
changeset 58994 b09ba68c6a19
parent 57909 src/jdk.jpackage/windows/native/libjpackage/FileUtils.cpp@c7de06ed4b54
permissions -rw-r--r--
8233636 : Make jpackage an incubator and remove tool provider implementation Reviewed-by: asemenyuk, almatvee, kcr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57909
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
     1
/*
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
     4
 *
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    10
 *
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    15
 * accompanied this code).
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    16
 *
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    20
 *
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    23
 * questions.
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    24
 */
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    25
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    26
#include <memory>
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    27
#include <algorithm>
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    28
#include <shlwapi.h>
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    29
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    30
#include "FileUtils.h"
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    31
#include "WinErrorHandling.h"
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    32
#include "Log.h"
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    33
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    34
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    35
// Needed by FileUtils::isDirectoryNotEmpty
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    36
#pragma comment(lib, "shlwapi")
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    37
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    38
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    39
namespace FileUtils {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    40
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    41
namespace {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    42
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    43
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    44
tstring reservedFilenameChars() {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    45
    tstring buf;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    46
    for (char charCode = 0; charCode < 32; ++charCode) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    47
        buf.append(1, charCode);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    48
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    49
    buf += _T("<>:\"|?*/\\");
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    50
    return buf;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    51
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    52
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    53
} // namespace
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    54
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    55
bool isDirSeparator(const tstring::value_type c) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    56
    return (c == '/' || c == '\\');
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    57
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    58
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    59
bool isFileExists(const tstring &filePath) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    60
    return GetFileAttributes(filePath.c_str()) != INVALID_FILE_ATTRIBUTES;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    61
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    62
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    63
namespace {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    64
bool isDirectoryAttrs(const DWORD attrs) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    65
    return attrs != INVALID_FILE_ATTRIBUTES
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    66
            && (attrs & FILE_ATTRIBUTE_DIRECTORY) != 0;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    67
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    68
} // namespace
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    69
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    70
bool isDirectory(const tstring &filePath) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    71
    return isDirectoryAttrs(GetFileAttributes(filePath.c_str()));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    72
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    73
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    74
bool isDirectoryNotEmpty(const tstring &dirPath) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    75
    if (!isDirectory(dirPath)) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    76
        return false;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    77
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    78
    return FALSE == PathIsDirectoryEmpty(dirPath.c_str());
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    79
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    80
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    81
tstring dirname(const tstring &path) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    82
    tstring::size_type pos = path.find_last_of(_T("\\/"));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    83
    if (pos != tstring::npos) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    84
        pos = path.find_last_not_of(_T("\\/"), pos); // skip trailing slashes
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    85
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    86
    return pos == tstring::npos ? tstring() : path.substr(0, pos + 1);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    87
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    88
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    89
tstring basename(const tstring &path) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    90
    const tstring::size_type pos = path.find_last_of(_T("\\/"));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    91
    if (pos == tstring::npos) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    92
        return path;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    93
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    94
    return path.substr(pos + 1);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    95
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    96
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    97
tstring suffix(const tstring &path) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    98
    const tstring::size_type pos = path.rfind('.');
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
    99
    if (pos == tstring::npos) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   100
        return tstring();
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   101
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   102
    const tstring::size_type dirSepPos = path.find_first_of(_T("\\/"),
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   103
                                                            pos + 1);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   104
    if (dirSepPos != tstring::npos) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   105
        return tstring();
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   106
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   107
    // test for '/..' and '..' cases
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   108
    if (pos != 0 && path[pos - 1] == '.'
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   109
                            && (pos == 1 || isDirSeparator(path[pos - 2]))) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   110
        return tstring();
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   111
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   112
    return path.substr(pos);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   113
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   114
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   115
tstring combinePath(const tstring& parent, const tstring& child) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   116
    if (parent.empty()) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   117
        return child;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   118
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   119
    if (child.empty()) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   120
        return parent;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   121
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   122
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   123
    tstring parentWOSlash = removeTrailingSlash(parent);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   124
    // also handle the case when child contains starting slash
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   125
    bool childHasSlash = isDirSeparator(child.front());
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   126
    tstring childWOSlash = childHasSlash ? child.substr(1) : child;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   127
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   128
    return parentWOSlash + _T("\\") + childWOSlash;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   129
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   130
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   131
tstring removeTrailingSlash(const tstring& path) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   132
    if (path.empty()) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   133
        return path;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   134
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   135
    tstring::const_reverse_iterator it = path.rbegin();
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   136
    tstring::const_reverse_iterator end = path.rend();
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   137
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   138
    while (it != end && isDirSeparator(*it)) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   139
        ++it;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   140
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   141
    return path.substr(0, end - it);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   142
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   143
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   144
tstring normalizePath(tstring v) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   145
    std::replace(v.begin(), v.end(), '/', '\\');
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   146
    return tstrings::toLower(v);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   147
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   148
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   149
namespace {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   150
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   151
bool createNewFile(const tstring& path) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   152
    HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   153
            FILE_ATTRIBUTE_NORMAL, NULL);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   154
    // if the file exists => h == INVALID_HANDLE_VALUE & GetLastError
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   155
    // returns ERROR_FILE_EXISTS
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   156
    if (h != INVALID_HANDLE_VALUE) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   157
        CloseHandle(h);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   158
        LOG_TRACE(tstrings::any() << "Created [" << path << "] file");
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   159
        return true;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   160
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   161
    return false;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   162
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   163
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   164
} // namespace
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   165
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   166
tstring createTempFile(const tstring &prefix, const tstring &suffix,
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   167
        const tstring &path) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   168
    const tstring invalidChars = reservedFilenameChars();
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   169
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   170
    if (prefix.find_first_of(invalidChars) != tstring::npos) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   171
        JP_THROW(tstrings::any() << "Illegal characters in prefix=" << prefix);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   172
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   173
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   174
    if (suffix.find_first_of(invalidChars) != tstring::npos) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   175
        JP_THROW(tstrings::any() << "Illegal characters in suffix=" << suffix);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   176
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   177
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   178
    int rnd = (int)GetTickCount();
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   179
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   180
    // do no more than 100 attempts
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   181
    for (int i=0; i<100; i++) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   182
        const tstring filePath = mkpath() << path << (prefix
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   183
                + (tstrings::any() << (rnd + i)).tstr() + suffix);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   184
        if (createNewFile(filePath)) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   185
            return filePath;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   186
        }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   187
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   188
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   189
    // 100 attempts failed
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   190
    JP_THROW(tstrings::any() << "createTempFile("  << prefix << ", "
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   191
                                                    << suffix << ", "
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   192
                                                    << path << ") failed");
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   193
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   194
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   195
tstring createTempDirectory(const tstring &prefix, const tstring &suffix,
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   196
        const tstring &basedir) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   197
    const tstring filePath = createTempFile(prefix, suffix, basedir);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   198
    // delete the file and create directory with the same name
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   199
    deleteFile(filePath);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   200
    createDirectory(filePath);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   201
    return filePath;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   202
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   203
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   204
tstring createUniqueFile(const tstring &prototype) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   205
    if (createNewFile(prototype)) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   206
        return prototype;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   207
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   208
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   209
    return createTempFile(replaceSuffix(basename(prototype)),
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   210
            suffix(prototype), dirname(prototype));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   211
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   212
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   213
namespace {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   214
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   215
void createDir(const tstring path, LPSECURITY_ATTRIBUTES saAttr,
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   216
        tstring_array* createdDirs=0) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   217
    if (CreateDirectory(path.c_str(), saAttr)) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   218
        LOG_TRACE(tstrings::any() << "Created [" << path << "] directory");
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   219
        if (createdDirs) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   220
            createdDirs->push_back(removeTrailingSlash(path));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   221
        }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   222
    } else {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   223
        const DWORD createDirectoryErr = GetLastError();
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   224
        // if saAttr is specified, fail even if the directory exists
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   225
        if (saAttr != NULL || !isDirectory(path)) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   226
            JP_THROW(SysError(tstrings::any() << "CreateDirectory("
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   227
                << path << ") failed", CreateDirectory, createDirectoryErr));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   228
        }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   229
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   230
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   231
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   232
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   233
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   234
void createDirectory(const tstring &path, tstring_array* createdDirs) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   235
    const tstring dirPath = removeTrailingSlash(path) + _T("\\");
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   236
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   237
    tstring::size_type pos = dirPath.find_first_of(_T("\\/"));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   238
    while (pos != tstring::npos) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   239
        const tstring subdirPath = dirPath.substr(0, pos + 1);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   240
        createDir(subdirPath, NULL, createdDirs);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   241
        pos = dirPath.find_first_of(_T("\\/"), pos + 1);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   242
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   243
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   244
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   245
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   246
void copyFile(const tstring& fromPath, const tstring& toPath,
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   247
        bool failIfExists) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   248
    createDirectory(dirname(toPath));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   249
    if (!CopyFile(fromPath.c_str(), toPath.c_str(),
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   250
            (failIfExists ? TRUE : FALSE))) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   251
        JP_THROW(SysError(tstrings::any()
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   252
                << "CopyFile(" << fromPath << ", " << toPath << ", "
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   253
                << failIfExists << ") failed", CopyFile));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   254
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   255
    LOG_TRACE(tstrings::any() << "Copied [" << fromPath << "] file to ["
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   256
            << toPath << "]");
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   257
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   258
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   259
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   260
namespace {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   261
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   262
void moveFileImpl(const tstring& fromPath, const tstring& toPath,
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   263
        DWORD flags) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   264
    const bool isDir = isDirectory(fromPath);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   265
    if (!MoveFileEx(fromPath.c_str(), toPath.empty() ? NULL : toPath.c_str(),
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   266
            flags)) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   267
        JP_THROW(SysError(tstrings::any() << "MoveFileEx(" << fromPath
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   268
                << ", " << toPath << ", " << flags << ") failed", MoveFileEx));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   269
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   270
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   271
    const bool onReboot = 0 != (flags & MOVEFILE_DELAY_UNTIL_REBOOT);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   272
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   273
    const LPCTSTR label = isDir ? _T("folder") : _T("file");
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   274
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   275
    tstrings::any msg;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   276
    if (!toPath.empty()) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   277
        if (onReboot) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   278
            msg << "Move";
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   279
        } else {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   280
            msg << "Moved";
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   281
        }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   282
        msg << " '" << fromPath << "' " << label << " to '" << toPath << "'";
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   283
    } else {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   284
        if (onReboot) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   285
            msg << "Delete";
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   286
        } else {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   287
            msg << "Deleted";
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   288
        }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   289
        msg << " '" << fromPath << "' " << label;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   290
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   291
    if (onReboot) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   292
        msg << " on reboot";
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   293
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   294
    LOG_TRACE(msg);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   295
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   296
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   297
} // namespace
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   298
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   299
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   300
void moveFile(const tstring& fromPath, const tstring& toPath,
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   301
        bool failIfExists) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   302
    createDirectory(dirname(toPath));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   303
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   304
    DWORD flags = MOVEFILE_COPY_ALLOWED;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   305
    if (!failIfExists) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   306
        flags |= MOVEFILE_REPLACE_EXISTING;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   307
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   308
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   309
    moveFileImpl(fromPath, toPath, flags);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   310
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   311
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   312
void deleteFile(const tstring &path)
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   313
{
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   314
    if (!deleteFile(path, std::nothrow)) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   315
        JP_THROW(SysError(tstrings::any()
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   316
                << "DeleteFile(" << path << ") failed", DeleteFile));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   317
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   318
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   319
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   320
namespace {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   321
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   322
bool notFound(const DWORD status=GetLastError()) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   323
    return status == ERROR_FILE_NOT_FOUND || status == ERROR_PATH_NOT_FOUND;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   324
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   325
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   326
bool deleteFileImpl(const std::nothrow_t &, const tstring &path) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   327
    const bool deleted = (DeleteFile(path.c_str()) != 0);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   328
    if (deleted) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   329
        LOG_TRACE(tstrings::any() << "Deleted [" << path << "] file");
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   330
        return true;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   331
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   332
    return notFound();
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   333
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   334
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   335
} // namespace
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   336
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   337
bool deleteFile(const tstring &path, const std::nothrow_t &) throw()
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   338
{
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   339
    bool deleted = deleteFileImpl(std::nothrow, path);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   340
    const DWORD status = GetLastError();
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   341
    if (!deleted && status == ERROR_ACCESS_DENIED) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   342
        DWORD attrs = GetFileAttributes(path.c_str());
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   343
        SetLastError(status);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   344
        if (attrs == INVALID_FILE_ATTRIBUTES) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   345
            return false;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   346
        }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   347
        if (attrs & FILE_ATTRIBUTE_READONLY) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   348
            // DeleteFile() failed because file is R/O.
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   349
            // Remove R/O attribute and retry DeleteFile().
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   350
            attrs &= ~FILE_ATTRIBUTE_READONLY;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   351
            if (SetFileAttributes(path.c_str(), attrs)) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   352
                LOG_TRACE(tstrings::any() << "Discarded R/O attribute from ["
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   353
                                                        << path << "] file");
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   354
                deleted = deleteFileImpl(std::nothrow, path);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   355
            } else {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   356
                LOG_WARNING(SysError(tstrings::any()
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   357
                            << "Failed to discard R/O attribute from ["
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   358
                            << path << "] file. File will not be deleted",
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   359
                            SetFileAttributes).what());
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   360
                SetLastError(status);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   361
            }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   362
        }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   363
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   364
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   365
    return deleted || notFound();
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   366
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   367
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   368
void deleteDirectory(const tstring &path)
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   369
{
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   370
    if (!deleteDirectory(path, std::nothrow)) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   371
        JP_THROW(SysError(tstrings::any()
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   372
                << "RemoveDirectory(" << path << ") failed", RemoveDirectory));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   373
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   374
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   375
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   376
bool deleteDirectory(const tstring &path, const std::nothrow_t &) throw()
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   377
{
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   378
    const bool deleted = (RemoveDirectory(path.c_str()) != 0);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   379
    if (deleted) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   380
        LOG_TRACE(tstrings::any() << "Deleted [" << path << "] directory");
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   381
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   382
    return deleted || notFound();
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   383
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   384
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   385
namespace {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   386
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   387
class DeleteFilesCallback: public DirectoryCallback {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   388
public:
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   389
    explicit DeleteFilesCallback(bool ff): failfast(ff), failed(false) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   390
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   391
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   392
    virtual bool onFile(const tstring& path) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   393
        if (failfast) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   394
            deleteFile(path);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   395
        } else {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   396
            updateStatus(deleteFile(path, std::nothrow));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   397
        }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   398
        return true;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   399
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   400
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   401
    bool good() const {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   402
        return !failed;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   403
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   404
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   405
protected:
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   406
    void updateStatus(bool success) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   407
        if (!success) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   408
            failed = true;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   409
        }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   410
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   411
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   412
    const bool failfast;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   413
private:
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   414
    bool failed;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   415
};
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   416
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   417
class DeleteAllCallback: public DeleteFilesCallback {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   418
public:
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   419
    explicit DeleteAllCallback(bool failfast): DeleteFilesCallback(failfast) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   420
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   421
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   422
    virtual bool onDirectory(const tstring& path) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   423
        if (failfast) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   424
            deleteDirectoryRecursive(path);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   425
        } else {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   426
            updateStatus(deleteDirectoryRecursive(path, std::nothrow));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   427
        }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   428
        return true;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   429
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   430
};
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   431
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   432
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   433
class BatchDeleter {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   434
    const tstring dirPath;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   435
    bool recursive;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   436
public:
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   437
    explicit BatchDeleter(const tstring& path): dirPath(path) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   438
        deleteSubdirs(false);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   439
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   440
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   441
    BatchDeleter& deleteSubdirs(bool v) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   442
        recursive = v;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   443
        return *this;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   444
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   445
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   446
    void execute() const {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   447
        if (!isFileExists(dirPath)) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   448
            return;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   449
        }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   450
        iterateDirectory(true /* fail fast */);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   451
        if (recursive) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   452
            deleteDirectory(dirPath);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   453
        }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   454
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   455
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   456
    bool execute(const std::nothrow_t&) const {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   457
        if (!isFileExists(dirPath)) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   458
            return true;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   459
        }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   460
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   461
        if (!isDirectory(dirPath)) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   462
            return false;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   463
        }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   464
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   465
        JP_TRY;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   466
        if (!iterateDirectory(false /* ignore errors */)) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   467
            return false;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   468
        }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   469
        if (recursive) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   470
            return deleteDirectory(dirPath, std::nothrow);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   471
        }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   472
        return true;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   473
        JP_CATCH_ALL;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   474
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   475
        return false;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   476
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   477
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   478
private:
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   479
    bool iterateDirectory(bool failfast) const {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   480
        std::unique_ptr<DeleteFilesCallback> callback;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   481
        if (recursive) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   482
            callback = std::unique_ptr<DeleteFilesCallback>(
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   483
                                            new DeleteAllCallback(failfast));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   484
        } else {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   485
            callback = std::unique_ptr<DeleteFilesCallback>(
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   486
                                            new DeleteFilesCallback(failfast));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   487
        }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   488
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   489
        FileUtils::iterateDirectory(dirPath, *callback);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   490
        return callback->good();
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   491
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   492
};
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   493
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   494
} // namespace
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   495
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   496
void deleteFilesInDirectory(const tstring &dirPath) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   497
    BatchDeleter(dirPath).execute();
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   498
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   499
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   500
bool deleteFilesInDirectory(const tstring &dirPath,
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   501
                                            const std::nothrow_t &) throw() {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   502
    return BatchDeleter(dirPath).execute(std::nothrow);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   503
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   504
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   505
void deleteDirectoryRecursive(const tstring &dirPath) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   506
    BatchDeleter(dirPath).deleteSubdirs(true).execute();
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   507
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   508
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   509
bool deleteDirectoryRecursive(const tstring &dirPath,
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   510
                                            const std::nothrow_t &) throw() {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   511
    return BatchDeleter(dirPath).deleteSubdirs(true).execute(std::nothrow);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   512
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   513
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   514
namespace {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   515
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   516
struct FindFileDeleter {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   517
    typedef HANDLE pointer;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   518
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   519
    void operator()(HANDLE h) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   520
        if (h && h != INVALID_HANDLE_VALUE) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   521
            FindClose(h);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   522
        }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   523
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   524
};
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   525
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   526
typedef std::unique_ptr<HANDLE, FindFileDeleter> UniqueFindFileHandle;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   527
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   528
}; // namesace
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   529
void iterateDirectory(const tstring &dirPath, DirectoryCallback& callback)
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   530
{
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   531
    const tstring searchString = combinePath(dirPath, _T("*"));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   532
    WIN32_FIND_DATA findData;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   533
    UniqueFindFileHandle h(FindFirstFile(searchString.c_str(), &findData));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   534
    if (h.get() == INVALID_HANDLE_VALUE) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   535
        // GetLastError() == ERROR_FILE_NOT_FOUND is OK
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   536
        // - no files in the directory
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   537
        // ERROR_PATH_NOT_FOUND is returned
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   538
        // if the parent directory does not exist
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   539
        if (GetLastError() != ERROR_FILE_NOT_FOUND) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   540
            JP_THROW(SysError(tstrings::any() << "FindFirstFile("
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   541
                    << dirPath << ") failed", FindFirstFile));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   542
        }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   543
        return;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   544
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   545
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   546
    do {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   547
        const tstring fname(findData.cFileName);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   548
        const tstring filePath = combinePath(dirPath, fname);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   549
        if (!isDirectoryAttrs(findData.dwFileAttributes)) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   550
            if (!callback.onFile(filePath)) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   551
                return;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   552
            }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   553
        } else if (fname != _T(".") && fname != _T("..")) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   554
            if (!callback.onDirectory(filePath)) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   555
                return;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   556
            }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   557
        }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   558
    } while (FindNextFile(h.get(), &findData));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   559
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   560
    // expect GetLastError() == ERROR_NO_MORE_FILES
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   561
    if (GetLastError() != ERROR_NO_MORE_FILES) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   562
        JP_THROW(SysError(tstrings::any() << "FindNextFile("
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   563
                << dirPath << ") failed", FindNextFile));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   564
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   565
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   566
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   567
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   568
tstring replaceSuffix(const tstring& path, const tstring& newSuffix) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   569
    return (path.substr(0, path.size() - suffix(path).size()) + newSuffix);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   570
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   571
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   572
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   573
DirectoryIterator& DirectoryIterator::findItems(tstring_array& v) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   574
    if (!isDirectory(root)) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   575
        return *this;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   576
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   577
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   578
    iterateDirectory(root, *this);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   579
    v.insert(v.end(), items.begin(), items.end());
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   580
    items = tstring_array();
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   581
    return *this;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   582
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   583
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   584
bool DirectoryIterator::onFile(const tstring& path) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   585
    if (theWithFiles) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   586
        items.push_back(path);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   587
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   588
    return true;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   589
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   590
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   591
bool DirectoryIterator::onDirectory(const tstring& path) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   592
    if (theWithFolders) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   593
        items.push_back(path);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   594
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   595
    if (theRecurse) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   596
        DirectoryIterator(path).recurse(theRecurse)
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   597
                .withFiles(theWithFiles)
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   598
                .withFolders(theWithFolders)
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   599
                .findItems(items);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   600
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   601
    return true;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   602
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   603
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   604
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   605
namespace {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   606
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   607
struct DeleterFunctor {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   608
    // Order of items in the following enum is important!
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   609
    // It controls order in which items of particular type will be deleted.
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   610
    // See Deleter::execute().
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   611
    enum {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   612
        File,
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   613
        FilesInDirectory,
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   614
        RecursiveDirectory,
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   615
        EmptyDirectory
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   616
    };
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   617
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   618
    void operator () (const Deleter::Path& path) const {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   619
        switch (path.second) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   620
#define DELETE_SOME(o, f)\
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   621
        case o:\
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   622
            f(path.first, std::nothrow);\
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   623
            break
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   624
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   625
        DELETE_SOME(File, deleteFile);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   626
        DELETE_SOME(EmptyDirectory, deleteDirectory);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   627
        DELETE_SOME(FilesInDirectory, deleteFilesInDirectory);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   628
        DELETE_SOME(RecursiveDirectory, deleteDirectoryRecursive);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   629
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   630
#undef DELETE_SOME
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   631
        default:
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   632
            break;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   633
        }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   634
    }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   635
};
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   636
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   637
} // namespace
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   638
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   639
void Deleter::execute() {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   640
    Paths tmp;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   641
    tmp.swap(paths);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   642
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   643
    // Reorder items to delete.
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   644
    std::stable_sort(tmp.begin(), tmp.end(), [] (const Paths::value_type& a,
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   645
                                                const Paths::value_type& b) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   646
        return a.second < b.second;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   647
    });
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   648
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   649
    std::for_each(tmp.begin(), tmp.end(), DeleterFunctor());
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   650
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   651
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   652
Deleter& Deleter::appendFile(const tstring& path) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   653
    paths.push_back(std::make_pair(path, DeleterFunctor::File));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   654
    return *this;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   655
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   656
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   657
Deleter& Deleter::appendEmptyDirectory(const Directory& dir) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   658
     tstring path =  normalizePath(removeTrailingSlash(dir));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   659
     const tstring parent = normalizePath(removeTrailingSlash(dir.parent));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   660
     while(parent != path) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   661
         appendEmptyDirectory(path);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   662
         path = dirname(path);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   663
     }
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   664
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   665
    return *this;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   666
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   667
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   668
Deleter& Deleter::appendEmptyDirectory(const tstring& path) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   669
    paths.push_back(std::make_pair(path, DeleterFunctor::EmptyDirectory));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   670
    return *this;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   671
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   672
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   673
Deleter& Deleter::appendAllFilesInDirectory(const tstring& path) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   674
    paths.push_back(std::make_pair(path, DeleterFunctor::FilesInDirectory));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   675
    return *this;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   676
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   677
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   678
Deleter& Deleter::appendRecursiveDirectory(const tstring& path) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   679
    paths.push_back(std::make_pair(path, DeleterFunctor::RecursiveDirectory));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   680
    return *this;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   681
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   682
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   683
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   684
FileWriter::FileWriter(const tstring& path): dstPath(path) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   685
    tmpFile = FileUtils::createTempFile(_T("jds"), _T(".tmp"),
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   686
            FileUtils::dirname(path));
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   687
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   688
    cleaner.appendFile(tmpFile);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   689
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   690
    // we want to get exception on error
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   691
    tmp.exceptions(std::ifstream::failbit | std::ifstream::badbit);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   692
    tmp.open(tmpFile, std::ios::binary | std::ios::trunc);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   693
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   694
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   695
FileWriter& FileWriter::write(const void* buf, size_t bytes) {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   696
    tmp.write(static_cast<const char*>(buf), bytes);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   697
    return *this;
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   698
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   699
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   700
void FileWriter::finalize() {
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   701
    tmp.close();
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   702
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   703
    FileUtils::moveFile(tmpFile, dstPath, false);
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   704
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   705
    // cancel file deletion
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   706
    cleaner.cancel();
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   707
}
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   708
c7de06ed4b54 8229979: jpackage cleanup src files, help text, and javadoc
herrick
parents: 57413
diff changeset
   709
} //  namespace FileUtils