Module: Omnizip::Formats::Iso::RockRidge

Defined in:
lib/omnizip/formats/iso/rock_ridge.rb

Overview

Rock Ridge Extensions for ISO 9660

Implements System Use Sharing Protocol (SUSP) and Rock Ridge extensions to store Unix file attributes, permissions, symbolic links, and device nodes in ISO 9660 images.

Rock Ridge allows ISO images to preserve full Unix filesystem semantics.

Defined Under Namespace

Modules: Signatures Classes: SUEntry

Class Method Summary collapse

Class Method Details

.add_extensions(record_data, file_stat, name, is_symlink: false) ⇒ String

Add Rock Ridge extensions to directory record

Parameters:

  • record_data (String)

    Directory record data

  • file_stat (File::Stat)

    File statistics

  • name (String)

    File name

  • is_symlink (Boolean) (defaults to: false)

    Is this a symbolic link

Returns:

  • (String)

    Updated record with Rock Ridge fields



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/omnizip/formats/iso/rock_ridge.rb', line 79

def self.add_extensions(record_data, file_stat, name, is_symlink: false)
  system_use = +""

  # Add SP entry (first entry in root directory only)
  # This indicates Rock Ridge is being used
  # For simplicity, we'll add to all entries

  # Add PX (POSIX attributes)
  system_use << build_px_entry(file_stat).to_binary

  # Add TF (timestamps)
  system_use << build_tf_entry(file_stat).to_binary

  # Add NM (alternate name) if different from ISO name
  unless name == File.basename(name).upcase
    system_use << build_nm_entry(name).to_binary
  end

  # Add SL (symbolic link) if applicable
  if is_symlink
    link_target = File.readlink(file_stat)
    system_use << build_sl_entry(link_target).to_binary
  end

  # Append system use to record
  record_data + system_use
end

.build_nm_entry(name) ⇒ SUEntry

Build NM (alternate name) entry

Parameters:

  • name (String)

    File name

Returns:



170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/omnizip/formats/iso/rock_ridge.rb', line 170

def self.build_nm_entry(name)
  # Flags
  # Bit 0: CONTINUE (name continues in next NM)
  # Bit 1: CURRENT (name is ".")
  # Bit 2: PARENT (name is "..")
  flags = 0

  data = +""
  data << [flags].pack("C")
  data << name

  SUEntry.new(Signatures::NM, data: data)
end

.build_pn_entry(stat) ⇒ SUEntry

Build PN (POSIX device number) entry

Parameters:

  • stat (File::Stat)

    File statistics

Returns:



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/omnizip/formats/iso/rock_ridge.rb', line 215

def self.build_pn_entry(stat)
  data = +""

  # Device number high (both-endian)
  dev_high = (stat.rdev >> 32) & 0xFFFFFFFF
  data << [dev_high].pack("V")
  data << [dev_high].pack("N")

  # Device number low (both-endian)
  dev_low = stat.rdev & 0xFFFFFFFF
  data << [dev_low].pack("V")
  data << [dev_low].pack("N")

  SUEntry.new(Signatures::PN, data: data)
end

.build_px_entry(stat) ⇒ SUEntry

Build PX (POSIX attributes) entry

Parameters:

  • stat (File::Stat)

    File statistics

Returns:



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/omnizip/formats/iso/rock_ridge.rb', line 111

def self.build_px_entry(stat)
  data = +""

  # File mode (both-endian)
  data << [stat.mode].pack("V")
  data << [stat.mode].pack("N")

  # Number of links (both-endian)
  data << [stat.nlink].pack("V")
  data << [stat.nlink].pack("N")

  # User ID (both-endian)
  data << [stat.uid].pack("V")
  data << [stat.uid].pack("N")

  # Group ID (both-endian)
  data << [stat.gid].pack("V")
  data << [stat.gid].pack("N")

  # Inode number (both-endian) - optional
  data << [stat.ino].pack("V")
  data << [stat.ino].pack("N")

  SUEntry.new(Signatures::PX, data: data)
end

.build_sl_entry(target) ⇒ SUEntry

Build SL (symbolic link) entry

Parameters:

  • target (String)

    Link target

Returns:



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/omnizip/formats/iso/rock_ridge.rb', line 188

def self.build_sl_entry(target)
  # Flags
  # Bit 0: CONTINUE (link continues in next SL)
  flags = 0

  data = +""
  data << [flags].pack("C")

  # Component flags
  # Bit 0: CONTINUE (component continues)
  # Bit 1: CURRENT (component is ".")
  # Bit 2: PARENT (component is "..")
  # Bit 3: ROOT (component is root)
  comp_flags = 0

  # Component length
  data << [comp_flags].pack("C")
  data << [target.bytesize].pack("C")
  data << target

  SUEntry.new(Signatures::SL, data: data)
end

