int col_push_str_property(struct collection_item *stack,
                          const char *property, const char *string, int length)
{
    int error = EOK;

    TRACE_FLOW_STRING("col_push_str_property", "Entry point.");

    /* Check that stack is not empty */
    if (stack == NULL) {
        TRACE_ERROR_STRING("Stack can't be NULL", "");
        return EINVAL;
    }

    /* Make sure it is a stack */
    if (!col_is_of_class(stack, COL_CLASS_STACK)) {
        TRACE_ERROR_STRING("Wrong class", "");
        return EINVAL;
    }

    error = col_add_str_property(stack, NULL, property, string, length);

    TRACE_FLOW_STRING("col_push_str_property", "Exit.");
    return error;
}
Exemple #2
0
/* Collect metadata for the file */
int collect_metadata(uint32_t metaflags,
                     struct collection_item **metadata,
                     FILE *config_file,
                     const char *config_filename)
{
    int error = EOK;
    struct collection_item *metasec = NULL;
    int filedes;
    struct stat file_stats;
    char buff[CONVERSION_BUFFER];

    TRACE_FLOW_STRING("collect_metadata", "Entry");
    /* Check and create section for file error if needed */
    if (metaflags & INI_META_SEC_ACCESS_FLAG) {
        /* Create ACCESS collection */
        error = col_create_collection(&metasec,
                                      INI_META_SEC_ACCESS,
                                      COL_CLASS_INI_SECTION);
        if (error) {
            TRACE_ERROR_NUMBER("Failed to create access section.", error);
            col_destroy_collection(metasec);
            return error;
        }

        filedes = fileno(config_file);

        /* Collect statistics */
        errno = 0;
        if (fstat(filedes, &file_stats) < 0) {
            error = errno;
            TRACE_ERROR_NUMBER("Failed to get statistics.", error);
            col_destroy_collection(metasec);
            return error;
        }

        /* Record statistics */
        /* UID */
        snprintf(buff, CONVERSION_BUFFER, "%lu",
                 (unsigned long)file_stats.st_uid);
        error = col_add_str_property(metasec,
                                     NULL,
                                     INI_META_KEY_UID,
                                     buff,
                                     0);
        if (error) {
            TRACE_ERROR_NUMBER("Failed to save uid", error);
            col_destroy_collection(metasec);
            return error;
        }

        /* GID */
        snprintf(buff, CONVERSION_BUFFER, "%lu",
                 (unsigned long)file_stats.st_gid);
        error = col_add_str_property(metasec,
                                     NULL,
                                     INI_META_KEY_GID,
                                     buff,
                                     0);
        if (error) {
            TRACE_ERROR_NUMBER("Failed to save gid", error);
            col_destroy_collection(metasec);
            return error;
        }

        /* PERMISSIONS */
        snprintf(buff, CONVERSION_BUFFER, "%lu",
                 (unsigned long)file_stats.st_mode);
        error = col_add_str_property(metasec,
                                     NULL,
                                     INI_META_KEY_PERM,
                                     buff,
                                     0);
        if (error) {
            TRACE_ERROR_NUMBER("Failed to save permissions", error);
            col_destroy_collection(metasec);
            return error;
        }

        /* Modification time stamp */
        snprintf(buff, CONVERSION_BUFFER, "%ld",
                 (long int)file_stats.st_mtime);
        error = col_add_str_property(metasec,
                                     NULL,
                                     INI_META_KEY_MODIFIED,
                                     buff,
                                     0);
        if (error) {
            TRACE_ERROR_NUMBER("Failed to save modification time", error);
            col_destroy_collection(metasec);
            return error;
        }

        /* Name */
        error = col_add_str_property(metasec,
                                     NULL,
                                     INI_META_KEY_NAME,
                                     config_filename,
                                     0);
        if (error) {
            TRACE_ERROR_NUMBER("Failed to save file name", error);
            col_destroy_collection(metasec);
            return error;
        }

        /* The device ID can actualy be bigger than
         * 32-bits according to the type sizes.
         * However it is probaly not going to happen
         * on a real system.
         * Add a check for this case.
         */
        if (file_stats.st_dev > ULONG_MAX) {
            TRACE_ERROR_NUMBER("Device is out of range", ERANGE);
            col_destroy_collection(metasec);
            return ERANGE;
        }

        /* Device  ID */
        TRACE_INFO_LNUMBER("Device ID", file_stats.st_dev);

        snprintf(buff, CONVERSION_BUFFER, "%lu",
                 (unsigned long)file_stats.st_dev);
        error = col_add_str_property(metasec,
                                     NULL,
                                     INI_META_KEY_DEV,
                                     buff,
                                     0);
        if (error) {
            TRACE_ERROR_NUMBER("Failed to save inode", error);
            col_destroy_collection(metasec);
            return error;
        }

        /* i-node */
        snprintf(buff, CONVERSION_BUFFER, "%lu",
                (unsigned long)file_stats.st_ino);
        error = col_add_str_property(metasec,
                                     NULL,
                                     INI_META_KEY_INODE,
                                     buff,
                                     0);
        if (error) {
            TRACE_ERROR_NUMBER("Failed to save inode", error);
            col_destroy_collection(metasec);
            return error;
        }

        /* Add section to metadata */
        error = col_add_collection_to_collection(
                                    *metadata,
                                    NULL,
                                    NULL,
                                    metasec,
                                    COL_ADD_MODE_REFERENCE);

        col_destroy_collection(metasec);

        if (error) {
            TRACE_ERROR_NUMBER("Failed to save file name", error);
            return error;
        }
    }

    TRACE_FLOW_STRING("collect_metadata", "Exit");
    return error;
}