From c345f7cf5acdcbacc37eb8ee47dc89321c32e963 Mon Sep 17 00:00:00 2001
From: Mike Hibler <hibler@cs.utah.edu>
Date: Fri, 2 May 2014 13:05:00 -0600
Subject: [PATCH] Add low/high sector numbers to the images table.

These are computed by imagedump for .ndz images. The plan is to
pass this info on to clients via tmcc so they can know the max disk
size required.

There will shortly be a utility to automatically update these values
when an image is created or updated. Stay tuned.
---
 db/Image.pm.in          | 80 +++++++++++++++++++++++++++++++++++++++++
 sql/database-create.sql |  3 ++
 sql/database-fill.sql   |  5 ++-
 sql/updates/4/390       | 47 ++++++++++++++++++++++++
 4 files changed, 134 insertions(+), 1 deletion(-)
 create mode 100644 sql/updates/4/390

diff --git a/db/Image.pm.in b/db/Image.pm.in
index ca6938d31c..2def9f6332 100644
--- a/db/Image.pm.in
+++ b/db/Image.pm.in
@@ -205,6 +205,38 @@ sub ActiveImages($)
     return \@result;
 }
 
+#
+# Return a list of all images of the given format for the given pid.
+# If format is NULL, return all formats. If pid is NULL, return for all pids.
+# List is names of the form "pid/imagename".
+#
+sub ListAll($$$)
+{
+    my ($class, $format, $pid);
+    my @result = ();
+
+    my $clause = "";
+    if (defined($format) && $format =~ /^([-\w]+)$/) {
+	$clause .= ($clause ? " and" : "where");
+	$clause .= " format='$1'";
+    }
+    if (defined($pid) && $pid =~ /^([-\w]+)$/) {
+	$clause .= ($clause ? " and" : "where");
+	$clause .= " pid='$1'";
+    }
+
+    my $query_result =
+	DBQueryWarn("select pid,imagename from images $clause ".
+		    "order by pid,imagename");
+    if ($query_result) {
+	while (my ($pid,$name) = $query_result->fetchrow_array()) {
+	    push(@result, "$pid/$name");
+	}
+    }
+
+    return @result;
+}
+
 #
 # Refresh a class instance by reloading from the DB.
 #
@@ -829,6 +861,31 @@ sub UnLockTables($)
     return 0;
 }
 
+#
+# Return updated time for image as a UNIX timestamp via the passed ref.
+# Return 0 on success, non-zero otherwise.
+#
+sub GetUpdate($$)
+{
+    my ($self,$stampp) = @_;
+
+    # Must be a real reference. 
+    return -1
+	if (! ref($self));
+
+    my $imageid     = $self->imageid();
+
+    my $result = DBQueryWarn("select UNIX_TIMESTAMP(updated) from images ".
+			     "where imageid='$imageid'");
+    if ($result && $result->numrows) {
+	my ($stamp) = $result->fetchrow_array();
+	$$stampp = $stamp;
+	return 0;
+    }
+
+    return -1;
+}
+
 #
 # Mark the update time in the record,
 #
@@ -893,6 +950,29 @@ sub SetHash($$)
     return 0;
 }
 
+#
+# Set the sector range of an image.
+#
+sub SetRange($$$;$)
+{
+    my ($self,$start,$end,$ssize) = @_;
+
+    # Must be a real reference. 
+    return -1
+	if (! ref($self));
+
+    my $imageid = $self->imageid();
+    $ssize = 512
+	if (!defined($ssize));
+
+    return -1
+	if (! DBQueryWarn("update images set ".
+			  "  lba_low=$start,lba_high=$end,lba_size=$ssize ".
+			  "where imageid='$imageid'"));
+
+    return 0;
+}
+
 #
 # Lock and Unlock
 #
