Tests if we can add two Resource objects together resulting in one valid Resource object. For example, two Resource objects with different name, type or role are not addable.
| 360 | // valid Resource object. For example, two Resource objects with |
| 361 | // different name, type or role are not addable. |
| 362 | static bool addable(const Resource& left, const Resource& right) |
| 363 | { |
| 364 | // Check SharedInfo. |
| 365 | if (left.has_shared() != right.has_shared()) { |
| 366 | return false; |
| 367 | } |
| 368 | |
| 369 | // For shared resources, they can be added only if left == right. |
| 370 | if (left.has_shared()) { |
| 371 | return left == right; |
| 372 | } |
| 373 | |
| 374 | // Now, we verify if the two non-shared resources can be added. |
| 375 | if (left.name() != right.name() || left.type() != right.type()) { |
| 376 | return false; |
| 377 | } |
| 378 | |
| 379 | // Check AllocationInfo. |
| 380 | if (left.has_allocation_info() != right.has_allocation_info()) { |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | if (left.has_allocation_info() && |
| 385 | left.allocation_info() != right.allocation_info()) { |
| 386 | return false; |
| 387 | } |
| 388 | |
| 389 | // Check the stack of ReservationInfo. |
| 390 | if (left.reservations_size() != right.reservations_size()) { |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | for (int i = 0; i < left.reservations_size(); ++i) { |
| 395 | if (left.reservations(i) != right.reservations(i)) { |
| 396 | return false; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | // Check DiskInfo. |
| 401 | if (left.has_disk() != right.has_disk()) { return false; } |
| 402 | |
| 403 | if (left.has_disk()) { |
| 404 | if (left.disk() != right.disk()) { return false; } |
| 405 | |
| 406 | if (left.disk().has_source()) { |
| 407 | switch (left.disk().source().type()) { |
| 408 | case Resource::DiskInfo::Source::PATH: { |
| 409 | // Two PATH resources can be added if their disks are identical. |
| 410 | break; |
| 411 | } |
| 412 | case Resource::DiskInfo::Source::BLOCK: |
| 413 | case Resource::DiskInfo::Source::MOUNT: { |
| 414 | // Two resources that represent exclusive 'MOUNT' or 'RAW' disks |
| 415 | // cannot be added together; this would defeat the exclusivity. |
| 416 | return false; |
| 417 | } |
| 418 | case Resource::DiskInfo::Source::RAW: { |
| 419 | // We can only add resources representing 'RAW' disks if |
no test coverage detected