openc3-cosmos-cfdp 2.0.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.md +18 -0
  3. data/README.md +103 -55
  4. data/Rakefile +12 -0
  5. data/lib/cfdp.py +564 -0
  6. data/lib/cfdp.rb +68 -14
  7. data/lib/cfdp_api.py +302 -0
  8. data/lib/cfdp_api.rb +55 -9
  9. data/microservices/CFDP/Gemfile +18 -4
  10. data/microservices/CFDP/Gemfile.dev +22 -8
  11. data/microservices/CFDP/Gemfile.prod +18 -4
  12. data/microservices/CFDP/Rakefile +13 -2
  13. data/microservices/CFDP/app/controllers/application_controller.rb +2 -2
  14. data/microservices/CFDP/app/controllers/cfdp_controller.rb +62 -31
  15. data/microservices/CFDP/app/models/cfdp_checksum.rb +12 -4
  16. data/microservices/CFDP/app/models/cfdp_crc_checksum.rb +10 -2
  17. data/microservices/CFDP/app/models/cfdp_mib.rb +173 -32
  18. data/microservices/CFDP/app/models/cfdp_null_checksum.rb +10 -2
  19. data/microservices/CFDP/app/models/cfdp_pdu.rb +96 -20
  20. data/microservices/CFDP/app/models/cfdp_receive_transaction.rb +139 -11
  21. data/microservices/CFDP/app/models/cfdp_source_transaction.rb +433 -93
  22. data/microservices/CFDP/app/models/cfdp_topic.rb +2 -2
  23. data/microservices/CFDP/app/models/cfdp_transaction.rb +174 -4
  24. data/microservices/CFDP/app/models/cfdp_user.rb +216 -73
  25. data/microservices/CFDP/config/routes.rb +1 -0
  26. data/microservices/CFDP/lib/cfdp_pdu/cfdp_pdu_ack.rb +7 -6
  27. data/microservices/CFDP/lib/cfdp_pdu/cfdp_pdu_eof.rb +1 -2
  28. data/microservices/CFDP/lib/cfdp_pdu/cfdp_pdu_file_data.rb +2 -25
  29. data/microservices/CFDP/lib/cfdp_pdu/cfdp_pdu_finished.rb +8 -7
  30. data/microservices/CFDP/lib/cfdp_pdu/cfdp_pdu_keep_alive.rb +7 -6
  31. data/microservices/CFDP/lib/cfdp_pdu/cfdp_pdu_metadata.rb +1 -2
  32. data/microservices/CFDP/lib/cfdp_pdu/cfdp_pdu_nak.rb +7 -6
  33. data/microservices/CFDP/lib/cfdp_pdu/cfdp_pdu_prompt.rb +1 -2
  34. data/microservices/CFDP/spec/controllers/cfdp_controller_spec.rb +226 -1
  35. data/microservices/CFDP/spec/models/cfdp_checksum_spec.rb +239 -0
  36. data/microservices/CFDP/spec/models/cfdp_crc_checksum_spec.rb +16 -13
  37. data/microservices/CFDP/spec/models/cfdp_mib_spec.rb +880 -138
  38. data/microservices/CFDP/spec/models/cfdp_null_checksum_spec.rb +111 -0
  39. data/microservices/CFDP/spec/models/cfdp_pdu_file_data_spec.rb +48 -0
  40. data/microservices/CFDP/spec/models/cfdp_pdu_nak_spec.rb +2 -2
  41. data/microservices/CFDP/spec/models/cfdp_pdu_spec.rb +129 -0
  42. data/microservices/CFDP/spec/models/cfdp_receive_transaction_spec.rb +109 -10
  43. data/microservices/CFDP/spec/models/cfdp_source_transaction_spec.rb +357 -0
  44. data/microservices/CFDP/spec/models/cfdp_transaction_spec.rb +282 -5
  45. data/microservices/CFDP/spec/models/cfdp_user_spec.rb +462 -20
  46. data/microservices/CFDP/spec/requests/cfdp_spec.rb +92 -43
  47. data/microservices/CFDP/spec/spec_helper.rb +34 -3
  48. data/plugin.txt +55 -4
  49. data/public/store_img.png +0 -0
  50. data/pyproject.toml +70 -0
  51. data/targets/CFDPTEST/procedures/cfdp_test_suite.py +179 -0
  52. data/targets/CFDPTEST/procedures/cfdp_test_suite.rb +2 -2
  53. data/targets/CFDPTEST/procedures/interrupt_test.rb +31 -0
  54. metadata +25 -12
  55. data/LICENSE.txt +0 -18
@@ -1,6 +1,6 @@
1
1
  # encoding: ascii-8bit
2
2
 
3
- # Copyright 2023 OpenC3, Inc.
3
+ # Copyright 2026 OpenC3, Inc.
4
4
  # All Rights Reserved.
5
5
  #
6
6
  # Licensed for Evaluation and Educational Use
