Ticket #10141: 0001-Added-image-menu-to-mount-unmount-disk-images-from-D.patch

File 0001-Added-image-menu-to-mount-unmount-disk-images-from-D.patch, 10.2 KB (added by dsjonny, 11 years ago)

Fixed namings and texts.

  • src/apps/drivesetup/Jamfile

    From f22de5c68e4414ac938444de9712d664b0319758 Mon Sep 17 00:00:00 2001
    From: =?UTF-8?q?Dancs=C3=B3=20R=C3=B3bert?= <dancso.robert@d-rendszer.hu>
    Date: Sun, 27 Oct 2013 16:44:15 +0100
    Subject: [PATCH] Added image menu to mount/unmount disk images from
     DriveSetup.
    
    ---
     src/apps/drivesetup/Jamfile        |   2 +-
     src/apps/drivesetup/MainWindow.cpp | 209 +++++++++++++++++++++++++++++++++++++
     src/apps/drivesetup/MainWindow.h   |   7 ++
     3 files changed, 217 insertions(+), 1 deletion(-)
    
    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..74ca366 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        message.SetTo(B_TRANSLATE("Registered file as disk device \"%s\""));
     379        message.ReplaceFirst("%s", path.Path());
     380        BAlert* alert = new BAlert("error", message.String(), B_TRANSLATE("OK"), NULL, NULL,
     381            B_WIDTH_FROM_WIDEST, B_INFO_ALERT);
     382        alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
     383        alert->Go(NULL);
     384    } else {
     385        message.SetTo(B_TRANSLATE("Registered file as disk device, but failed to get the device path."));
     386        BAlert* alert = new BAlert("error", message.String(), B_TRANSLATE("OK"), NULL, NULL,
     387            B_WIDTH_FROM_WIDEST, B_WARNING_ALERT);
     388        alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
     389        alert->Go(NULL);
     390    }
     391
     392    return B_OK;
     393}
     394
     395
     396static status_t
     397unregister_file_disk_device(const char* fileNameOrID)
     398{
     399    BString message;
     400
     401    // try to parse the parameter as ID
     402    char* numberEnd;
     403    partition_id id = strtol(fileNameOrID, &numberEnd, 0);
     404    BDiskDeviceRoster roster;
     405    if (id >= 0 && numberEnd != fileNameOrID && *numberEnd == '\0') {
     406        BDiskDevice device;
     407        if (roster.GetDeviceWithID(id, &device) == B_OK && device.IsFile()) {
     408            status_t error = roster.UnregisterFileDevice(id);
     409            if (error != B_OK) {
     410                message.SetTo(B_TRANSLATE("Failed to unregister file disk device."));
     411                BAlert* alert = new BAlert("error", message.String(), B_TRANSLATE("OK"), NULL, NULL,
     412                    B_WIDTH_FROM_WIDEST, B_STOP_ALERT);
     413                alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
     414                alert->Go(NULL);
     415                return error;
     416            }
     417
     418            message.SetTo(B_TRANSLATE("Unregistered file disk device."));
     419            BAlert* alert = new BAlert("error", message.String(), B_TRANSLATE("OK"), NULL, NULL,
     420                B_WIDTH_FROM_WIDEST, B_INFO_ALERT);
     421            alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
     422            alert->Go(NULL);
     423            return B_OK;
     424        } else {
     425            message.SetTo(B_TRANSLATE("No file disk device found with the given ID, trying file \"%s\""));
     426            message.ReplaceFirst("%s", fileNameOrID);
     427            BAlert* alert = new BAlert("error", message.String(), B_TRANSLATE("OK"), NULL, NULL,
     428                B_WIDTH_FROM_WIDEST, B_WARNING_ALERT);
     429            alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
     430            alert->Go(NULL);
     431        }
     432    }
     433
     434    // the parameter must be a file name -- stat() it
     435    struct stat st;
     436    if (lstat(fileNameOrID, &st) != 0) {
     437        status_t error = errno;
     438        message.SetTo(B_TRANSLATE("Failed to stat() \"%s\""));
     439        message.ReplaceFirst("%s", fileNameOrID);
     440        BAlert* alert = new BAlert("error", message.String(), B_TRANSLATE("OK"), NULL, NULL,
     441            B_WIDTH_FROM_WIDEST, B_STOP_ALERT);
     442        alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
     443        alert->Go(NULL);
     444        return error;
     445    }
     446
     447    // remember the volume and node ID, so we can identify the file
     448    // NOTE: There's a race condition -- we would need to open the file and
     449    // keep it open to avoid it.
     450    dev_t volumeID = st.st_dev;
     451    ino_t nodeID = st.st_ino;
     452
     453    // iterate through all file disk devices and try to find a match
     454    BDiskDevice device;
     455    while (roster.GetNextDevice(&device) == B_OK) {
     456        if (!device.IsFile())
     457            continue;
     458
     459        // get file path and stat it, same for the device path
     460        BPath path;
     461        bool isFilePath = true;
     462        if ((device.GetFilePath(&path) == B_OK && lstat(path.Path(), &st) == 0
     463                && volumeID == st.st_dev && nodeID == st.st_ino)
     464            || (isFilePath = false, false)
     465            || (device.GetPath(&path) == B_OK && lstat(path.Path(), &st) == 0
     466                && volumeID == st.st_dev && nodeID == st.st_ino)) {
     467            status_t error = roster.UnregisterFileDevice(device.ID());
     468            if (error != B_OK) {
     469                message.SetTo(B_TRANSLATE("Failed to unregister file disk device \"%s\""));
     470                message.ReplaceFirst("%s", fileNameOrID);
     471                BAlert* alert = new BAlert("error", message.String(), B_TRANSLATE("OK"), NULL, NULL,
     472                    B_WIDTH_FROM_WIDEST, B_STOP_ALERT);
     473                alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
     474                alert->Go(NULL);
     475                return error;
     476            }
     477
     478            message.SetTo(B_TRANSLATE("Unregistered file disk device \"%s\""));
     479            message.ReplaceFirst("%s", fileNameOrID);
     480            BAlert* alert = new BAlert("error", message.String(), B_TRANSLATE("OK"), NULL, NULL,
     481                B_WIDTH_FROM_WIDEST, B_INFO_ALERT);
     482            alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
     483            alert->Go(NULL);
     484
     485            return B_OK;
     486        }
     487    }
     488
     489    message.SetTo(B_TRANSLATE("\"%s\" does not refer to a file disk device."));
     490    message.ReplaceFirst("%s", fileNameOrID);
     491    BAlert* alert = new BAlert("error", message.String(), B_TRANSLATE("OK"), NULL, NULL,
     492        B_WIDTH_FROM_WIDEST, B_WARNING_ALERT);
     493    alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
     494    alert->Go(NULL);
     495    return B_BAD_VALUE;
     496}
     497
     498
    314499void
    315500MainWindow::MessageReceived(BMessage* message)
    316501{
    MainWindow::MessageReceived(BMessage* message)  
    385570            }
    386571            break;
    387572        }
     573        case MSG_REGISTER: {
     574            fOpen = new BFilePanel();
     575            fOpen->SetTarget(BMessenger(this));
     576            fOpen->Show();
     577            break;
     578        }
     579        case MSG_UNREGISTER: {
     580            BPath path;
     581            fCurrentDisk->GetPath(&path);
     582            unregister_file_disk_device(path.Path());
     583            break;
     584        }
     585        case B_REFS_RECEIVED: {
     586            entry_ref entryRef;
     587            message->FindRef("refs", &entryRef);
     588            BEntry entry(&entryRef);
     589            BPath path;
     590            if (entry.GetPath(&path) == B_OK) {
     591                register_file_disk_device(path.Path());
     592            }
     593            break;
     594        }
    388595
    389596        default:
    390597            BWindow::MessageReceived(message);
    MainWindow::_UpdateMenus(BDiskDevice* disk,  
    586793        fWipeMenuItem->SetEnabled(false);
    587794        fEjectMenuItem->SetEnabled(false);
    588795        fSurfaceTestMenuItem->SetEnabled(false);
     796        fUnRegisterMenuItem->SetEnabled(false);
    589797    } else {
    590798//      fWipeMenuItem->SetEnabled(true);
    591799        fWipeMenuItem->SetEnabled(false);
    592800        fEjectMenuItem->SetEnabled(disk->IsRemovableMedia());
    593801//      fSurfaceTestMenuItem->SetEnabled(true);
    594802        fSurfaceTestMenuItem->SetEnabled(false);
     803        fUnRegisterMenuItem->SetEnabled(true);
    595804
    596805        // Create menu and items
    597806        BPartition* parentPartition = NULL;
  • src/apps/drivesetup/MainWindow.h

    diff --git a/src/apps/drivesetup/MainWindow.h b/src/apps/drivesetup/MainWindow.h
    index dbf3f59..80768be 100644
    a b  
    1111
    1212
    1313#include <DiskDeviceRoster.h>
     14#include <FilePanel.h>
    1415#include <Window.h>
    1516
    1617#include "Support.h"
    private:  
    8990
    9091            BMenu*              fPartitionMenu;
    9192            BMenu*              fFormatMenu;
     93            BMenu*              fDiskImageMenu;
    9294
    9395            BMenuItem*          fWipeMenuItem;
    9496            BMenuItem*          fEjectMenuItem;
    private:  
    101103            BMenuItem*          fMountMenuItem;
    102104            BMenuItem*          fUnmountMenuItem;
    103105            BMenuItem*          fMountAllMenuItem;
     106
     107            BMenuItem*          fRegisterMenuItem;
     108            BMenuItem*          fUnRegisterMenuItem;
     109
     110            BFilePanel*         fOpen;
    104111};
    105112
    106113