2013-10-09 06:44:39 +04:00
#!/usr/bin/env gjs
//
// Copyright (C) 2013 Colin Walters <walters@verbum.org>
//
2018-01-30 22:26:26 +03:00
// SPDX-License-Identifier: LGPL-2.0+
//
2013-10-09 06:44:39 +04:00
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
2021-12-07 04:20:55 +03:00
// License along with this library. If not, see <https://www.gnu.org/licenses/>.
2013-10-09 06:44:39 +04:00
const GLib = imports . gi . GLib ;
const Gio = imports . gi . Gio ;
const OSTree = imports . gi . OSTree ;
function assertEquals ( a , b ) {
if ( a != b )
throw new Error ( "assertion failed " + JSON . stringify ( a ) + " == " + JSON . stringify ( b ) ) ;
}
2019-10-22 23:59:19 +03:00
function assertGreater ( a , b ) {
if ( a <= b )
throw new Error ( "assertion failed " + JSON . stringify ( a ) + " > " + JSON . stringify ( b ) ) ;
}
function assertGreaterEquals ( a , b ) {
if ( a < b )
throw new Error ( "assertion failed " + JSON . stringify ( a ) + " >= " + JSON . stringify ( b ) ) ;
}
// Adapted from _ostree_read_varuint64()
function readVarint ( buffer ) {
let result = 0 ;
let count = 0 ;
let len = buffer . length ;
let cur ;
do {
assertGreater ( len , 0 ) ;
cur = buffer [ count ] ;
result = result | ( ( cur & 0x7F ) << ( 7 * count ) ) ;
count ++ ;
len -- ;
} while ( cur & 0x80 ) ;
return [ result , count ] ;
}
// There have been various bugs with byte array unpacking in GJS, so
// just do it manually.
function unpackByteArray ( variant ) {
let array = [ ] ;
let nBytes = variant . n _children ( ) ;
for ( let i = 0 ; i < nBytes ; i ++ ) {
array . push ( variant . get _child _value ( i ) . get _byte ( ) ) ;
}
return array ;
}
2019-10-24 18:10:57 +03:00
function validateSizes ( repo , commit , expectedObjects ) {
2019-10-22 23:59:19 +03:00
let [ , commitVariant ] = repo . load _variant ( OSTree . ObjectType . COMMIT , commit ) ;
let metadata = commitVariant . get _child _value ( 0 ) ;
let sizes = metadata . lookup _value ( 'ostree.sizes' , GLib . VariantType . new ( 'aay' ) ) ;
2019-10-24 18:10:57 +03:00
let nObjects = sizes . n _children ( ) ;
let expectedNObjects = Object . keys ( expectedObjects ) . length
assertEquals ( nObjects , expectedNObjects ) ;
2019-10-22 23:59:19 +03:00
2019-10-24 18:10:57 +03:00
for ( let i = 0 ; i < nObjects ; i ++ ) {
2019-10-22 23:59:19 +03:00
let sizeEntry = sizes . get _child _value ( i ) ;
assertGreaterEquals ( sizeEntry . n _children ( ) , 34 ) ;
let entryBytes = unpackByteArray ( sizeEntry ) ;
let checksumBytes = entryBytes . slice ( 0 , 32 ) ;
let checksumString = OSTree . checksum _from _bytes ( checksumBytes ) ;
print ( "checksum = " + checksumString ) ;
// Read the sizes from the next 2 varints
let remainingBytes = entryBytes . slice ( 32 ) ;
assertGreaterEquals ( remainingBytes . length , 2 ) ;
let varintRead ;
let compressedSize ;
let uncompressedSize ;
[ compressedSize , varintRead ] = readVarint ( remainingBytes ) ;
remainingBytes = remainingBytes . slice ( varintRead ) ;
assertGreaterEquals ( remainingBytes . length , 1 ) ;
[ uncompressedSize , varintRead ] = readVarint ( remainingBytes ) ;
remainingBytes = remainingBytes . slice ( varintRead ) ;
2019-10-24 18:10:57 +03:00
assertEquals ( remainingBytes . length , 1 ) ;
let objectType = remainingBytes [ 0 ] ;
let objectTypeString = OSTree . object _type _to _string ( objectType ) ;
2019-10-22 23:59:19 +03:00
print ( "compressed = " + compressedSize ) ;
print ( "uncompressed = " + uncompressedSize ) ;
2019-10-24 18:10:57 +03:00
print ( "objtype = " + objectTypeString + " (" + objectType + ")" ) ;
let objectName = OSTree . object _to _string ( checksumString , objectType ) ;
print ( "object = " + objectName ) ;
2019-10-22 23:59:19 +03:00
2019-10-24 18:10:57 +03:00
if ( ! ( objectName in expectedObjects ) ) {
throw new Error ( "Object " + objectName + " not in " +
JSON . stringify ( expectedObjects ) ) ;
2019-10-22 23:59:19 +03:00
}
2019-10-24 18:10:57 +03:00
let expectedSizes = expectedObjects [ objectName ] ;
2019-10-22 23:59:19 +03:00
let expectedCompressedSize = expectedSizes [ 0 ] ;
let expectedUncompressedSize = expectedSizes [ 1 ] ;
if ( compressedSize != expectedCompressedSize ) {
throw new Error ( "Compressed size " + compressedSize +
" for checksum " + checksumString +
" does not match expected " + expectedCompressedSize ) ;
}
if ( uncompressedSize != expectedUncompressedSize ) {
throw new Error ( "Uncompressed size " + uncompressedSize +
" for checksum " + checksumString +
" does not match expected " + expectedUncompressedSize ) ;
}
}
}
2019-10-23 18:10:06 +03:00
print ( '1..3' )
2017-07-20 17:32:44 +03:00
2013-10-09 06:44:39 +04:00
let testDataDir = Gio . File . new _for _path ( 'test-data' ) ;
testDataDir . make _directory ( null ) ;
testDataDir . get _child ( 'some-file' ) . replace _contents ( "hello world!" , null , false , 0 , null ) ;
2019-10-23 18:43:10 +03:00
testDataDir . get _child ( 'some-file' ) . copy ( testDataDir . get _child ( 'duplicate-file' ) ,
Gio . FileCopyFlags . OVERWRITE ,
null , null ) ;
testDataDir . get _child ( 'link-file' ) . make _symbolic _link ( 'some-file' , null ) ;
2013-10-09 06:44:39 +04:00
testDataDir . get _child ( 'another-file' ) . replace _contents ( "hello world again!" , null , false , 0 , null ) ;
let repoPath = Gio . File . new _for _path ( 'repo' ) ;
let repo = OSTree . Repo . new ( repoPath ) ;
repo . create ( OSTree . RepoMode . ARCHIVE _Z2 , null ) ;
repo . open ( null ) ;
2019-10-22 23:59:19 +03:00
let commitModifierFlags = ( OSTree . RepoCommitModifierFlags . GENERATE _SIZES |
OSTree . RepoCommitModifierFlags . SKIP _XATTRS |
OSTree . RepoCommitModifierFlags . CANONICAL _PERMISSIONS ) ;
let commitModifier = OSTree . RepoCommitModifier . new ( commitModifierFlags , null ) ;
2013-10-09 06:44:39 +04:00
assertEquals ( repo . get _mode ( ) , OSTree . RepoMode . ARCHIVE _Z2 ) ;
repo . prepare _transaction ( null ) ;
let mtree = OSTree . MutableTree . new ( ) ;
repo . write _directory _to _mtree ( testDataDir , mtree , commitModifier , null ) ;
let [ , dirTree ] = repo . write _mtree ( mtree , null ) ;
let [ , commit ] = repo . write _commit ( null , 'Some subject' , 'Some body' , null , dirTree , null ) ;
print ( "commit => " + commit ) ;
2019-07-09 00:39:11 +03:00
repo . commit _transaction ( null ) ;
2013-10-09 06:44:39 +04:00
2019-10-24 18:10:57 +03:00
// Test the sizes metadata. The key is the object and the value is an
// array of compressed size and uncompressed size.
let expectedObjects = {
'f5ee222a21e2c96edbd6f2543c4bc8a039f827be3823d04777c9ee187778f1ad.file' : [
54 , 18
] ,
'd35bfc50864fca777dbeead3ba3689115b76674a093210316589b1fe5cc3ff4b.file' : [
48 , 12
] ,
'8322876a078e79d8c960b8b4658fe77e7b2f878f8a6cf89dbb87c6becc8128a0.file' : [
43 , 9
] ,
'1c77033ca06eae77ed99cb26472969964314ffd5b4e4c0fd3ff6ec4265c86e51.dirtree' : [
185 , 185
] ,
'446a0ef11b7cc167f3b603e585c7eeeeb675faa412d5ec73f62988eb0b6c5488.dirmeta' : [
12 , 12
] ,
2019-10-22 23:59:19 +03:00
} ;
2019-10-24 18:10:57 +03:00
validateSizes ( repo , commit , expectedObjects ) ;
2013-10-09 06:44:39 +04:00
2017-07-20 17:32:44 +03:00
print ( "ok test-sizes" ) ;
2019-10-23 00:14:58 +03:00
2019-10-23 18:10:06 +03:00
// Remove a file to make sure that metadata is not reused from the
2019-10-23 18:43:10 +03:00
// previous commit. Remove that file from the expected metadata and
// replace the dirtree object.
2019-10-23 18:10:06 +03:00
testDataDir . get _child ( 'another-file' ) . delete ( null ) ;
2019-10-24 18:10:57 +03:00
delete expectedObjects [ 'f5ee222a21e2c96edbd6f2543c4bc8a039f827be3823d04777c9ee187778f1ad.file' ] ;
delete expectedObjects [ '1c77033ca06eae77ed99cb26472969964314ffd5b4e4c0fd3ff6ec4265c86e51.dirtree' ] ;
expectedObjects [ 'a384660cc18ffdb60296961dde9a2d6f78f4fec095165652cb53aa81f6dc7539.dirtree' ] = [
138 , 138
] ;
2019-10-23 18:10:06 +03:00
repo . prepare _transaction ( null ) ;
mtree = OSTree . MutableTree . new ( ) ;
repo . write _directory _to _mtree ( testDataDir , mtree , commitModifier , null ) ;
[ , dirTree ] = repo . write _mtree ( mtree , null ) ;
[ , commit ] = repo . write _commit ( null , 'Some subject' , 'Some body' , null , dirTree , null ) ;
print ( "commit => " + commit ) ;
repo . commit _transaction ( null ) ;
2019-10-24 18:10:57 +03:00
validateSizes ( repo , commit , expectedObjects ) ;
2019-10-23 18:10:06 +03:00
print ( "ok test-sizes file deleted" ) ;
2019-10-23 00:14:58 +03:00
// Repeat the commit now that all the objects are cached and ensure the
// metadata is still correct
repo . prepare _transaction ( null ) ;
mtree = OSTree . MutableTree . new ( ) ;
repo . write _directory _to _mtree ( testDataDir , mtree , commitModifier , null ) ;
[ , dirTree ] = repo . write _mtree ( mtree , null ) ;
[ , commit ] = repo . write _commit ( null , 'Another subject' , 'Another body' , null , dirTree , null ) ;
print ( "commit => " + commit ) ;
repo . commit _transaction ( null ) ;
2019-10-24 18:10:57 +03:00
validateSizes ( repo , commit , expectedObjects ) ;
2019-10-23 00:14:58 +03:00
print ( "ok test-sizes repeated" ) ;