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