Skip to content

Commit

Permalink
Add basic support for presetting custom files in sandbox.
Browse files Browse the repository at this point in the history
  • Loading branch information
hermanzdosilovic committed Jan 15, 2020
1 parent 41e846b commit 7829db8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
5 changes: 4 additions & 1 deletion app/controllers/submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,12 @@ def submission_params
:enable_per_process_and_thread_memory_limit,
:max_file_size,
:redirect_stderr_to_stdout,
:callback_url
:callback_url,
:archive
)

submission_params[:archive] = Base64Service.decode(submission_params[:archive])

params[:base64_encoded] == "true" ? decode_params(submission_params) : submission_params
end

Expand Down
11 changes: 10 additions & 1 deletion app/jobs/isolate_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ class IsolateJob < ApplicationJob
STDOUT_FILE_NAME = "stdout.txt"
STDERR_FILE_NAME = "stderr.txt"
METADATA_FILE_NAME = "metadata.txt"
ARCHIVE_FILE_NAME = "archive.zip"

attr_reader :submission, :cgroups,
:box_id, :workdir, :boxdir, :tmpdir,
:source_file, :stdin_file, :stdout_file, :stderr_file, :metadata_file
:source_file, :stdin_file, :stdout_file,
:stderr_file, :metadata_file, :archive_file

def perform(submission)
@submission = submission
Expand Down Expand Up @@ -58,13 +60,20 @@ def initialize_workdir
@stdout_file = workdir + "/" + STDOUT_FILE_NAME
@stderr_file = workdir + "/" + STDERR_FILE_NAME
@metadata_file = workdir + "/" + METADATA_FILE_NAME
@archive_file = boxdir + "/" + ARCHIVE_FILE_NAME

[stdin_file, stdout_file, stderr_file, metadata_file].each do |f|
initialize_file(f)
end

File.open(source_file, "wb") { |f| f.write(submission.source_code) }
File.open(stdin_file, "wb") { |f| f.write(submission.stdin) }

if submission.archive?
File.open(archive_file, "wb") { |f| f.write(submission.archive) }
`cd #{boxdir} && timeout -s 15 -k 1s 2s unzip -n -qq #{archive_file} 2>&1`
File.delete(archive_file)
end
end

def initialize_file(file)
Expand Down
4 changes: 4 additions & 0 deletions app/serializers/submission_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def language
ActiveModelSerializers::SerializableResource.new(object.language, { serializer: LanguageSerializer, fields: [:id, :name] })
end

def archive
Base64Service.encode(object.archive)
end

private

def object_decoder(method)
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20200115205044_add_archive_to_submissions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddArchiveToSubmissions < ActiveRecord::Migration[5.0]
def change
add_column :submissions, :archive, :binary
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20200114220437) do
ActiveRecord::Schema.define(version: 20200115205044) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -55,6 +55,7 @@
t.string "command_line_arguments"
t.boolean "redirect_stderr_to_stdout"
t.string "callback_url"
t.binary "archive"
t.index ["token"], name: "index_submissions_on_token", using: :btree
end

Expand Down

0 comments on commit 7829db8

Please sign in to comment.