From 36adea515ab191e374bc4b2768e93d6e72713b9d Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Thu, 14 Apr 2016 01:27:41 -0500 Subject: [PATCH] Work on chroot jails. --- lib/kgrader.rb | 1 + lib/kgrader/assignment.rb | 10 ++++++---- lib/kgrader/cli.rb | 4 ++-- lib/kgrader/filesystem.rb | 47 ++++++++++++++++++++++++----------------------- lib/kgrader/jail.rb | 20 ++++++++++++++++++++ lib/kgrader/submission.rb | 20 +++++++++----------- 6 files changed, 62 insertions(+), 40 deletions(-) create mode 100644 lib/kgrader/jail.rb diff --git a/lib/kgrader.rb b/lib/kgrader.rb index 85fa912..c92b4c2 100644 --- a/lib/kgrader.rb +++ b/lib/kgrader.rb @@ -4,6 +4,7 @@ require_relative 'kgrader/cli' require_relative 'kgrader/course' require_relative 'kgrader/errors' require_relative 'kgrader/filesystem' +require_relative 'kgrader/jail' require_relative 'kgrader/roster' require_relative 'kgrader/submission' require_relative 'kgrader/task' diff --git a/lib/kgrader/assignment.rb b/lib/kgrader/assignment.rb index e7efec8..bf9ec3d 100644 --- a/lib/kgrader/assignment.rb +++ b/lib/kgrader/assignment.rb @@ -24,12 +24,14 @@ module KGrader end manifest = @config['manifest'] - provided = manifest['provided'].map do |glob| - Dir[File.join @root, 'provided', glob] - end.flatten + provided = manifest['provided'].map do |fn| + { :name => fn, :path => File.join(@root, 'provided', fn) } + end + graded = manifest['graded'].map { |fn| { :name => fn } } + { :provided => provided, - :graded => manifest['graded'], + :graded => graded, :report => manifest['report'] } end diff --git a/lib/kgrader/cli.rb b/lib/kgrader/cli.rb index 647a85e..6aeccfc 100644 --- a/lib/kgrader/cli.rb +++ b/lib/kgrader/cli.rb @@ -37,7 +37,7 @@ module KGrader end def clean - @fs.reset_jail + @fs.jail.reset @fs.clean_desk end @@ -46,7 +46,7 @@ module KGrader print "are you sure? [y/N] " abort "aborted" unless ['y', 'yes'].include? STDIN.gets.strip.downcase - @fs.reset_jail + @fs.jail.reset @fs.reset_desk end diff --git a/lib/kgrader/filesystem.rb b/lib/kgrader/filesystem.rb index 195833c..0d8f3a8 100644 --- a/lib/kgrader/filesystem.rb +++ b/lib/kgrader/filesystem.rb @@ -8,22 +8,8 @@ module KGrader @root = root end - def desk - File.join @root, 'desk' - end - - def jail - File.join @root, 'jail' - end - - def spec - File.join @root, 'spec' - end - - # ------------------------------------------------------------------------- - def course(name) - File.join spec, name + File.join spec_dir, name end def course_config(name) @@ -39,17 +25,17 @@ module KGrader end def roster(courseid, semester) - File.join desk, courseid, semester, '_roster.csv' + File.join desk_dir, courseid, semester, '_roster.csv' end def submission(courseid, semester, assignment, student) - File.join desk, courseid, semester, assignment, student + File.join desk_dir, courseid, semester, assignment, student end # ------------------------------------------------------------------------- def courses - Dir[File.join spec, '*', ''].map! { |fn| File.basename fn } + Dir[File.join spec_dir, '*', ''].map! { |fn| File.basename fn } end def assignments(courseid) @@ -79,19 +65,34 @@ module KGrader raise FilesystemError, "can't read file: #{path}" end - def reset_jail - FileUtils.rm_rf jail + def jail + @jail ||= Jail.new jail_dir end def reset_desk - FileUtils.rm_rf Dir[File.join desk, '*', ''] + FileUtils.rm_rf Dir[File.join desk_dir, '*', ''] end def clean_desk - Dir[File.join desk, '*', '*', '*', '*', 'status.txt'].each do |fn| + Dir[File.join desk_dir, '*', '*', '*', '*', 'status.txt'].each do |fn| File.write fn, "ungraded" if File.read(fn) == "graded" end - FileUtils.rm_rf Dir[File.join desk, '*', '*', '*', '*', 'pending'] + FileUtils.rm_rf Dir[File.join desk_dir, '*', '*', '*', '*', 'pending'] + end + + # ------------------------------------------------------------------------- + + private + def desk_dir + File.join @root, 'desk' + end + + def jail_dir + File.join @root, 'jail' + end + + def spec_dir + File.join @root, 'spec' end end end diff --git a/lib/kgrader/jail.rb b/lib/kgrader/jail.rb new file mode 100644 index 0000000..3ee2a43 --- /dev/null +++ b/lib/kgrader/jail.rb @@ -0,0 +1,20 @@ +module KGrader + class Jail + + def initialize(root) + @root = root + end + + def init + FileUtils.mkdir_p @root + end + + def reset + FileUtils.rm_rf @root + end + + def stage(source, target) + puts "[chroot::stage] #{source} -> (jail)/#{target}" + end + end +end diff --git a/lib/kgrader/submission.rb b/lib/kgrader/submission.rb index 95e4b90..2bcfadd 100644 --- a/lib/kgrader/submission.rb +++ b/lib/kgrader/submission.rb @@ -53,7 +53,7 @@ module KGrader stage # [grade the stuff] # [save report to gradefile] - # @fs.reset_jail + # @fs.jail.reset # FileUtils.touch pendingfile # self.status = :graded # return grade summary string @@ -108,17 +108,15 @@ module KGrader end def stage - @fs.reset_jail - FileUtils.mkdir_p @fs.jail + @fs.jail.reset + @fs.jail.init - # puts - # @assignment.manifest[:provided].each { |fn| p fn } - # abort - - # p @assignment.manifest[:provided], @assignment.manifest[:graded], @assignment.manifest[:report] - - # copytree files matching manifest from student submission - # use assignment.stage + @assignment.manifest[:provided].each do |entry| + @fs.jail.stage entry[:path], entry[:name] + end + @assignment.manifest[:graded].each do |entry| + @fs.jail.stage File.join(repo, entry[:name]), entry[:name] + end end end end