Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
zh99998 committed Nov 5, 2016
1 parent 4293e84 commit 3a9953d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
File renamed without changes.
4 changes: 2 additions & 2 deletions dist/index.js

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions src/bin/LICENSE.applet.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Joran Dirk Greef

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
51 changes: 42 additions & 9 deletions src/lib/sudoer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import {tmpdir} from 'os';
import {watchFile, unwatchFile, unlink, createReadStream, createWriteStream} from 'fs';
import {watchFile, unwatchFile, unlink, createReadStream, createWriteStream, readdir, rmdir, stat as fs_stat, chmod} from 'fs';
import {normalize, join, dirname} from 'path';
import {createHash} from 'crypto';
import {promisify} from "bluebird";

import {readFile, writeFile, exec, spawn, mkdir, stat} from '~/lib/utils';

const readdirAsync = promisify(readdir);
const unlinkAsync = promisify(unlink);
const rmdirAsync = promisify(rmdir);
const statAsync = promisify(fs_stat);
const mkdirAsync = promisify(mkdir);
const chmodAsync = promisify(chmod);

let {platform, env} = process;


Expand Down Expand Up @@ -62,17 +70,42 @@ class SudoerUnix extends Sudoer {
}

async copy(source, target) {
return new Promise(async (resolve, reject) => {
source = this.escapeDoubleQuotes(normalize(source));
target = this.escapeDoubleQuotes(normalize(target));
let stats = await statAsync(source);
let mode = 0o755; // add execute permission, seems asar package will lose permission information.
if (stats.isDirectory()) {
try {
let result = await exec(`/bin/cp -R -p "${source}" "${target}"`);
resolve(result);
let stats = await statAsync(target);
if (stats.isDirectory()) {
await chmodAsync(target, mode);
} else {
await unlinkAsync(target);
await mkdirAsync(target, mode);
}
} catch (error) {
await mkdirAsync(target, mode);
}
catch (err) {
reject(err);
for (let file of await readdirAsync(source)) {
await this.copy(join(source, file), join(target, file))
}
});
} else {
try {
let stats = await statAsync(target);
if (stats.isDirectory()) {
await rmdirAsync(target);
} else {
await chmodAsync(target, mode);
}
} catch (error) {
}
return new Promise((resolve, reject)=> {
let readable = createReadStream(source);
readable.on("error", reject);
let writable = createWriteStream(target, {mode: mode});
writable.on("error", reject);
writable.on("close", resolve);
readable.pipe(writable);
})
}
}

async remove(target) {
Expand Down

0 comments on commit 3a9953d

Please sign in to comment.