Example #1
0
bool ConfigFile::locate()
{
    // temporary configuration

    llvm::SmallString<128> p;
    const char* filename = "ldc2.conf";

#define APPEND_FILENAME_AND_RETURN_IF_EXISTS \
    { \
        sys::path::append(p, filename); \
        if (sys::fs::exists(p.str())) \
        { \
            pathstr = p.str(); \
            return true; \
        } \
    }

    // try the current working dir
    if (!sys::fs::current_path(p))
        APPEND_FILENAME_AND_RETURN_IF_EXISTS

    // try next to the executable
    p = exe_path::getBinDir();
    APPEND_FILENAME_AND_RETURN_IF_EXISTS

    // user configuration

    // try ~/.ldc
    p = getUserHomeDirectory();
    sys::path::append(p, ".ldc");
    APPEND_FILENAME_AND_RETURN_IF_EXISTS

#if _WIN32
    // try home dir
    p = getUserHomeDirectory();
    APPEND_FILENAME_AND_RETURN_IF_EXISTS
#endif

    // system configuration

    // try in etc relative to the executable: exe\..\etc
    // do not use .. in path because of security risks
    p = exe_path::getBaseDir();
    if (!p.empty())
    {
        sys::path::append(p, "etc");
        APPEND_FILENAME_AND_RETURN_IF_EXISTS
    }
Example #2
0
File: file.c Project: inilabs/caer
static bool caerOutputFileInit(caerModuleData moduleData) {
	// First, always create all needed setting nodes, set their default values
	// and add their listeners.
	char *userHomeDir = getUserHomeDirectory(moduleData);
	if (userHomeDir == NULL) {
		// caerModuleLog() called inside getUserHomeDirectory().
		return (false);
	}

	sshsNodeCreateString(moduleData->moduleNode, "directory", userHomeDir, 1, (PATH_MAX - MAX_PREFIX_LENGTH),
		SSHS_FLAGS_NORMAL, "Directory to write output data files in.");
	free(userHomeDir);

	// Support file-chooser in GUI, select any directory.
	sshsNodeCreateAttributeFileChooser(moduleData->moduleNode, "directory", "DIRECTORY");

	sshsNodeCreateString(moduleData->moduleNode, "prefix", DEFAULT_PREFIX, 1, MAX_PREFIX_LENGTH, SSHS_FLAGS_NORMAL,
		"Output data files name prefix.");

	// Generate current file name and open it.
	char *directory = sshsNodeGetString(moduleData->moduleNode, "directory");
	char *prefix    = sshsNodeGetString(moduleData->moduleNode, "prefix");

	char *filePath = getFullFilePath(moduleData, directory, prefix);
	free(directory);
	free(prefix);

	if (filePath == NULL) {
		// caerModuleLog() called inside getFullFilePath().
		return (false);
	}

	int fileFd = open(filePath, O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR | S_IRGRP);
	if (fileFd < 0) {
		caerModuleLog(moduleData, CAER_LOG_CRITICAL,
			"Could not create or open output file '%s' for writing. Error: %d.", filePath, errno);
		free(filePath);

		return (false);
	}

	caerModuleLog(moduleData, CAER_LOG_INFO, "Opened output file '%s' successfully for writing.", filePath);
	free(filePath);

	if (!caerOutputCommonInit(moduleData, fileFd, NULL)) {
		close(fileFd);

		return (false);
	}

	return (true);
}
/**
 * Internal implementation to set the default machine folder. Gets called
 * from the public attribute setter as well as loadSettings(). With 4.0,
 * the "default default" machine folder has changed, and we now require
 * a full path always.
 * @param aPath
 * @return
 */
HRESULT SystemProperties::setDefaultMachineFolder(const Utf8Str &strPath)
{
    Utf8Str path(strPath);      // make modifiable
    if (    path.isEmpty()          // used by API calls to reset the default
         || path == "Machines"      // this value (exactly like this, without path) is stored
                                    // in VirtualBox.xml if user upgrades from before 4.0 and
                                    // has not changed the default machine folder
       )
    {
        // new default with VirtualBox 4.0: "$HOME/VirtualBox VMs"
        HRESULT rc = getUserHomeDirectory(path);
        if (FAILED(rc)) return rc;
        path += RTPATH_SLASH_STR "VirtualBox VMs";
    }

    if (!RTPathStartsWithRoot(path.c_str()))
        return setError(E_INVALIDARG,
                        tr("Given default machine folder '%s' is not fully qualified"),
                        path.c_str());

    m->strDefaultMachineFolder = path;

    return S_OK;
}
Example #4
0
bool ConfigFile::locate(llvm::SmallString<128> &p, const char* argv0, void* mainAddr, const char* filename)
{
    // temporary configuration

    // try the current working dir
    if (!sys::fs::current_path(p))
    {
        sys::path::append(p, filename);
        if (sys::fs::exists(p.str()))
            return true;
    }

    // try next to the executable
    p = getMainExecutable(argv0, mainAddr);
    sys::path::remove_filename(p);
    sys::path::append(p, filename);
    if (sys::fs::exists(p.str()))
        return true;

    // user configuration

    // try ~/.ldc
    p = getUserHomeDirectory();
    sys::path::append(p, ".ldc");
    sys::path::append(p, filename);
    if (sys::fs::exists(p.str()))
        return true;

#if _WIN32
    // try home dir
    p = getUserHomeDirectory();
    sys::path::append(p, filename);
    if (sys::fs::exists(p.str()))
        return true;
#endif

    // system configuration

    // try in etc relative to the executable: exe\..\etc
    // do not use .. in path because of security risks
    p = getMainExecutable(argv0, mainAddr);
    sys::path::remove_filename(p);
    sys::path::remove_filename(p);
    if (!p.empty())
    {
        sys::path::append(p, "etc");
        sys::path::append(p, filename);
        if (sys::fs::exists(p.str()))
            return true;
    }

#if _WIN32
    // Try reading path from registry
    if (ReadPathFromRegistry(p))
    {
        sys::path::append(p, "etc");
        sys::path::append(p, filename);
        if (sys::fs::exists(p.str()))
            return true;
    }
#else
    // try the install-prefix/etc
    p = LDC_INSTALL_PREFIX;
    sys::path::append(p, "etc");
    sys::path::append(p, filename);
    if (sys::fs::exists(p.str()))
        return true;

    // try the install-prefix/etc/ldc
    p = LDC_INSTALL_PREFIX;
    sys::path::append(p, "etc");
    sys::path::append(p, "ldc");
    sys::path::append(p, filename);
    if (sys::fs::exists(p.str()))
        return true;

    // try /etc (absolute path)
    p = "/etc";
    sys::path::append(p, filename);
    if (sys::fs::exists(p.str()))
        return true;

    // try /etc/ldc (absolute path)
    p = "/etc/ldc";
    sys::path::append(p, filename);
    if (sys::fs::exists(p.str()))
        return true;
#endif

    return false;
}
Example #5
0
int main(int argc, char *argv[]) {
    // First of all, parse the IP:Port we need to connect to.
    // Those are for now also the only two parameters permitted.
    // If none passed, attempt to connect to default IP:Port.
    const char *ipAddress = "127.0.0.1";
    uint16_t portNumber = 4040;

    if (argc != 1 && argc != 3) {
        fprintf(stderr, "Incorrect argument number. Either pass none for default IP:Port"
                "combination of 127.0.0.1:4040, or pass the IP followed by the Port.\n");
        return (EXIT_FAILURE);
    }

    // If explicitly passed, parse arguments.
    if (argc == 3) {
        ipAddress = argv[1];
        sscanf(argv[2], "%" SCNu16, &portNumber);
    }

    // Connect to the remote cAER config server.
    sockFd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sockFd < 0) {
        fprintf(stderr, "Failed to create TCP socket.\n");
        return (EXIT_FAILURE);
    }

    struct sockaddr_in configServerAddress;
    memset(&configServerAddress, 0, sizeof(struct sockaddr_in));

    configServerAddress.sin_family = AF_INET;
    configServerAddress.sin_port = htons(portNumber);
    inet_aton(ipAddress, &configServerAddress.sin_addr); // htonl() is implicit here.

    if (connect(sockFd, (struct sockaddr *) &configServerAddress, sizeof(struct sockaddr_in)) < 0) {
        fprintf(stderr, "Failed to connect to remote config server.\n");
        return (EXIT_FAILURE);
    }

    // Create a shell prompt with the IP:Port displayed.
    size_t shellPromptLength = (size_t) snprintf(NULL, 0, "cAER @ %s:%" PRIu16 " >> ", ipAddress, portNumber);
    char shellPrompt[shellPromptLength + 1]; // +1 for terminating NUL byte.
    snprintf(shellPrompt, shellPromptLength + 1, "cAER @ %s:%" PRIu16 " >> ", ipAddress, portNumber);

    // Set our own command completion function.
    linenoiseSetCompletionCallback(&handleCommandCompletion);

    // Generate command history file path (in user home).
    char commandHistoryFilePath[1024];

    char *userHomeDir = getUserHomeDirectory();
    snprintf(commandHistoryFilePath, 1024, "%s/.caerctl_history", userHomeDir);
    free(userHomeDir);

    // Load command history file.
    linenoiseHistoryLoad(commandHistoryFilePath);

    while (true) {
        // Display prompt and read input (NOTE: remember to free input after use!).
        char *inputLine = linenoise(shellPrompt);

        // Check for EOF first.
        if (inputLine == NULL) {
            // Exit loop.
            break;
        }

        // Add input to command history.
        linenoiseHistoryAdd(inputLine);

        // Then, after having added to history, check for termination commands.
        if (strncmp(inputLine, "quit", 4) == 0 || strncmp(inputLine, "exit", 4) == 0) {
            // Exit loop, free memory.
            free(inputLine);
            break;
        }

        // Try to generate a request, if there's any content.
        size_t inputLineLength = strlen(inputLine);

        if (inputLineLength > 0) {
            handleInputLine(inputLine, inputLineLength);
        }

        // Free input after use.
        free(inputLine);
    }

    // Close connection.
    close(sockFd);

    // Save command history file.
    linenoiseHistorySave(commandHistoryFilePath);

    return (EXIT_SUCCESS);
}