diff --git a/sql/database-create.sql b/sql/database-create.sql
index ef5d07369c..2a43959484 100644
--- a/sql/database-create.sql
+++ b/sql/database-create.sql
@@ -1926,6 +1926,9 @@ CREATE TABLE `images` (
   `auth_key` varchar(512) default NULL,
   `decryption_key` varchar(256) default NULL,
   `hash` varchar(64) default NULL,
+  `lba_low` bigint(20) unsigned NOT NULL default '0',
+  `lba_high` bigint(20) unsigned NOT NULL default '0',
+  `lba_size` int(10) unsigned NOT NULL default '512',
   `locked` datetime default NULL,
   `locker_pid` int(11) default '0',
   `metadata_url` tinytext,
diff --git a/sql/database-fill.sql b/sql/database-fill.sql
index 63efe1d25f..01482a8455 100644
--- a/sql/database-fill.sql
+++ b/sql/database-fill.sql
@@ -1057,6 +1057,9 @@ REPLACE INTO table_regex VALUES ('images','max_concurrent','text','redirect','de
 REPLACE INTO table_regex VALUES ('images','reboot_waittime','text','redirect','default:int',0,0,NULL);
 REPLACE INTO table_regex VALUES ('images','format','text','regex','^[-\\w]+$',1,8,NULL);
 REPLACE INTO table_regex VALUES ('images','hash','text','regex','^[\\w]+$',16,64,NULL);
+REPLACE INTO `table_regex` VALUES ('images','lba_low','int','redirect','default:bigint',0,0,NULL);
+REPLACE INTO `table_regex` VALUES ('images','lba_high','int','redirect','default:bigint',0,0,NULL);
+REPLACE INTO `table_regex` VALUES ('images','lba_size','int','redirect','default:int',0,0,NULL);
 REPLACE INTO table_regex VALUES ('node_types','new_type','text','redirect','default:tinytext',0,0,NULL);
 REPLACE INTO table_regex VALUES ('node_types','node_type','text','regex','^[-\\w]+$',1,30,NULL);
 REPLACE INTO table_regex VALUES ('node_types','class','text','regex','^[\\w]+$',1,30,NULL);
@@ -1268,7 +1271,7 @@ REPLACE INTO table_regex VALUES ('default','boolean','int','regex','^(0|1)$',0,1
 REPLACE INTO table_regex VALUES ('default','tinyuint','int','regex','^[\\d]+$',0,255,'Default regex for tiny int fields. Allow any standard ascii integer, but no binary data');
 REPLACE INTO table_regex VALUES ('default','int','int','regex','^[\\d]+$',-2147483648,2147483647,'Default regex for int fields. Allow any standard ascii integer, but no binary data');
 REPLACE INTO table_regex VALUES ('default','float','float','regex','^[+-]?\\ *(\\d+(\\.\\d*)?|\\.\\d+)([eE][+-]?\\d+)?$',-2147483648,2147483647,'Default regex for float fields. Allow any digits and the decimal point');
-
+REPLACE INTO table_regex VALUES ('default','bigint','int','regex','^[\\d]+$',0,0,'Allow any ascii 64-bit integer');
 REPLACE INTO table_regex VALUES ('default','tinytext_utf8','text','regex','^(?:[\\x20-\\x7E]|[\\xC2-\\xDF][\\x80-\\xBF]|\\xE0[\\xA0-\\xBF][\\x80-\\xBF]|[\\xE1-\\xEC\\xEE\\xEF][\\x80-\\xBF]{2}|\\xED[\\x80-\\x9F][\\x80-\\xBF])*$',0,256,'adopted from http://www.w3.org/International/questions/qa-forms-utf-8.en.php');
 REPLACE INTO table_regex VALUES ('default','text_utf8','text','regex','^(?:[\\x20-\\x7E]|[\\xC2-\\xDF][\\x80-\\xBF]|\\xE0[\\xA0-\\xBF][\\x80-\\xBF]|[\\xE1-\\xEC\\xEE\\xEF][\\x80-\\xBF]{2}|\\xED[\\x80-\\x9F][\\x80-\\xBF])*$',0,65535,'adopted from http://www.w3.org/International/questions/qa-forms-utf-8.en.php');
 REPLACE INTO table_regex VALUES ('default','fulltext_utf8','text','regex','^(?:[\\x09\\x0A\\x0D\\x20-\\x7E]|[\\xC2-\\xDF][\\x80-\\xBF]|\\xE0[\\xA0-\\xBF][\\x80-\\xBF]|[\\xE1-\\xEC\\xEE\\xEF][\\x80-\\xBF]{2}|\\xED[\\x80-\\x9F][\\x80-\\xBF])*$',0,65535,'adopted from http://www.w3.org/International/questions/qa-forms-utf-8.en.php');
diff --git a/sql/updates/4/390 b/sql/updates/4/390
new file mode 100644
index 0000000000..511d6b9370
--- /dev/null
+++ b/sql/updates/4/390
@@ -0,0 +1,47 @@
+#
+# Image state info for tracking uncompressed size of image.
+#
+use strict;
+use libdb;
+
+sub DoUpdate($$$)
+{
+    my ($dbhandle, $dbname, $version) = @_;
+
+    if (!DBSlotExists("images", "lba_low")) {
+	DBQueryFatal("ALTER TABLE images ADD ".
+		     "`lba_low` bigint unsigned NOT NULL default '0'".
+		     "   AFTER hash");
+	DBQueryFatal("ALTER TABLE images ADD ".
+		     "`lba_high` bigint unsigned NOT NULL default '0'".
+		     "   AFTER lba_low");
+	DBQueryFatal("ALTER TABLE images ADD ".
+		     "`lba_size` int(10) unsigned NOT NULL default '512' ".
+		     "   AFTER lba_high");
+
+	#
+	# Since these are the first "bigint" types in the DB, add a regex
+	# for them.
+	# XXX cannot set min/max since the table_regex columns for those
+	# are type "int"!
+	#
+	DBQueryFatal("REPLACE INTO table_regex VALUES ".
+		     "('default','bigint','int','regex',".
+		     "'^[\\\\d]+\$',0,0,'Allow any ascii 64-bit integer')");
+
+	DBQueryFatal("REPLACE INTO table_regex VALUES ".
+		     "('images','lba_low','int','redirect',".
+		     "'default:bigint',0,0,NULL)");
+	DBQueryFatal("REPLACE INTO table_regex VALUES ".
+		     "('images','lba_high','int','redirect',".
+		     "'default:bigint',0,0,NULL)");
+	DBQueryFatal("REPLACE INTO table_regex VALUES ".
+		     "('images','lba_size','int','redirect',".
+		     "'default:int',0,0,NULL)");
+    }
+    return 0;
+}
+
+# Local Variables:
+# mode:perl
+# End:
-- 
GitLab