1b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael/*
2b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael * Copyright (C) 2012 The Android Open Source Project
3b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael *
4b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael * Licensed under the Apache License, Version 2.0 (the "License");
5b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael * you may not use this file except in compliance with the License.
6b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael * You may obtain a copy of the License at
7b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael *
8b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael *      http://www.apache.org/licenses/LICENSE-2.0
9b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael *
10b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael * Unless required by applicable law or agreed to in writing, software
11b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael * distributed under the License is distributed on an "AS IS" BASIS,
12b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael * See the License for the specific language governing permissions and
14b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael * limitations under the License.
15b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael */
16b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael
17b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael/*
18b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael * "find_lock.exe", for Windows only.
19b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael */
20b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael
21b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael#ifdef _WIN32
22b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael
23b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael#include "utils.h"
24b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael#include "find_lock.h"
25b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael#include <io.h>
26b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael#include <fcntl.h>
27b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael
28b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphaelint main(int argc, char* argv[]) {
29b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael
30b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael    gIsConsole = true; // tell utils to to print errors to stderr
31b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael    gIsDebug = (getenv("ANDROID_SDKMAN_DEBUG") != NULL);
32b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael    CPath dirPath;
33b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael    bool doPrintUsage = false;
34b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael
35b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael    for (int i = 1; i < argc; i++) {
36b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael        if (strncmp(argv[i], "-d", 2) == 0) {
37b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael            gIsDebug = true;
38b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael
39b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael        } else if (dirPath.isEmpty()) {
40b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael            dirPath.set(argv[i]);
41b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael
42b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael        } else {
43b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael            doPrintUsage = true;
44b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael        }
45b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael    }
46b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael
47b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael    if (dirPath.isEmpty()) {
48b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael        fprintf(stderr, "Error: Missing directory path\n");
49b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael        doPrintUsage = true;
50b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael
51b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael    } else if (!dirPath.dirExists()) {
52b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael        fprintf(stderr, "Error: '%s' is not a valid directory.\n", dirPath.cstr());
53b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael        return 1;
54b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael    }
55b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael
56b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael    if (doPrintUsage) {
57b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael        printf(
58b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael            "Usage: find_lock.exe [options] sdk_directory_path\n"
59b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael            "\n"
60b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael            "Outputs the names of modules that are locking the given directory.\n"
61b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael            "Returns code 0 when found, 1 when not found.\n"
62b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael            "\n"
63b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael            "Options:\n"
64b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael            "-h / -help   : This help.\n"
65b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael            "-t / -test   : Internal test.\n"
66b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael            );
67b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael        return 2;
68b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael    }
69b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael
70b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael    CString result;
71b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael    if (findLock(dirPath, &result)) {
72b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael        fflush(stdout);
73b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael        fflush(stderr);
74b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael        printf("%s", result.cstr());
75b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael        return 0;
76b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael    }
77b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael    return 1;
78b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael}
79b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael
80b099a8ca899d1a3051555bcc2fdbffee8ca6b406Raphael#endif /* _WIN32 */
81