@@ -33,9 +33,29 @@ class CfdpController < ApplicationController
33
33
  transaction = $cfdp_user.start_source_transaction(params)
34
34
  render json: transaction.id
35
35
  rescue ActionController::ParameterMissing => error
36
- render :json => { :status => 'error', :message => error.message }.as_json(:allow_nan => true), :status => 400
36
+ render :json => { :status => 'error', :message => error.message }.as_json(), :status => 400
37
37
  rescue => error
38
- render :json => { :status => 'error', :message => "#{error.message}\n#{error.backtrace.join("\n")}" }.as_json(:allow_nan => true), :status => 500
38
+ render :json => { :status => 'error', :message => "#{error.message}\n#{error.backtrace.join("\n")}" }.as_json(), :status => 500
39
+ end
40
+
41
+ def put_dir
42
+ # params matches that of put() except there's "source_directory_name" instead of "source_file_name", and there's no "destination_file_name"
43
+ params.require([:destination_entity_id, :source_directory_name])
44
+ return unless check_authorization()
45
+ transaction_ids = []
46
+ CfdpMib.list_directory_files(params[:source_directory_name]) do |filename|
47
+ file_params = params.dup
48
+ file_params.delete(:source_directory_name)
49
+ file_params[:source_file_name] = filename
50
+ file_params[:destination_file_name] = filename
51
+ transaction = $cfdp_user.start_source_transaction(file_params)
52
+ transaction_ids << transaction.id
53
+ end
54
+ render json: transaction_ids
55
+ rescue ActionController::ParameterMissing => error
56
+ render :json => { :status => 'error', :message => error.message }.as_json(), :status => 400
57
+ rescue => error
58
+ render :json => { :status => 'error', :message => "#{error.message}\n#{error.backtrace.join("\n")}" }.as_json(), :status => 500
39
59
  end
40
60
 
41
61
  # Cancel.request (transaction ID)
@@ -44,14 +64,18 @@ class CfdpController < ApplicationController
44
64
  return unless check_authorization()
45
65
  transaction = $cfdp_user.cancel(params)
46
66
  if transaction
47
- render json: transaction.id
67
+ if transaction.state == "CANCELED"
68
+ render json: transaction.id
69
+ else
70
+ render json: nil
71
+ end
48
72
  else
49
- render :json => { :status => 'error', :message => "Transaction #{params[:transaction_id]} not found" }.as_json(:allow_nan => true), :status => 404
73
+ render :json => { :status => 'error', :message => "Transaction #{params[:transaction_id]} not found" }.as_json(), :status => 404
50
74
  end
51
75
  rescue ActionController::ParameterMissing => error
52
- render :json => { :status => 'error', :message => error.message }.as_json(:allow_nan => true), :status => 400
76
+ render :json => { :status => 'error', :message => error.message }.as_json(), :status => 400
53
77
  rescue => error
54
- render :json => { :status => 'error', :message => "#{error.message}\n#{error.backtrace.join("\n")}" }.as_json(:allow_nan => true), :status => 500
78
+ render :json => { :status => 'error', :message => "#{error.message}\n#{error.backtrace.join("\n")}" }.as_json(), :status => 500
55
79
  end
56
80
 
57
81
  # Suspend.request (transaction ID)
@@ -60,14 +84,18 @@ class CfdpController < ApplicationController
60
84
  return unless check_authorization()
61
85
  transaction = $cfdp_user.suspend(params)
62
86
  if transaction
63
- render json: transaction.id
87
+ if transaction.state == "SUSPENDED"
88
+ render json: transaction.id
89
+ else
90
+ render json: nil
91
+ end
64
92
  else
65
- render :json => { :status => 'error', :message => "Transaction #{params[:transaction_id]} not found" }.as_json(:allow_nan => true), :status => 404
93
+ render :json => { :status => 'error', :message => "Transaction #{params[:transaction_id]} not found" }.as_json(), :status => 404
66
94
  end
67
95
  rescue ActionController::ParameterMissing => error
68
- render :json => { :status => 'error', :message => error.message }.as_json(:allow_nan => true), :status => 400
96
+ render :json => { :status => 'error', :message => error.message }.as_json(), :status => 400
69
97
  rescue => error
70
- render :json => { :status => 'error', :message => "#{error.message}\n#{error.backtrace.join("\n")}" }.as_json(:allow_nan => true), :status => 500
98
+ render :json => { :status => 'error', :message => "#{error.message}\n#{error.backtrace.join("\n")}" }.as_json(), :status => 500
71
99
  end
72
100
 
73
101
  # Resume.request (transaction ID)
@@ -76,14 +104,18 @@ class CfdpController < ApplicationController
76
104
  return unless check_authorization()
77
105
  transaction = $cfdp_user.resume(params)
78
106
  if transaction
79
- render json: transaction.id
107
+ if transaction.state == "ACTIVE"
108
+ render json: transaction.id
109
+ else
110
+ render json: nil
111
+ end
80
112
  else
