1#!/usr/bin/env bash
2
3# @brief Execute the command and save the output into the dreport
4#        packaging, if it is in the user allowed dump size limit.
5# @param $1 Command to be executed.
6# @param $2 Save file name.
7# @param $3 Plugin description used for logging.
8function add_cmd_output()
9{
10    command="$1"
11    file_name="$2"
12    desc="$3"
13
14    eval $command >> "$name_dir/$file_name"
15    if [ $? -ne 0 ]; then
16        log_error "Failed to collect $desc"
17        return 1
18    fi
19
20    if check_size "$name_dir/$file_name"; then
21        log_info "Collected $desc"
22    else
23        log_warning "Skipping $desc"
24    fi
25}
26
27# @brief Copy the file or directory into the dreport packaging,
28#        if it is in the user allowed dump size limit.
29# @param $1 Copy file or directory name.
30# @param $2 Plugin description used for logging.
31function add_copy_file()
32{
33    file_name="$1"
34    desc="$2"
35
36    cp -r $file_name $name_dir
37    if [ $? -ne 0 ]; then
38        log_error "Failed to copy $desc $file_name"
39        return $RESOURCE_UNAVAILABLE
40    fi
41    if check_size "$name_dir/$(basename "$file_name")"; then
42        log_info "Copied $desc $file_name"
43        return $SUCCESS
44    else
45        return $RESOURCE_UNAVAILABLE
46        log_warning "Skipping copy $desc $file_name"
47    fi
48}
49# @brief Copy the symbolic link file to the dreport packaging,
50#        if it is in the user allowed dump size limit.
51# @param $1 symbolic link file name
52# @param $2 Plugin description used for logging.
53function add_copy_sym_link_file()
54{
55    file_name="$1"
56    desc="$2"
57
58    cp $file_name $name_dir
59    if [ $? -ne 0 ]; then
60        log_error "Failed to copy $desc $file_name"
61        return $RESOURCE_UNAVAILABLE
62    fi
63    if check_size "$name_dir/$(basename "$file_name")"; then
64        log_info "Copied $desc $file_name"
65        return $SUCCESS
66    else
67        log_warning "Skipping copy $desc $file_name"
68        return $RESOURCE_UNAVAILABLE
69    fi
70}
71
72# @brief Calculate file or directory compressed size based on input
73#        and check whether the size in the allowed size limit.
74#        Remove the file or directory from the name_dir
75#        if the check fails.
76# @param $1 Source file or directory
77# @return 0 on success, error code if size exceeds the limit.
78# Limitation: compress and tar will have few bytes size difference
79function check_size()
80{
81    source=$1
82
83    #No size check required in case dump_size is set to unlimited
84    if [ $dump_size = $UNLIMITED ]; then
85        return 0
86    fi
87
88    #get the file or directory size
89    if [[ -d $source ]] && [[ -n $source ]]; then
90        tar -cf "$source.tar" -C \
91                 $(dirname "$source") $(basename "$source")
92        size=$(stat -c%s "$source.tar")
93        rm "$source.tar"
94    else
95        size=$(stat -c%s "$source")
96    fi
97
98    if [ $((size + cur_dump_size)) -gt $dump_size ]; then
99        #Exceed the allowed limit,
100        #tar and compress the files and check the size
101        tar -Jcf "$name_dir.tar.xz" -C \
102                  $(dirname "$name_dir") $(basename "$name_dir")
103        size=$(stat -c%s "$name_dir.tar.xz")
104        if [ $size -gt $dump_size ]; then
105            #Remove the the specific data from the name_dir and continue
106            rm "$source" "$name_dir.tar.xz"
107            return $RESOURCE_UNAVAILABLE
108        else
109            rm "$name_dir.tar.xz"
110        fi
111    fi
112
113    cur_dump_size=$((size + cur_dump_size))
114    return $SUCCESS
115}
116
117# @brief log the error message
118# @param error message
119function log_error()
120{
121   echo $($TIME_STAMP) "ERROR: $@" >> $dreport_log
122   if ((quiet != TRUE)); then
123      echo $($TIME_STAMP) "ERROR: $@" >&2
124   fi
125}
126
127# @brief log warning message
128# @param warning message
129function log_warning()
130{
131    if ((verbose == TRUE)); then
132        echo $($TIME_STAMP) "WARNING: $@" >> $dreport_log
133        if ((quiet != TRUE)); then
134            echo $($TIME_STAMP) "WARNING: $@" >&2
135        fi
136    fi
137}
138
139# @brief log info message
140# @param info message
141function log_info()
142{
143    if ((verbose == TRUE)); then
144        echo $($TIME_STAMP) "INFO: $@" >> $dreport_log
145        if ((quiet != TRUE)); then
146            echo $($TIME_STAMP) "INFO: $@" >&1
147        fi
148    fi
149}
150
151# @brief log summary message
152# @param message
153function log_summary()
154{
155    echo $($TIME_STAMP) "$@" >> $summary_log
156    if ((quiet != TRUE)); then
157        echo $($TIME_STAMP) "$@" >&1
158    fi
159}
160