From f585f531446654c93c9df38da3b374751a5b38ea Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 11 Feb 2014 14:57:51 +0800 Subject: [PATCH 1/4] Upgrade to runas@0.4.0 --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 4c1733c42..cd96407dc 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "pegjs": "0.8.0", "property-accessors": "1.x", "q": "1.0.x", + "runas": "0.4.0", "scandal": "0.14.0", "season": "1.x", "semver": "1.1.4", From 84ee94dfd129385759015ec26ba174eca26a8b58 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 11 Feb 2014 14:58:15 +0800 Subject: [PATCH 2/4] Support privilege escalation when writing file. --- src/file.coffee | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/file.coffee b/src/file.coffee index beedf73f8..7e6242942 100644 --- a/src/file.coffee +++ b/src/file.coffee @@ -5,6 +5,7 @@ Q = require 'q' {Emitter} = require 'emissary' _ = require 'underscore-plus' fs = require 'fs-plus' +runas = require 'runas' # Public: Represents an individual file. # @@ -61,7 +62,7 @@ class File write: (text) -> previouslyExisted = @exists() @cachedContents = text - fs.writeFileSync(@getPath(), text) + @writeFileWithPrivilegeEscalationSync(@getPath(), text) @subscribeToNativeChangeEvents() if not previouslyExisted and @hasSubscriptions() # Deprecated @@ -122,6 +123,21 @@ class File getDigest: -> @digest ? @setDigest(@readSync()) + # Private: Writes the text to specified path. + # + # Privilege escalation would be asked when current user doesn't have + # permission to the path. + writeFileWithPrivilegeEscalationSync: (path, text) -> + try + fs.writeFileSync(path, text) + catch error + if error.code is 'EACCES' and process.platform is 'darwin' + authopen = '/usr/libexec/authopen' # man 1 auth open + unless runas(authopen, ['-w', '-c', path], stdin: text) is 0 + throw error + else + throw error + handleNativeChangeEvent: (eventType, path) -> if eventType is "delete" @unsubscribeFromNativeChangeEvents() From d0fe2c9a5ba220ced52e5d10bd8d446373268585 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 11 Feb 2014 15:01:14 +0800 Subject: [PATCH 3/4] Cache content after file is written successfully. --- src/file.coffee | 4 ++-- src/text-buffer.coffee | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/file.coffee b/src/file.coffee index 7e6242942..7ca86a821 100644 --- a/src/file.coffee +++ b/src/file.coffee @@ -61,8 +61,8 @@ class File # Public: Overwrites the file with the given String. write: (text) -> previouslyExisted = @exists() - @cachedContents = text @writeFileWithPrivilegeEscalationSync(@getPath(), text) + @cachedContents = text @subscribeToNativeChangeEvents() if not previouslyExisted and @hasSubscriptions() # Deprecated @@ -132,7 +132,7 @@ class File fs.writeFileSync(path, text) catch error if error.code is 'EACCES' and process.platform is 'darwin' - authopen = '/usr/libexec/authopen' # man 1 auth open + authopen = '/usr/libexec/authopen' # man 1 authopen unless runas(authopen, ['-w', '-c', path], stdin: text) is 0 throw error else diff --git a/src/text-buffer.coffee b/src/text-buffer.coffee index 044f89946..e58ed81de 100644 --- a/src/text-buffer.coffee +++ b/src/text-buffer.coffee @@ -194,8 +194,8 @@ class TextBuffer extends TextBufferCore @emit 'will-be-saved', this @setPath(path) - @cachedDiskContents = @getText() @file.write(@getText()) + @cachedDiskContents = @getText() @emitModifiedStatusChanged(false) @emit 'saved', this From 823a79610f2a9719086b3d47ee25a84646541e8e Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 12 Feb 2014 19:34:29 +0800 Subject: [PATCH 4/4] :lipstick: "Private:" is not needed anymore. --- src/file.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/file.coffee b/src/file.coffee index 7ca86a821..b4fffe060 100644 --- a/src/file.coffee +++ b/src/file.coffee @@ -123,7 +123,7 @@ class File getDigest: -> @digest ? @setDigest(@readSync()) - # Private: Writes the text to specified path. + # Writes the text to specified path. # # Privilege escalation would be asked when current user doesn't have # permission to the path.