[SERVER-3998] It's trivial to make the new "edit" function work on Windows Created: 02/Oct/11  Updated: 11/Jul/16  Resolved: 27/Nov/11

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: None
Fix Version/s: 2.1.0

Type: New Feature Priority: Minor - P4
Reporter: Tad Marshall Assignee: Tad Marshall
Resolution: Done Votes: 0
Labels: rn
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows


Issue Links:
Related
related to SERVER-3787 'edit' command in shell Closed
Participants:

 Description   

I was curious why the new "edit" function in the shell wasn't supported in Windows, so I tried taking out the #ifndef _WIN32 code that is hiding the function and making minor adjustments for Windows. It took 5 minutes to get it working. Windows isn't crazy about editing a file that is still open by the shell, but notepad let me overwrite the file anyway and I got my edited values back in the shell just fine. I think that closing and reopening the file (at least in Windows) is the better way to go rather than letting the editor operate on a file we still have open. I tweaked the filename generation slightly for Windows:

#ifdef _WIN32
char tempFolder[MAX_PATH];
GetTempPathA(sizeof tempFolder, tempFolder);
sb << tempFolder << "mongo_edit" << time(0)+i << ".js";
#else
sb << "/tmp/mongo_edit" << time(0)+i << ".js";
#endif

and I had to comment out the use of File to do the delete because "fd" is private in the Windows build, so my hack needs more work. The file isn't deleted with my quick hack, but that's easy to fix if we want to enable this feature in Windows.

// just to make sure this gets closed no matter what
#ifndef _WIN32
File holder;
holder.fd = fd; // can't do this in windows because holder.fd is private
#endif

I was thinking that I might need to use ShellExecute() to run the program, but ::system() works fine. No #ifdef required.

StringBuilder sb;
sb << editor << " " << filename;
int ret = ::system(sb.str().c_str());
int systemErrno = errno;

I set Visual Studio to pass --nodb on the command line and EDITOR=notepad.exe as an environment variable and except for the complaint from notepad that "The file is open in another process" it just worked.

MongoDB shell version: 2.1.0-pre-
> f={}
{ }
> edit f // notepad runs, complains about open file, brings up Save As dialog, asks permission to overwrite, then writes file
> f

{ "key" : "value" }

> // it worked



 Comments   
Comment by Tad Marshall [ 27/Nov/11 ]

Fixed by commit 2cad4df74dd544273fc23d0fb69429766e29a446 .

Comment by auto [ 27/Nov/11 ]

Author:

{u'login': u'', u'name': u'Tad Marshall', u'email': u'tad@10gen.com'}

Message: SERVER-3998 – enable the shell's edit command in Windows

Make the mongo shell's 'edit' command work in Windows. Change
the code to use C file stream functions instead of Unix low level
IO (fopen instead of open), close and reopen the file so there are
fewer OS dependencies, delete (remove()) the file ourselves where
needed. Also, remove #ifdefs are are now constant (USE_LINENOISE,
USE_TABCOMPLETION) and the unused code they supported, and try to
get the entire source file to adhere to the kernel code style.
Branch: master
https://github.com/mongodb/mongo/commit/2cad4df74dd544273fc23d0fb69429766e29a446

Generated at Thu Feb 08 03:04:39 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.