81
- render :json => { :status => 'error', :message => "Transaction #{params[:transaction_id]} not found" }.as_json(:allow_nan => true), :status => 404
113
+ render :json => { :status => 'error', :message => "Transaction #{params[:transaction_id]} not found" }.as_json(), :status => 404
82
114
  end
83
115
  rescue ActionController::ParameterMissing => error
84
- render :json => { :status => 'error', :message => error.message }.as_json(:allow_nan => true), :status => 400
116
+ render :json => { :status => 'error', :message => error.message }.as_json(), :status => 400
85
117
  rescue => error
86
- render :json => { :status => 'error', :message => "#{error.message}\n#{error.backtrace.join("\n")}" }.as_json(:allow_nan => true), :status => 500
118
+ render :json => { :status => 'error', :message => "#{error.message}\n#{error.backtrace.join("\n")}" }.as_json(), :status => 500
87
119
  end
88
120
 
89
121
  # Report.request (transaction ID)
@@ -94,12 +126,12 @@ class CfdpController < ApplicationController
94
126
  if transaction
95
127
  render json: transaction.id
96
128
  else
97
- render :json => { :status => 'error', :message => "Transaction #{params[:transaction_id]} not found" }.as_json(:allow_nan => true), :status => 404
129
+ render :json => { :status => 'error', :message => "Transaction #{params[:transaction_id]} not found" }.as_json(), :status => 404
98
130
  end
99
131
  rescue ActionController::ParameterMissing => error
100
- render :json => { :status => 'error', :message => error.message }.as_json(:allow_nan => true), :status => 400
132
+ render :json => { :status => 'error', :message => error.message }.as_json(), :status => 400
101
133
  rescue => error
102
- render :json => { :status => 'error', :message => "#{error.message}\n#{error.backtrace.join("\n")}" }.as_json(:allow_nan => true), :status => 500
134
+ render :json => { :status => 'error', :message => "#{error.message}\n#{error.backtrace.join("\n")}" }.as_json(), :status => 500
103
135
  end
104
136
 
105
137
  def directory_listing
@@ -108,17 +140,17 @@ class CfdpController < ApplicationController
108
140
  transaction = $cfdp_user.start_directory_listing(params)
109
141
  render json: transaction.id
110
142
  rescue ActionController::ParameterMissing => error
111
- render :json => { :status => 'error', :message => error.message }.as_json(:allow_nan => true), :status => 400
143
+ render :json => { :status => 'error', :message => error.message }.as_json(), :status => 400
112
144
  rescue => error
113
- render :json => { :status => 'error', :message => "#{error.message}\n#{error.backtrace.join("\n")}" }.as_json(:allow_nan => true), :status => 500
145
+ render :json => { :status => 'error', :message => "#{error.message}\n#{error.backtrace.join("\n")}" }.as_json(), :status => 500
114
146
  end
115
147
 
116
148
  def subscribe
117
149
  return unless check_authorization()
118
150
  result = CfdpTopic.subscribe_indications
119
- render json: result.as_json(:allow_nan => true)
151
+ render json: result.as_json()
120
152
  rescue => error
121
- render :json => { :status => 'error', :message => "#{error.message}\n#{error.backtrace.join("\n")}" }.as_json(:allow_nan => true), :status => 500
153
+ render :json => { :status => 'error', :message => "#{error.message}\n#{error.backtrace.join("\n")}" }.as_json(), :status => 500
122
154
  end
123
155
 
124
156
  # Transaction.indication (transaction ID)
@@ -157,9 +189,9 @@ class CfdpController < ApplicationController
157
189
  def indications
158
190
  return unless check_authorization()
159
191
  result = CfdpTopic.read_indications(transaction_id: params[:transaction_id], continuation: params[:continuation], limit: params[:limit])
160
- render json: result.as_json(:allow_nan => true)
192
+ render json: result.as_json()
161
193
  rescue => error
162
- render :json => { :status => 'error', :message => "#{error.message}\n#{error.backtrace.join("\n")}" }.as_json(:allow_nan => true), :status => 500
194
+ render :json => { :status => 'error', :message => "#{error.message}\n#{error.backtrace.join("\n")}" }.as_json(), :status => 500
163
195
  end
164
196
 
165
197
  def transactions
@@ -174,9 +206,9 @@ class CfdpController < ApplicationController
174
206
  end
175
207
  end
176
208
  result = result.sort {|a, b| a.id <=> b.id}
177
- render json: result.as_json(:allow_nan => true)
209
+ render json: result.as_json()
178
210
  rescue => error
179
- render :json => { :status => 'error', :message => "#{error.message}\n#{error.backtrace.join("\n")}" }.as_json(:allow_nan => true), :status => 500
211
+ render :json => { :status => 'error', :message => "#{error.message}\n#{error.backtrace.join("\n")}" }.as_json(), :status => 500
180
212
  end
181
213
 
182
214
  # private
@@ -187,14 +219,14 @@ class CfdpController < ApplicationController
187
219
 
