Skip to content

Ability to close the fd of an .asar we're using #3949

Closed

Description

@Ivshti

My problem is:

On Windows, I use my own auto-update framework which is supposed to replace the .asar file of an app. This works fine for Linux, OS X, but it requires a hack for Windows with an external script doing the work, because Electron keeps the .asar file open at all times.

What I'm asking is a way to close any asar files programatically we've used in Electron, therefore allowing to move them/delete them on Windows.

Looking at https://github.com/atom/electron/blob/master/atom/common/lib/asar.coffee, we have a getFD function, so this shouldn't be too hard. It would be even possible to do it only by exposing cachedArchives.

The reason I'm not submitting a PR is that I don't know what's the cleanest way to expose an API like that.

Activity

  1. zcbenz commented on Dec 31, 2015

    @zcbenz
    Contributor

    The cleanest way I can think of is to add a clearCachedAsarArchive method to the process object, which calls destroy() for the archive and clears it from the cachedArchives. So it will unlock the file, and your app will continue to work if you want to read it later.

  2. Ivshti commented on Dec 31, 2015

    @Ivshti
    ContributorAuthor

    @zcbenz This sounds reasonable, but would it destroy all archives? Since reading another file would open it again and work, I don't see anything bad about cleaning all archives, it's OK.

    What concerns me is that .destroy() doesn't seem to call fs.close on the fd (.getFD()). Am I wrong?

  3. zcbenz commented on Dec 31, 2015

    @zcbenz
    Contributor

    @Ivshti You need to add a _close call in Archive::~Archive for Windows, for POSIX the fd is destroyed automatically, I forgot to do the same for Windows.

  4. Ivshti commented on Dec 31, 2015

    @Ivshti
    ContributorAuthor

    Couldn't find a destructor at all... I have to look again apparently

  5. Ivshti commented on Jan 1, 2016

    @Ivshti
    ContributorAuthor

    @zcbenz how does this work?

    https://github.com/atom/electron/blob/0d50e08ed106ae2897d6320966b47ac0357b8da1/atom/common/asar/archive.cc#L117-L132

    Archive::Archive(const base::FilePath& path)
        : path_(path),
          file_(path_, base::File::FLAG_OPEN | base::File::FLAG_READ),
    #if defined(OS_WIN)
          fd_(_open_osfhandle(
                  reinterpret_cast<intptr_t>(file_.GetPlatformFile()), 0)),
    #elif defined(OS_POSIX)
          fd_(file_.GetPlatformFile()),
    #else
          fd_(-1),
    #endif
          header_size_(0) {
    }
    
    Archive::~Archive() {
    }

    So MSDN says

    To close a file opened with _open_osfhandle, call _close. The underlying handle is also closed by a call to _close, so it is not necessary to call the Win32 function CloseHandle on the original handle.
    

    Do I just do this: ?

    Archive::~Archive() {
    #if defined(OS_WIN)
       _close(fd_)
    #endif
    }
  6. zcbenz commented on Jan 4, 2016

    @zcbenz
    Contributor

    Do I just do this: ?

    Yeah.

  7. zcbenz commented on May 6, 2016

    @zcbenz
    Contributor

    I'm closing this since we don't have plan to work on it, feel free publishing a PR if anyone is interested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions