delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2018/01/23/16:13:39

X-Recipient: archive-cygwin AT delorie DOT com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:reply-to:subject:to:references:from:message-id
:date:mime-version:in-reply-to:content-type
:content-transfer-encoding; q=dns; s=default; b=KRADKd6C094+9TxC
XQ02U2fs1oMuks0ycZNmsHwmMr0oQwhWHLjF4lA5O7ByW6buY01/4MCZ6I95sGJG
FJKbQfuMKrj+9wqiboMIrZcxVkrvmhne+KYnvrn6MxbvHYuYZvrCT2gswDpnTgx9
Pl5zDjh0WVcrM04NaFVWjOXDkbo=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:reply-to:subject:to:references:from:message-id
:date:mime-version:in-reply-to:content-type
:content-transfer-encoding; s=default; bh=Vd+jvay8HOtBMSYzp7wS12
6Npkc=; b=O2KUWLobIUaNcEmb7FlMiJwDD/GUq+bfSCH6pbRUSSiv4oNn+5neoE
z7CQ4JepQW/fIxDD8t5lCzxhSfbezub8BFPmqj/t5dDueTnvP7ag68QcYyN5EkO9
UXjwlVyMVAOMOq/mKAl82ubTh9uVWrUA9USCmsx3JRtfMQGqRmwtY=
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Authentication-Results: sourceware.org; auth=none
X-Virus-Found: No
X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL,BAYES_00,KAM_LAZY_DOMAIN_SECURITY,RCVD_IN_DNSWL_LOW autolearn=no version=3.3.2 spammy=clicking, vintage, DOS, fischer
X-HELO: smtp-out-no.shaw.ca
X-Authority-Analysis: v=2.3 cv=I59Luuog c=1 sm=1 tr=0 a=MVEHjbUiAHxQW0jfcDq5EA==:117 a=MVEHjbUiAHxQW0jfcDq5EA==:17 a=N659UExz7-8A:10 a=cYCMvJSPAAAA:8 a=T1n7gGMsGpWxAFVXtnMA:9 a=pILNOxqGKmIA:10 a=0cYAWHTMCJUZ_s7sn94O:22
Reply-To: Brian DOT Inglis AT SystematicSw DOT ab DOT ca
Subject: Re: Ejecting a USB drive using Cygwin (sync)?
To: cygwin AT cygwin DOT com
References: <1516706057 DOT 3270332 DOT 1245015392 DOT 5FE803A3 AT webmail DOT messagingengine DOT com>
From: Brian Inglis <Brian DOT Inglis AT SystematicSw DOT ab DOT ca>
Message-ID: <ca5d930a-326c-51a2-c82b-db8efcb0a2bc@SystematicSw.ab.ca>
Date: Tue, 23 Jan 2018 14:13:24 -0700
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2
MIME-Version: 1.0
In-Reply-To: <1516706057.3270332.1245015392.5FE803A3@webmail.messagingengine.com>
X-CMAE-Envelope: MS4wfObFaZGwX1P7W3HvUIJIKwXCgpttkjm/KAYTi4Tq8/Cs7ifNj4HSN/Q0DG4BqxRhDEQL4InPbV05YxgsyfRxpDqMfeWSJdRmp+Lrlh5BO9WYuf469Ydr BTE8KFECih1P5CfKHKoX/1ggQQNPraekb2S928egJnRoUO4b60W7LenqsNa26ariw0dctTBdP9WeyA==
X-IsSubscribed: yes

On 2018-01-23 04:14, Ronald Fischer wrote:
> I'm looking for a command, which would allow me from a shell script to 
> prepare the removal of a USB device (stick, external hard drive etc.). With 
> other words, after issuing the command, I should be able to physically remove
> the USB device.
> Can the `sync` command be used, for instance
>     sync -f /cygdrive/e
> assuming that the USB device is on drive E:? The man page of *sync* is a bit 
> vague in this respect. Or is there another Cygwin command which can be used
> for this purpose?

Running sync as above, and umount are always advisable.
Some ejection utilities detach the device, leaving it unusable until the device
is attached by forcing enumeration, or the system restarted.
I found the following utility works well without elevation - Windows code from
http://www.leapsecond.com/tools/eject.{c,exe}:

// eject -- Allow safe removal of USB thumb drive.
// - Command line tool to flush/dismount/eject USB drive.
// - Simpler than mouse clicking through taskbar "Safely Remove Hardware" icon.
// - Usable in batch file scripts.
// 02-Nov-2012 Tom Van Baak (tvb) www.LeapSecond.com/tools
...
// Per MSDN, follow procedure for safe removal of USB drive.
int drive_eject (char *drive)
{
    HANDLE hDev;
    // Convert vintage DOS drive letter to weird Windows object pathname.
    char path[10];
    sprintf(path, "\\\\.\\%s", drive);
    // Open (with write, but no lock) to flush pending writes.
    DEV_OPEN(path, GENERIC_READ | GENERIC_WRITE);
    FlushFileBuffers(hDev);
    CloseHandle(hDev);
    // Open (with read, and lock) to dismount and eject.
    DEV_OPEN(path, GENERIC_READ);
    DEV_IOCTL(FSCTL_LOCK_VOLUME);
    DEV_IOCTL(FSCTL_DISMOUNT_VOLUME);
    DEV_IOCTL(IOCTL_STORAGE_EJECT_MEDIA);
    DEV_IOCTL(FSCTL_UNLOCK_VOLUME);
    CloseHandle(hDev);
    return 0;
}

Cygwin does not appear to use ioctls that perform the same Windows functions
except for floppy lock volume.

-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019