188
220
  if params[:remote_entity_id]
189
221
  if params[:remote_entity_id].to_i.to_s != params[:remote_entity_id].to_s
190
- render :json => { :status => 'error', :message => "remote_entity_id must be numeric" }.as_json(:allow_nan => true), :status => 400
222
+ render :json => { :status => 'error', :message => "remote_entity_id must be numeric" }.as_json(), :status => 400
191
223
  return false
192
224
  end
193
225
  cmd_entity_id = Integer(params[:remote_entity_id])
194
226
  cmd_entity = CfdpMib.entity(cmd_entity_id)
195
227
  elsif params[:destination_entity_id]
196
228
  if params[:destination_entity_id].to_i.to_s != params[:destination_entity_id].to_s
197
- render :json => { :status => 'error', :message => "destination_entity_id must be numeric" }.as_json(:allow_nan => true), :status => 400
229
+ render :json => { :status => 'error', :message => "destination_entity_id must be numeric" }.as_json(), :status => 400
198
230
  return
199
231
  end
200
232
  cmd_entity_id = Integer(params[:destination_entity_id])
@@ -215,16 +247,15 @@ class CfdpController < ApplicationController
215
247
  # Caller must be able to send this command
216
248
  return false unless authorization('cmd', target_name: target_name, packet_name: packet_name)
217
249
  else
218
- render :json => { :status => 'error', :message => "info not configured for entity: #{cmd_entity_id}" }.as_json(:allow_nan => true), :status => 400
250
+ render :json => { :status => 'error', :message => "info not configured for entity: #{cmd_entity_id}" }.as_json(), :status => 400
219
251
  return false
220
252
  end
221
253
  else
222
- render :json => { :status => 'error', :message => "Unknown entity: #{cmd_entity_id}" }.as_json(:allow_nan => true), :status => 400
254
+ render :json => { :status => 'error', :message => "Unknown entity: #{cmd_entity_id}" }.as_json(), :status => 400
223
255
  return false
224
256
  end
225
257
 
226
258
  # Authorized
227
- CfdpMib.cleanup_old_transactions()
228
259
  return true
229
260
  end
230
261
 
@@ -1,6 +1,6 @@
1
1
  # encoding: ascii-8bit
2
2
 
3
- # Copyright 2023 OpenC3, Inc.
3
+ # Copyright 2025 OpenC3, Inc.
4
4
  # All Rights Reserved.
5
5
  #
6
6
  # Licensed for Evaluation and Educational Use
@@ -15,8 +15,8 @@
15
15
  # The development of this software was funded in-whole or in-part by MethaneSAT LLC.
16
16
 
17
17
  class CfdpChecksum
18
- def initialize
19
- @checksum = 0
18
+ def initialize(checksum = 0)
19
+ @checksum = checksum
20
20
  end
21
21
 
22
22
  def add(offset, data)
@@ -49,4 +49,12 @@ class CfdpChecksum
49
49
  def check(file, other_checksum, full_checksum_needed)
50
50
  checksum(file, full_checksum_needed) == other_checksum
51
51
  end
52
- end
52
+
53
+ def self.json_create(object)
54
+ return CfdpChecksum.new(object['raw'])
55
+ end
56
+
57
+ def as_json(_options = nil)
58
+ return { "json_class" => self.class.to_s, "raw" => @checksum }
59
+ end
60
+ end
@@ -1,6 +1,6 @@
1
1
  # encoding: ascii-8bit
2
2
 
3
- # Copyright 2023 OpenC3, Inc.
3
+ # Copyright 2025 OpenC3, Inc.
4
4
  # All Rights Reserved.
5
5
  #
6
6
  # Licensed for Evaluation and Educational Use
@@ -40,4 +40,12 @@ class CfdpCrcChecksum
40
40
  def check(file, other_checksum, full_checksum_needed)
41
41
  checksum(file, full_checksum_needed) == other_checksum
42
42
  end
43
- end
43
+
44
+ def self.json_create(object)
45
+ return CfdpCrcChecksum.new(*object['raw'])
46
+ end
47
+
48
+ def as_json(_options = nil)
49
+ return { "json_class" => self.class.to_s, "raw" => [@crc.poly, @crc.seed, @crc.xor, @crc.reflect] }
50
+ end
51
+ end
@@ -42,9 +42,17 @@ require 'openc3/models/microservice_model'
42
42
  require 'openc3/utilities/bucket'
43
43
  require 'openc3/utilities/logger'
44
44
  require 'openc3/config/config_parser'
45
+ require_relative 'cfdp_transaction'
46
+ require_relative 'cfdp_source_transaction'
47
+ require_relative 'cfdp_receive_transaction'
45
48
  require 'tempfile'
46
49
  require 'fileutils'
47
50
  require 'json'
51
+ begin
52
+ require 'json/add/string'
53
+ rescue LoadError
54
+ # Earlier json didn't have this file
55
+ end
48
56
 
