Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-3998

It's trivial to make the new "edit" function work on Windows

    • Type: Icon: New Feature New Feature
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.1.0
    • Affects Version/s: None
    • Component/s: Shell
    • Environment:

      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";
      sb << "/tmp/mongo_edit" << time(0)+i << ".js";

      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

      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

            tad Tad Marshall
            tad Tad Marshall
            0 Vote for this issue
            0 Start watching this issue