Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set sw=2 ts=8 et tw=80 : */ |
michael@0 | 3 | |
michael@0 | 4 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 7 | |
michael@0 | 8 | #include "mozilla/net/RemoteOpenFileParent.h" |
michael@0 | 9 | #include "mozilla/unused.h" |
michael@0 | 10 | #include "nsEscape.h" |
michael@0 | 11 | |
michael@0 | 12 | #if !defined(XP_WIN) && !defined(MOZ_WIDGET_COCOA) |
michael@0 | 13 | #include <fcntl.h> |
michael@0 | 14 | #include <unistd.h> |
michael@0 | 15 | #endif |
michael@0 | 16 | |
michael@0 | 17 | namespace mozilla { |
michael@0 | 18 | namespace net { |
michael@0 | 19 | |
michael@0 | 20 | bool |
michael@0 | 21 | RemoteOpenFileParent::OpenSendCloseDelete() |
michael@0 | 22 | { |
michael@0 | 23 | #if defined(XP_WIN) || defined(MOZ_WIDGET_COCOA) |
michael@0 | 24 | MOZ_CRASH("OS X and Windows shouldn't be doing IPDL here"); |
michael@0 | 25 | #else |
michael@0 | 26 | |
michael@0 | 27 | // TODO: make this async! |
michael@0 | 28 | |
michael@0 | 29 | FileDescriptor fileDescriptor; |
michael@0 | 30 | |
michael@0 | 31 | nsAutoCString path; |
michael@0 | 32 | nsresult rv = mURI->GetFilePath(path); |
michael@0 | 33 | NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "GetFilePath failed!"); |
michael@0 | 34 | |
michael@0 | 35 | NS_UnescapeURL(path); |
michael@0 | 36 | |
michael@0 | 37 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 38 | int fd = open(path.get(), O_RDONLY); |
michael@0 | 39 | if (fd == -1) { |
michael@0 | 40 | printf_stderr("RemoteOpenFileParent: file '%s' was not found!\n", |
michael@0 | 41 | path.get()); |
michael@0 | 42 | } else { |
michael@0 | 43 | fileDescriptor = FileDescriptor(fd); |
michael@0 | 44 | } |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | // Sending a potentially invalid file descriptor is just fine. |
michael@0 | 48 | unused << Send__delete__(this, fileDescriptor); |
michael@0 | 49 | |
michael@0 | 50 | if (fileDescriptor.IsValid()) { |
michael@0 | 51 | // close file now that other process has it open, else we'll leak fds in the |
michael@0 | 52 | // parent process. |
michael@0 | 53 | close(fileDescriptor.PlatformHandle()); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | #endif // OS_TYPE |
michael@0 | 57 | |
michael@0 | 58 | return true; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | } // namespace net |
michael@0 | 62 | } // namespace mozilla |