49
57
  class Tempfile
50
58
  def persist(filename)
@@ -117,6 +125,9 @@ class CfdpMib
117
125
  @@entities = {}
118
126
  @@bucket = nil
119
127
  @@root_path = "/"
128
+ @@prevent_received_file_overwrite = true
129
+ @@allow_duplicate_transaction_ids = false
130
+ @@transaction_cleanup_frequency_hours = 24
120
131
  @@transactions = {}
121
132
 
122
133
  def self.transactions
@@ -148,6 +159,10 @@ class CfdpMib
148
159
  end
149
160
 
150
161
  def self.root_path=(root_path)
162
+ # Always store an absolute path so filestore path safety checks work.
163
+ # A leading '/' is stripped from bucket keys (see bucket_key).
164
+ root_path = root_path.to_s
165
+ root_path = "/#{root_path}" unless root_path.start_with?('/')
151
166
  @@root_path = root_path
152
167
  end
153
168
 
@@ -155,6 +170,30 @@ class CfdpMib
155
170
  @@root_path
156
171
  end
157
172
 
173
+ def self.prevent_received_file_overwrite=(prevent_received_file_overwrite)
174
+ @@prevent_received_file_overwrite = prevent_received_file_overwrite
175
+ end
176
+
177
+ def self.prevent_received_file_overwrite
178
+ @@prevent_received_file_overwrite
179
+ end
180
+
181
+ def self.allow_duplicate_transaction_ids=(allow_duplicate_transaction_ids)
182
+ @@allow_duplicate_transaction_ids = allow_duplicate_transaction_ids
183
+ end
184
+
185
+ def self.allow_duplicate_transaction_ids
186
+ @@allow_duplicate_transaction_ids
187
+ end
188
+
189
+ def self.transaction_cleanup_frequency_hours=(transaction_cleanup_frequency_hours)
190
+ @@transaction_cleanup_frequency_hours = transaction_cleanup_frequency_hours
191
+ end
192
+
193
+ def self.transaction_cleanup_frequency_hours
194
+ @@transaction_cleanup_frequency_hours
195
+ end
196
+
158
197
  def self.define_entity(entity_id)
159
198
  entity_id = Integer(entity_id)
160
199
  entity = {}
@@ -237,11 +276,19 @@ class CfdpMib
237
276
  end
238
277
  end
239
278
 
279
+ # S3 treats '/' as an ordinary key character so a leading '/' would put every
280
+ # object under a hidden top level folder named '/'. root_path stays absolute
281
+ # for filesystem use and path safety checks, so strip it only for bucket keys.
282
+ def self.bucket_key(path)
283
+ path.sub(/\A\/+/, '')
284
+ end
285
+
240
286
  def self.get_source_file(source_file_name)
287
+ return nil if source_file_name.nil?
241
288
  file_name = File.join(@@root_path, source_file_name)
242
289
  if self.bucket
243
290
  file = Tempfile.new('cfdp', binmode: true)
244
- OpenC3::Bucket.getClient().get_object(bucket: self.bucket, key: file_name, path: file.path)
291
+ OpenC3::Bucket.getClient().get_object(bucket: self.bucket, key: bucket_key(file_name), path: file.path)
245
292
  else
246
293
  file = File.open(file_name, 'rb')
247
294
  end
@@ -251,24 +298,69 @@ class CfdpMib
251
298
  nil
252
299
  end
253
300
 
