Ticket #10141: 0001-Add-disk-image-menu-to-register-unregister-disk-images.patch

File 0001-Add-disk-image-menu-to-register-unregister-disk-images.patch, 9.3 KB (added by dsjonny, 10 years ago)
  • src/apps/drivesetup/Jamfile

    From 0addc038c17f8b9ac37266eadd7bdded34d63e30 Mon Sep 17 00:00:00 2001
    From: =?UTF-8?q?Dancs=C3=B3=20R=C3=B3bert?= <dancso.robert@d-rendszer.hu>
    Date: Mon, 28 Oct 2013 15:12:32 +0100
    Subject: [PATCH] Add disk image menu to register/unregister disk images from
     DriveSetup.
    
    ---
     src/apps/drivesetup/Jamfile        |   2 +-
     src/apps/drivesetup/MainWindow.cpp | 193 ++++++++++++++++++++++++++++++++++++-
     src/apps/drivesetup/MainWindow.h   |   6 ++
     3 files changed, 199 insertions(+), 2 deletions(-)
    
    diff --git a/src/apps/drivesetup/Jamfile b/src/apps/drivesetup/Jamfile
    index cb003fa..086ac1f 100644
    a b Preference DriveSetup :  
    1515    PartitionList.cpp
    1616    Support.cpp
    1717
    18     : be localestub libcolumnlistview.a libshared.a
     18    : be localestub tracker libcolumnlistview.a libshared.a
    1919        $(TARGET_LIBSUPC++)
    2020    : DriveSetup.rdef
    2121;
  • src/apps/drivesetup/MainWindow.cpp

    diff --git a/src/apps/drivesetup/MainWindow.cpp b/src/apps/drivesetup/MainWindow.cpp
    index 30b6c8f..bb28bb1 100644
    a b  
    1313
    1414#include "MainWindow.h"
    1515
     16#include <errno.h>
    1617#include <stdio.h>
    1718#include <string.h>
    1819
    enum {  
    6465    MSG_EJECT                   = 'ejct',
    6566    MSG_SURFACE_TEST            = 'sfct',
    6667    MSG_RESCAN                  = 'rscn',
     68    MSG_REGISTER                = 'regi',
     69    MSG_UNREGISTER              = 'unrg',
    6770
    6871    MSG_PARTITION_ROW_SELECTED  = 'prsl',
    6972};
    MainWindow::MainWindow()  
    232235    fMountAllMenuItem = new BMenuItem(B_TRANSLATE("Mount all"),
    233236        new BMessage(MSG_MOUNT_ALL), 'M', B_SHIFT_KEY);
    234237
     238    fRegisterMenuItem = new BMenuItem(
     239        B_TRANSLATE("Register disk image" B_UTF8_ELLIPSIS),
     240        new BMessage(MSG_REGISTER));
     241    fUnRegisterMenuItem = new BMenuItem(
     242        B_TRANSLATE("Unregister disk image"),
     243        new BMessage(MSG_UNREGISTER));
     244
    235245    // Disk menu
    236246    fDiskMenu = new BMenu(B_TRANSLATE("Disk"));
    237247
    MainWindow::MainWindow()  
    267277    fPartitionMenu->AddItem(fMountAllMenuItem);
    268278    menuBar->AddItem(fPartitionMenu);
    269279
     280    // Disk image menu
     281    fDiskImageMenu = new BMenu(B_TRANSLATE("Disk image"));
     282
     283    fDiskImageMenu->AddItem(fRegisterMenuItem);
     284    fDiskImageMenu->AddItem(fUnRegisterMenuItem);
     285
     286    menuBar->AddItem(fDiskImageMenu);
     287
    270288    AddChild(menuBar);
    271289
    272290    // add DiskView
    MainWindow::~MainWindow()  
    311329}
    312330
    313331
     332static status_t
     333register_file_disk_device(const char* fileName)
     334{
     335    BString message;
     336
     337    struct stat st;
     338    if (lstat(fileName, &st) != 0) {
     339        status_t error = errno;
     340
     341        message.SetTo(B_TRANSLATE("Failed to stat() %s"));
     342        message.ReplaceFirst("%s", fileName);
     343        BAlert* alert = new BAlert("error", message.String(), B_TRANSLATE("OK"), NULL, NULL,
     344            B_WIDTH_FROM_WIDEST, B_STOP_ALERT);
     345        alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
     346        alert->Go(NULL);
     347        return error;
     348    }
     349
     350    if (!S_ISREG(st.st_mode)) {
     351        message.SetTo(B_TRANSLATE("%s is not a regular file."));
     352        message.ReplaceFirst("%s", fileName);
     353        BAlert* alert = new BAlert("error", message.String(), B_TRANSLATE("OK"), NULL, NULL,
     354            B_WIDTH_FROM_WIDEST, B_STOP_ALERT);
     355        alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
     356        alert->Go(NULL);
     357        return B_BAD_VALUE;
     358    }
     359
     360    // register the file
     361    BDiskDeviceRoster roster;
     362    partition_id id = roster.RegisterFileDevice(fileName);
     363    if (id < 0) {
     364        message.SetTo(B_TRANSLATE("Failed to register file disk device: %s"));
     365        message.ReplaceFirst("%s", fileName);
     366        BAlert* alert = new BAlert("error", message.String(), B_TRANSLATE("OK"), NULL, NULL,
     367            B_WIDTH_FROM_WIDEST, B_STOP_ALERT);
     368        alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
     369        alert->Go(NULL);
     370        return id;
     371    }
     372
     373    // print the success message (get the device path)
     374    BDiskDevice device;
     375    BPath path;
     376    if (roster.GetDeviceWithID(id, &device) == B_OK
     377        && device.GetPath(&path) == B_OK) {
     378    } else {
     379        message.SetTo(B_TRANSLATE("Registered file as disk device, but failed to get the device path."));
     380        BAlert* alert = new BAlert("error", message.String(), B_TRANSLATE("OK"), NULL, NULL,
     381            B_WIDTH_FROM_WIDEST, B_WARNING_ALERT);
     382        alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
     383        alert->Go(NULL);
     384    }
     385
     386    return B_OK;
     387}
     388
     389
     390static status_t
     391unregister_file_disk_device(const char* fileNameOrID)
     392{
     393    BString message;
     394
     395    // try to parse the parameter as ID
     396    char* numberEnd;
     397    partition_id id = strtol(fileNameOrID, &numberEnd, 0);
     398    BDiskDeviceRoster roster;
     399    if (id >= 0 && numberEnd != fileNameOrID && *numberEnd == '\0') {
     400        BDiskDevice device;
     401        if (roster.GetDeviceWithID(id, &device) == B_OK && device.IsFile()) {
     402            status_t error = roster.UnregisterFileDevice(id);
     403            if (error != B_OK) {
     404                message.SetTo(B_TRANSLATE("Failed to unregister file disk device."));
     405                BAlert* alert = new BAlert("error", message.String(), B_TRANSLATE("OK"), NULL, NULL,
     406                    B_WIDTH_FROM_WIDEST, B_STOP_ALERT);
     407                alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
     408                alert->Go(NULL);
     409                return error;
     410            }
     411            return B_OK;
     412        } else {
     413            message.SetTo(B_TRANSLATE("No file disk device found with the given ID, trying file %s"));
     414            message.ReplaceFirst("%s", fileNameOrID);
     415            BAlert* alert = new BAlert("error", message.String(), B_TRANSLATE("OK"), NULL, NULL,
     416                B_WIDTH_FROM_WIDEST, B_WARNING_ALERT);
     417            alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
     418            alert->Go(NULL);
     419        }
     420    }
     421
     422    // the parameter must be a file name -- stat() it
     423    struct stat st;
     424    if (lstat(fileNameOrID, &st) != 0) {
     425        status_t error = errno;
     426        message.SetTo(B_TRANSLATE("Failed to stat() %s"));
     427        message.ReplaceFirst("%s", fileNameOrID);
     428        BAlert* alert = new BAlert("error", message.String(), B_TRANSLATE("OK"), NULL, NULL,
     429            B_WIDTH_FROM_WIDEST, B_STOP_ALERT);
     430        alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
     431        alert->Go(NULL);
     432        return error;
     433    }
     434
     435    // remember the volume and node ID, so we can identify the file
     436    // NOTE: There's a race condition -- we would need to open the file and
     437    // keep it open to avoid it.
     438    dev_t volumeID = st.st_dev;
     439    ino_t nodeID = st.st_ino;
     440
     441    // iterate through all file disk devices and try to find a match
     442    BDiskDevice device;
     443    while (roster.GetNextDevice(&device) == B_OK) {
     444        if (!device.IsFile())
     445            continue;
     446
     447        // get file path and stat it, same for the device path
     448        BPath path;
     449        bool isFilePath = true;
     450        if ((device.GetFilePath(&path) == B_OK && lstat(path.Path(), &st) == 0
     451                && volumeID == st.st_dev && nodeID == st.st_ino)
     452            || (isFilePath = false, false)
     453            || (device.GetPath(&path) == B_OK && lstat(path.Path(), &st) == 0
     454                && volumeID == st.st_dev && nodeID == st.st_ino)) {
     455            status_t error = roster.UnregisterFileDevice(device.ID());
     456            if (error != B_OK) {
     457                message.SetTo(B_TRANSLATE("Failed to unregister file disk device %s"));
     458                message.ReplaceFirst("%s", fileNameOrID);
     459                BAlert* alert = new BAlert("error", message.String(), B_TRANSLATE("OK"), NULL, NULL,
     460                    B_WIDTH_FROM_WIDEST, B_STOP_ALERT);
     461                alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
     462                alert->Go(NULL);
     463                return error;
     464            }
     465            return B_OK;
     466        }
     467    }
     468
     469    message.SetTo(B_TRANSLATE("%s does not refer to a file disk device."));
     470    message.ReplaceFirst("%s", fileNameOrID);
     471    BAlert* alert = new BAlert("error", message.String(), B_TRANSLATE("OK"), NULL, NULL,
     472        B_WIDTH_FROM_WIDEST, B_WARNING_ALERT);
     473    alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
     474    alert->Go(NULL);
     475    return B_BAD_VALUE;
     476}
     477
     478
    314479void
    315480MainWindow::MessageReceived(BMessage* message)
    316481{
    MainWindow::MessageReceived(BMessage* message)  
    385550            }
    386551            break;
    387552        }
    388 
     553        case MSG_REGISTER:
     554        {
     555            if (fFileOpen == NULL) fFileOpen = new BFilePanel();
     556            fFileOpen->SetTarget(BMessenger(this));
     557            fFileOpen->Show();
     558            break;
     559        }
     560        case MSG_UNREGISTER:
     561        {
     562            BPath path;
     563            fCurrentDisk->GetPath(&path);
     564            unregister_file_disk_device(path.Path());
     565            break;
     566        }
     567        case B_REFS_RECEIVED:
     568        {
     569            entry_ref entryRef;
     570            message->FindRef("refs", &entryRef);
     571            BEntry entry(&entryRef);
     572            BPath path;
     573            if (entry.GetPath(&path) == B_OK) {
     574                register_file_disk_device(path.Path());
     575            }
     576            break;
     577        }
    389578        default:
    390579            BWindow::MessageReceived(message);
    391580            break;
    MainWindow::_UpdateMenus(BDiskDevice* disk,  
    586775        fWipeMenuItem->SetEnabled(false);
    587776        fEjectMenuItem->SetEnabled(false);
    588777        fSurfaceTestMenuItem->SetEnabled(false);
     778        fUnRegisterMenuItem->SetEnabled(false);
    589779    } else {
    590780//      fWipeMenuItem->SetEnabled(true);
    591781        fWipeMenuItem->SetEnabled(false);
    592782        fEjectMenuItem->SetEnabled(disk->IsRemovableMedia());
    593783//      fSurfaceTestMenuItem->SetEnabled(true);
    594784        fSurfaceTestMenuItem->SetEnabled(false);
     785        fUnRegisterMenuItem->SetEnabled(true);
    595786
    596787        // Create menu and items
    597788        BPartition* parentPartition = NULL;
  • src/apps/drivesetup/MainWindow.h

    diff --git a/src/apps/drivesetup/MainWindow.h b/src/apps/drivesetup/MainWindow.h
    index dbf3f59..34670fc 100644
    a b  
    1111
    1212
    1313#include <DiskDeviceRoster.h>
     14#include <FilePanel.h>
    1415#include <Window.h>
    1516
    1617#include "Support.h"
    private:  
    8687
    8788            BMenu*              fDiskMenu;
    8889            BMenu*              fDiskInitMenu;
     90            BMenu*              fDiskImageMenu;
    8991
    9092            BMenu*              fPartitionMenu;
    9193            BMenu*              fFormatMenu;
    private:  
    101103            BMenuItem*          fMountMenuItem;
    102104            BMenuItem*          fUnmountMenuItem;
    103105            BMenuItem*          fMountAllMenuItem;
     106            BMenuItem*          fRegisterMenuItem;
     107            BMenuItem*          fUnRegisterMenuItem;
     108
     109            BFilePanel*         fFileOpen;
    104110};
    105111
    106112