.build_tf_entry(stat) ⇒ SUEntry

Build TF (timestamps) entry

Parameters:

  • stat (File::Stat)

    File statistics

Returns:



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/omnizip/formats/iso/rock_ridge.rb', line 141

def self.build_tf_entry(stat)
  # Flags indicating which timestamps are present
  # Bit 0: Creation time
  # Bit 1: Modify time
  # Bit 2: Access time
  # Bit 3: Attributes time
  # Bit 4: Backup time
  # Bit 5: Expiration time
  # Bit 6: Effective time
  # Bit 7: Long format (17 bytes instead of 7)

  flags = 0b00000110 # Modify and access times, short format

  data = +""
  data << [flags].pack("C")

  # Modification time (7-byte format)
  data << encode_time_7byte(stat.mtime)

  # Access time (7-byte format)
  data << encode_time_7byte(stat.atime)

  SUEntry.new(Signatures::TF, data: data)
end

.encode_time_7byte(time) ⇒ String

Encode time in 7-byte format

Parameters:

  • time (Time)

    Time to encode

Returns:

  • (String)

    7-byte encoded time



235
236
237
238
239
240
241
242
243
244
245
# File 'lib/omnizip/formats/iso/rock_ridge.rb', line 235

def self.encode_time_7byte(time)
  [
    time.year - 1900,
    time.month,
    time.day,
    time.hour,
    time.min,
    time.sec,
    0, # GMT offset
  ].pack("C7")
end

.parse_extensions(system_use) ⇒ Hash

Parse Rock Ridge extensions from system use field

Parameters:

  • system_use (String)

    System use field data

Returns:

  • (Hash)

    Parsed attributes



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/omnizip/formats/iso/rock_ridge.rb', line 251

def self.parse_extensions(system_use)
  attributes = {}
  offset = 0

  while offset < system_use.bytesize
    entry = SUEntry.parse(system_use, offset)

    case entry.signature
    when Signatures::PX
      attributes[:posix] = parse_px_entry(entry)
    when Signatures::TF
      attributes[:times] = parse_tf_entry(entry)
    when Signatures::NM
      attributes[:name] = parse_nm_entry(entry)
    when Signatures::SL
      attributes[:symlink] = parse_sl_entry(entry)
    end

    offset += entry.length
  end

  attributes
end

.parse_nm_entry(entry) ⇒ String

Parse NM entry

Parameters:

Returns:

  • (String)

    Alternate name



316
317
318
# File 'lib/omnizip/formats/iso/rock_ridge.rb', line 316

def self.parse_nm_entry(entry)
  entry.data[1..]
end

.parse_px_entry(entry) ⇒ Hash

Parse PX entry

Parameters:

Returns:

  • (Hash)

    Parsed POSIX attributes



279
280
281
282
283
284
285
286
287
288
# File 'lib/omnizip/formats/iso/rock_ridge.rb', line 279

def self.parse_px_entry(entry)
  data = entry.data
  {
    mode: data[0, 4].unpack1("V"),
    nlink: data[8, 4].unpack1("V"),
    uid: data[16, 4].unpack1("V"),
    gid: data[24, 4].unpack1("V"),
    ino: data[32, 4].unpack1("V"),
  }
end

.parse_sl_entry(entry) ⇒ String

Parse SL entry

Parameters:

Returns:

  • (String)

    Symbolic link target



324
325
326
327
328
# File 'lib/omnizip/formats/iso/rock_ridge.rb', line 324

def self.parse_sl_entry(entry)
  # Skip flags byte, component flags, read length and target
  comp_len = entry.data.getbyte(2)
  entry.data[3, comp_len]
end

.parse_tf_entry(entry) ⇒ Hash

Parse TF entry

Parameters:

Returns:

  • (Hash)

    Parsed timestamps



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/omnizip/formats/iso/rock_ridge.rb', line 294

def self.parse_tf_entry(entry)
  # Parse based on flags
  flags = entry.data.getbyte(0)
  times = {}

  offset = 1
  if flags & 0x02 # Modify time
    times[:mtime] = parse_time_7byte(entry.data[offset, 7])
    offset += 7
  end

  if flags & 0x04 # Access time
    times[:atime] = parse_time_7byte(entry.data[offset, 7])
  end

  times
end

.parse_time_7byte(data) ⇒ Time

Parse 7-byte time format

Parameters:

  • data (String)

    7-byte time data

Returns:

  • (Time)

    Parsed time



334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/omnizip/formats/iso/rock_ridge.rb', line 334

def self.parse_time_7byte(data)
  year = 1900 + data.getbyte(0)
  month = data.getbyte(1)
  day = data.getbyte(2)
  hour = data.getbyte(3)
  minute = data.getbyte(4)
  second = data.getbyte(5)

  Time.new(year, month, day, hour, minute, second)
rescue ArgumentError
  Time.now
end