301
+ def self.list_directory_files(directory_name)
302
+ return if directory_name.nil?
303
+ directory_path = File.join(@@root_path, directory_name)
304
+ if self.bucket
305
+ client = OpenC3::Bucket.getClient()
306
+ prefix = bucket_key(directory_path) # list_objects doesn't work if you prepend a /
307
+ prefix += '/' unless prefix.end_with?('/') # but it needs a trailing / otherwise you could conflict with other dirs
308
+ objects = client.list_objects(bucket: self.bucket, prefix: prefix)
309
+ objects.each do |object|
310
+ next if object[:key].end_with?('/')
311
+ filename = object[:key].sub(/^#{Regexp.escape(bucket_key(@@root_path))}/, '')
312
+ yield filename
313
+ end
314
+ else
315
+ return unless Dir.exist?(directory_path)
316
+ Dir.entries(directory_path).each do |entry|
317
+ next if entry == '.' || entry == '..'
318
+ file_path = File.join(directory_path, entry) # path
319
+ next unless File.file?(file_path)
320
+ filename = File.join(directory_name, entry) # name
321
+ yield filename
322
+ end
323
+ end
324
+ rescue StandardError => error
325
+ OpenC3::Logger.error(error.message, scope: ENV['OPENC3_SCOPE'])
326
+ end
327
+
254
328
  def self.complete_source_file(file)
255
- file.close
329
+ file.close if file
256
330
  end
257
331
 
258
- def self.put_destination_file(destination_filename, tmp_file)
332
+ def self.put_destination_file(destination_filename, tmp_file, timestamp_format = "_%Y%m%d_%H%M%S")
259
333
  file_name = File.join(@@root_path, destination_filename)
334
+ actual_filename = destination_filename
335
+
260
336
  if self.bucket
261
- OpenC3::Bucket.getClient().put_object(bucket: self.bucket, key: file_name, body: tmp_file.open.read)
337
+ client = OpenC3::Bucket.getClient()
338
+ if @@prevent_received_file_overwrite && client.check_object(bucket: self.bucket, key: bucket_key(file_name))
339
+ # File exists, append timestamp to not overwrite it
340
+ timestamp = Time.now.utc.strftime(timestamp_format)
341
+ file_extension = File.extname(destination_filename)
342
+ base_name = File.basename(destination_filename, file_extension)
343
+ actual_filename = "#{base_name}#{timestamp}#{file_extension}"
344
+ file_name = File.join(@@root_path, actual_filename)
345
+ end
346
+ client.put_object(bucket: self.bucket, key: bucket_key(file_name), body: tmp_file.open.read)
262
347
  else
263
- file_name = File.join(@@root_path, destination_filename)
348
+ if @@prevent_received_file_overwrite && File.exist?(file_name)
349
+ # File exists, append timestamp to not overwrite it
350
+ timestamp = Time.now.utc.strftime(timestamp_format)
351
+ file_extension = File.extname(destination_filename)
352
+ base_name = File.basename(destination_filename, file_extension)
353
+ actual_filename = "#{base_name}#{timestamp}#{file_extension}"
354
+ file_name = File.join(@@root_path, actual_filename)
355
+ end
264
356
  tmp_file.persist(file_name)
265
357
  end
266
358
  tmp_file.unlink
267
- return true
359
+ return true, actual_filename
268
360
  rescue => error
269
361
  OpenC3::Logger.error(error.message, scope: ENV['OPENC3_SCOPE'])
270
362
  # Something went wrong so return false
271
- return false
363
+ return false, nil
272
364
  end
273
365
 
274
366
  def self.filestore_request(action_code, first_file_name, second_file_name)
@@ -289,7 +381,7 @@ class CfdpMib
289
381
  case action_code
290
382
  when "CREATE_FILE"
291
383
  if self.bucket
292
- OpenC3::Bucket.getClient().put_object(bucket: self.bucket, key: first_file_name, body: '')
384
+ OpenC3::Bucket.getClient().put_object(bucket: self.bucket, key: bucket_key(first_file_name), body: '')
293
385
  else
294
386
  FileUtils.touch(first_file_name)
295
387
  end
@@ -298,8 +390,8 @@ class CfdpMib
298
390
  when "DELETE_FILE"
299
391
  if self.bucket
300
392
  client = OpenC3::Bucket.getClient()
301
- if client.check_object(bucket: self.bucket, key: first_file_name)
302
- client.delete_object(bucket: self.bucket, key: first_file_name)
393
+ if client.check_object(bucket: self.bucket, key: bucket_key(first_file_name))
394
+ client.delete_object(bucket: self.bucket, key: bucket_key(first_file_name))
303
395
  status_code = "SUCCESSFUL"
304
396
  else
305
397
  status_code = "FILE_DOES_NOT_EXIST"
@@ -316,15 +408,15 @@ class CfdpMib
316
408
  when "RENAME_FILE"
317
409
  if self.bucket
318
410
  client = OpenC3::Bucket.getClient()
319
- if client.check_object(bucket: self.bucket, key: second_file_name)
411
+ if client.check_object(bucket: self.bucket, key: bucket_key(second_file_name))
320
412
  status_code = "NEW_FILE_ALREADY_EXISTS"
321
- elsif not client.check_object(bucket: self.bucket, key: first_file_name)
413
+ elsif not client.check_object(bucket: self.bucket, key: bucket_key(first_file_name))
322
414
  status_code = "OLD_FILE_DOES_NOT_EXIST"
323
415
  else
324
416
  temp = Tempfile.new('cfdp', binmode: true)
325
- client.get_object(bucket: self.bucket, key: first_file_name, path: temp.path)
326
- client.put_object(bucket: self.bucket, key: second_file_name, body: temp.read)
327
- client.delete_object(bucket: self.bucket, key: first_file_name)
417
+ client.get_object(bucket: self.bucket, key: bucket_key(first_file_name), path: temp.path)
418
+ client.put_object(bucket: self.bucket, key: bucket_key(second_file_name), body: temp.read)
419
+ client.delete_object(bucket: self.bucket, key: bucket_key(first_file_name))
328
420
  temp.unlink
329
421
  status_code = "SUCCESSFUL"
330
422
  end
@@ -342,16 +434,16 @@ class CfdpMib
342
434
  when "APPEND_FILE"
343
435
  if self.bucket
344
436
  client = OpenC3::Bucket.getClient()
345
- if not client.check_object(bucket: self.bucket, key: first_file_name)
437
+ if not client.check_object(bucket: self.bucket, key: bucket_key(first_file_name))
346
438
  status_code = "FILE_1_DOES_NOT_EXIST"
347
- elsif not client.check_object(bucket: self.bucket, key: second_file_name)
439
+ elsif not client.check_object(bucket: self.bucket, key: bucket_key(second_file_name))
348
440
  status_code = "FILE_2_DOES_NOT_EXIST"
349
441
  else
350
442
  temp1 = Tempfile.new('cfdp', binmode: true)
351
443
  temp2 = Tempfile.new('cfdp', binmode: true)
352
- client.get_object(bucket: self.bucket, key: first_file_name, path: temp1.path)
353
- client.get_object(bucket: self.bucket, key: second_file_name, path: temp2.path)
354
- client.put_object(bucket: self.bucket, key: first_file_name, body: temp1.read + temp2.read)
444
+ client.get_object(bucket: self.bucket, key: bucket_key(first_file_name), path: temp1.path)
445
+ client.get_object(bucket: self.bucket, key: bucket_key(second_file_name), path: temp2.path)
446
+ client.put_object(bucket: self.bucket, key: bucket_key(first_file_name), body: temp1.read + temp2.read)
355
447
  temp1.unlink
356
448
  temp2.unlink
357
449
  status_code = "SUCCESSFUL"
@@ -372,14 +464,14 @@ class CfdpMib
372
464
  when "REPLACE_FILE"
373
465
  if self.bucket
374
466
  client = OpenC3::Bucket.getClient()
375
- if not client.check_object(bucket: self.bucket, key: first_file_name)
467
+ if not client.check_object(bucket: self.bucket, key: bucket_key(first_file_name))
376
468
  status_code = "FILE_1_DOES_NOT_EXIST"
377
- elsif not client.check_object(bucket: self.bucket, key: second_file_name)
469
+ elsif not client.check_object(bucket: self.bucket, key: bucket_key(second_file_name))
378
470
  status_code = "FILE_2_DOES_NOT_EXIST"
379
471
  else
380
472
  temp = Tempfile.new('cfdp', binmode: true)
381
- client.get_object(bucket: self.bucket, key: second_file_name, path: temp.path)
382
- client.put_object(bucket: self.bucket, key: first_file_name, body: temp.read)
473
+ client.get_object(bucket: self.bucket, key: bucket_key(second_file_name), path: temp.path)
474
+ client.put_object(bucket: self.bucket, key: bucket_key(first_file_name), body: temp.read)
383
475
  temp.unlink
384
476
  status_code = "SUCCESSFUL"
385
477
  end
@@ -419,7 +511,7 @@ class CfdpMib
419
511
  when "DENY_FILE"
420
512
  if self.bucket
421
513
  begin
422
- OpenC3::Bucket.getClient().delete_object(bucket: self.bucket, key: first_file_name)
514
+ OpenC3::Bucket.getClient().delete_object(bucket: self.bucket, key: bucket_key(first_file_name))
423
515
  rescue
424
516
  # Don't care if the file doesn't exist
425
517
  end
@@ -492,6 +584,12 @@ class CfdpMib
492
584
  when 'root_path'
493
585
  root_path_defined = true
494
586
  CfdpMib.root_path = value
587
+ when 'prevent_received_file_overwrite'
588
+ CfdpMib.prevent_received_file_overwrite = value.downcase != "false"
589
+ when 'allow_duplicate_transaction_ids'
590
+ CfdpMib.allow_duplicate_transaction_ids = value.downcase != "false"
591
+ when 'transaction_cleanup_frequency_hours'
592
+ CfdpMib.transaction_cleanup_frequency_hours = Integer(value)
495
593
  else
496
594
  if current_entity_id
497
595
  case field_name
@@ -548,7 +646,7 @@ class CfdpMib
548
646
  raise "Value for MIB setting #{field_name} must be CLOSURE_REQUESTED or CLOSURE_NOT_REQUESTED"
549
647
  end
550
648
  when 'incomplete_file_disposition'
551
- value = value.to_s.upcaseD
649
+ value = value.to_s.upcase
552
650
  if ['DISCARD', 'RETAIN'].include?(value)
553
651
  CfdpMib.set_entity_value(current_entity_id, field_name, value)
554
652
  else
@@ -559,7 +657,7 @@ class CfdpMib
559
657
  fault_response = value[1].to_s.upcase
560
658
 
561
659
  raise "Value for MIB setting #{field_name} fault_type must be #{KNOWN_FAULT_TYPES.join(", ")}" unless KNOWN_FAULT_TYPES.include?(fault_type)
562
- raise "Value for MIB setting #{field_name} fault_response must be #{KNOWN_FAULT_RESPONSES.join(", ")}" unless KNOWN_FAULT_RESPONSES.include?(fault_type)
660
+ raise "Value for MIB setting #{field_name} fault_response must be #{KNOWN_FAULT_RESPONSES.join(", ")}" unless KNOWN_FAULT_RESPONSES.include?(fault_response)
563
661
  entity = CfdpMib.entity(current_entity_id)
564
662
  entity['fault_handler'][fault_type] = fault_response
565
663
 
@@ -575,6 +673,46 @@ class CfdpMib
575
673
  raise "OPTION source_entity_id is required" unless source_entity_defined
576
674
  raise "OPTION destination_entity_id is required" unless destination_entity_defined
577
675
  raise "OPTION root_path is required" unless root_path_defined
676
+
677
+ load_saved_transactions
678
+ end
679
+
680
+ def self.load_saved_transactions
681
+ saved_transaction_ids = CfdpTransaction.get_saved_transaction_ids
682
+ loaded_count = 0
683
+
684
+ saved_transaction_ids.each do |transaction_id|
685
+ begin
686
+ source_entity_id, transaction_seq_num = transaction_id.split('__').map(&:to_i)
687
+ if source_entity_id == @@source_entity_id
688
+ # This is a source transaction (we initiated it)
689
+ transaction = CfdpSourceTransaction.new(source_entity: @@entities[source_entity_id], transaction_seq_num: transaction_seq_num)
690
+ else
691
+ # This is a receive transaction (initiated by remote entity)
692
+ # We need to create a dummy PDU hash for initialization
693
+ dummy_pdu = {
694
+ "DIRECTIVE_CODE" => "METADATA",
695
+ "SOURCE_ENTITY_ID" => source_entity_id,
696
+ "SEQUENCE_NUMBER" => transaction_seq_num,
697
+ "TRANSMISSION_MODE" => "UNACKNOWLEDGED" # Default, will be overridden by loaded state
698
+ }
699
+ transaction = CfdpReceiveTransaction.new(dummy_pdu, no_persist: true) # Also calls handle_pdu inside
700
+ end
701
+
702
+ if transaction.load_state(transaction_id)
703
+ loaded_count += 1
704
+ else
705
+ OpenC3::Logger.warn("CFDP failed to load saved transaction: #{transaction_id}", scope: ENV['OPENC3_SCOPE'])
706
+ end
707
+
708
+ rescue => error
709
+ OpenC3::Logger.error("CFDP error loading saved transaction #{transaction_id}: #{error.message}", scope: ENV['OPENC3_SCOPE'])
710
+ # Remove invalid saved state
711
+ OpenC3::Store.del("#{CfdpTransaction.redis_key_prefix}cfdp_transaction_state:#{transaction_id}")
712
+ OpenC3::Store.srem("#{CfdpTransaction.redis_key_prefix}cfdp_saved_transaction_ids", transaction_id)
713
+ end
714
+ end
715
+ OpenC3::Logger.info("CFDP loaded #{loaded_count} saved transactions", scope: ENV['OPENC3_SCOPE'])
578
716
  end
579
717
 
580
718
  def self.directory_listing(directory_name, directory_file_name)
@@ -620,20 +758,23 @@ class CfdpMib
620
758
  @@entities = {}
621
759
  @@bucket = nil
622
760
  @@root_path = "/"
761
+ @@prevent_received_file_overwrite = true
762
+ @@allow_duplicate_transaction_ids = false
763
+ @@transaction_cleanup_frequency_hours = 24
623
764
  @@transactions = {}
765
+ CfdpTransaction.clear_saved_transaction_ids
624
766
  end
625
767
 
626
768
  def self.cleanup_old_transactions
627
- to_remove = []
628
769
  current_time = Time.now.utc
629
770
  transaction_retain_seconds = @@entities[@@source_entity_id]['transaction_retain_seconds']
771
+ cleaned_up_count = 0
630
772
  @@transactions.each do |id, transaction|
631
773
  if transaction.complete_time and (current_time - transaction.complete_time) > transaction_retain_seconds
632
- to_remove << id
774
+ transaction.delete
775
+ cleaned_up_count += 1
633
776
  end
634
777
  end
635
- to_remove.each do |id|
636
- @@transactions.delete(id)
637
- end
778
+ OpenC3::Logger.info("CFDP cleaned up #{cleaned_up_count} completed transactions", scope: ENV['OPENC3_SCOPE'])
638
779
  end
639
780
  end
@@ -1,6 +1,6 @@
1
1
  # encoding: ascii-8bit
2
2
 
3
- # Copyright 2023 OpenC3, Inc.
3
+ # Copyright 2025 OpenC3, Inc.
4
4
  # All Rights Reserved.
5
5
  #
6
6
  # Licensed for Evaluation and Educational Use
@@ -26,4 +26,12 @@ class CfdpNullChecksum
26
26
  def check(file, other_checkum, full_checksum_needed)
27
27
  true
28
28
  end
29
- end
29
+
30
+ def self.json_create(object)
31
+ return CfdpNullChecksum.new()
32
+ end
33
+
34
+ def as_json(_options = nil)
35
+ return { "json_class" => self.class.to_s }
36
+ end
37
+ end