Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 147 additions & 33 deletions fileUploadRandom.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh

Check failure on line 1 in fileUploadRandom.sh

View workflow job for this annotation

GitHub Actions / call-fossid-workflow / Fossid Annotate PR

FossID License Issue Detected

Source code with 'Apache-2.0' license found in local file 'fileUploadRandom.sh' (Match: rdkb/components/opensource/ccsp/sysint/rdkb/components/opensource/ccsp/sysint/1, 319 lines, url: https://code.rdkcentral.com/r/plugins/gitiles/rdkb/components/opensource/ccsp/sysint/+archive/RDKB-RELEASE-TEST-DUNFELL-1.tar.gz, file: fileUploadRandom.sh)
##########################################################################
# If not stated otherwise in this file or this component's Licenses.txt
# file the following copyright and licenses apply:
Expand Down Expand Up @@ -34,7 +34,17 @@
SELFHEAL_PATH="/usr/ccsp/tad"
CERT_CHECKER_PATH="/lib/rdk"

calcRandTimeandUpload()
CRON_MODE=0
FILEUPLOAD_TMP_DIR="/tmp/.fileupload_random"
RANDOM_DELAY_FILE="$FILEUPLOAD_TMP_DIR/.remaining_secs"
TICK_FILE="$FILEUPLOAD_TMP_DIR/.tick"
CRON_INSTALLED_FLAG="$FILEUPLOAD_TMP_DIR/.cron_installed"

if [ ! -d "$FILEUPLOAD_TMP_DIR" ]; then
mkdir -p "$FILEUPLOAD_TMP_DIR"
fi

generate_random_delay()
{
rand_hr=0
rand_min=0
Expand All @@ -45,15 +55,71 @@

# Calculate random second
rand_sec=`awk -v min=0 -v max=59 -v seed="$(date +%N)" 'BEGIN{srand(seed);print int(min+rand()*(max-min+1))}'`

# Calculate random hour
rand_hr=`awk -v min=0 -v max=2 -v seed="$(date +%N)" 'BEGIN{srand(seed);print int(min+rand()*(max-min+1))}'`

echo_t "RDK Logger : Random Time Generated : $rand_hr hr $rand_min min $rand_sec sec"
echo_t "RDK Logger : Random Time Generated : $rand_hr hr $rand_min min $rand_sec sec" >&2

min_to_sleep=$(($rand_hr*60 + $rand_min))
sec_to_sleep=$(($min_to_sleep*60 + $rand_sec))
sleep $sec_to_sleep;

echo "$sec_to_sleep"
}

calcRandTimeandUpload()
{
delay_completed=0
if [ "$CRON_MODE" = "1" ]; then
if [ ! -f "$RANDOM_DELAY_FILE" ]; then
sec_to_sleep=$(generate_random_delay)
echo "$sec_to_sleep" > "$RANDOM_DELAY_FILE"
echo_t "fileupload_random: Initial random delay stored: $sec_to_sleep seconds" >&2
else
echo_t "fileupload_random: Random delay already generated, reusing existing value" >&2
fi

if [ -f "$TICK_FILE" ]; then
current_tick=$(cat "$TICK_FILE")
else
current_tick=0
fi

if [ "$current_tick" = "0" ]; then
remaining=$(cat "$RANDOM_DELAY_FILE" 2>/dev/null)
[ -z "$remaining" ] && remaining=0

if [ "$remaining" -le 300 ]; then
echo_t "fileupload_random: Sleeping $remaining seconds NOW" >&2
[ "$remaining" -gt 0 ] && sleep "$remaining"
rm -f "$RANDOM_DELAY_FILE" "$TICK_FILE"
delay_completed=1
else
echo_t "fileupload_random: Remaining delay before upload: $remaining seconds" >&2
new_remaining=$((remaining - 300))
if [ "$new_remaining" -lt 0 ]; then
new_remaining=0
fi
echo $new_remaining > "$RANDOM_DELAY_FILE"
echo_t "fileupload_random: Updated remaining delay to $new_remaining seconds, exiting" >&2
fi
else
echo_t "fileupload_random: Skipping countdown this minute (tick=$current_tick/4)" >&2
fi

new_tick=$(( (current_tick + 1) % 5 ))
echo "$new_tick" > "$TICK_FILE"
Comment on lines +88 to +111
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same tick-based countdown logic issue exists here. The tick counter increments from 0 to 4, but countdown only happens when current_tick is 0, meaning the delay countdown progresses once every 5 minutes instead of every minute. For a random delay of up to 10800 seconds (3 hours), this would make the actual delay up to 5x longer than intended.

Copilot uses AI. Check for mistakes.

if [ "$delay_completed" != "1" ]; then
return 0
fi
fi
Comment on lines +73 to +116
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent indentation with tabs. Lines 73, 81, 92-93, and others use tabs while the surrounding code uses spaces.

Copilot uses AI. Check for mistakes.

if [ "$CRON_MODE" != "1" ]; then
sec_to_sleep=$(generate_random_delay)
echo_t "fileupload_random: Sleeping for $sec_to_sleep seconds" >&2
sleep "$sec_to_sleep"
fi

if [ -f "$MAINTENANCEWINDOW" ]
then
Expand Down Expand Up @@ -226,37 +292,85 @@
fi
}

check_maintenance_window_upload()
{
upload_logfile=1
if [ "$UTC_ENABLE" == "true" ]; then
cur_hr=`LTime H | tr -dc '0-9'`
cur_min=`LTime M | tr -dc '0-9'`
else
cur_hr=`date +"%H"`
cur_min=`date +"%M"`
fi

if [ "$cur_hr" -ge "02" ] && [ "$cur_hr" -le "05" ]; then
if [ "$cur_hr" = "05" ] && [ "$cur_min" != "00" ]; then
upload_logfile=1
else
if [ "$upload_logfile" = "1" ]; then
calcRandTimeandUpload
fi
fi
else
upload_logfile=1
fi
}
Comment on lines +295 to +317
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check_maintenance_window_upload function sets upload_logfile but never uses it outside of the function (lines 297, 308, 310, 315). The function either calls calcRandTimeandUpload (which performs the upload) or does nothing. When the current time is not in the maintenance window (lines 314-316), upload_logfile is set to 1 but no action is taken - the upload doesn't happen. This appears to be a logic error. The function should either perform the upload when outside the maintenance window, or the variable should be returned/used by the caller to determine whether to proceed with the upload.

Copilot uses AI. Check for mistakes.

BUILD_TYPE=`getBuildType`
SERVER=`getTFTPServer $BUILD_TYPE`
loop=1
upload_logfile=1
while [ $loop -eq 1 ]
do
sleep 60

if [ "$UTC_ENABLE" == "true" ]
then
cur_hr=`LTime H | tr -dc '0-9'`
cur_min=`LTime M | tr -dc '0-9'`
else
cur_hr=`date +"%H"`
cur_min=`date +"%M"`
fi
install_cron_entry() {
CRON_LINE="* * * * * /rdklogger/fileUploadRandom.sh"

if crontab -l 2>/dev/null | grep -q "fileUploadRandom.sh"; then
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cron entry check uses a simple grep which could match partial strings. Consider using a more precise pattern match to avoid false positives.

Suggested change
if crontab -l 2>/dev/null | grep -q "fileUploadRandom.sh"; then
if crontab -l 2>/dev/null | grep -qE '^[^#]*[[:space:]]/rdklogger/fileUpload\.sh([[:space:]]|$)'; then

Copilot uses AI. Check for mistakes.
echo_t "fileUploadRandom.sh - Cron entry already present"
return 0
fi

if [ "$cur_hr" -ge "02" ] && [ "$cur_hr" -le "05" ]
then
if [ "$cur_hr" = "05" ] && [ "$cur_min" != "00" ]
then
upload_logfile=1
else
if [ "$upload_logfile" = "1" ]
then
calcRandTimeandUpload
fi
fi
else
upload_logfile=1
fi
done
(crontab -l 2>/dev/null; echo "$CRON_LINE") | crontab -
rc=$?

if [ $rc -eq 0 ]; then
echo_t "fileUploadRandom.sh - Cron installed cleanly: $CRON_LINE"
else
echo_t "fileUploadRandom.sh - Cron install failed (rc=$rc)"
fi
}

service_mode() {
echo_t "fileUploadRandom.sh - Running in SERVICE mode"

while [ 1 ];
do
sleep 60
CRON_MODE=0
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In service mode, CRON_MODE is set to 0 inside the while loop (line 346), but this is unnecessary since CRON_MODE is already set to 0 at line 374 before calling service_mode. Setting it repeatedly inside the loop is redundant and could be misleading. Consider removing this line or moving it outside the loop.

Suggested change
CRON_MODE=0

Copilot uses AI. Check for mistakes.
check_maintenance_window_upload
done
}

rdklogger_cron_enable=`syscfg get RdkbLogCronEnable`

if [ "$rdklogger_cron_enable" = "true" ]; then
echo_t "fileUploadRandom.sh - Running in CRON mode"
CRON_MODE=1

if [ ! -d "$FILEUPLOAD_TMP_DIR" ]; then
mkdir -p "$FILEUPLOAD_TMP_DIR"
fi

if [ ! -f "$CRON_INSTALLED_FLAG" ]; then
install_cron_entry
touch "$CRON_INSTALLED_FLAG"
fi

if [ -f "$RANDOM_DELAY_FILE" ]; then
calcRandTimeandUpload
else
check_maintenance_window_upload
fi

exit 0
else
CRON_MODE=0
service_mode
fi
Loading
Loading