"Temp" directory in Windows, IO.getFile - firefox-addon

I would like to write to the C:\windows\temp directory (or it's configured equivalent) inside my Firefox-addon.
https://developer.mozilla.org/en/FileGuide/FileWriting
Gives the impression that there are system independent names for these paths:
var file = IO.getFile("Desktop", "myinfo.txt");
var stream = IO.newOutputStream(file, "text");
stream.writeString("This is some text");
stream.close();
But I can't find any reference in the specified references, as to what "Desktop" points to. So that leaves me not knowing what exactly is referred to in the names given by the documentation.
How to I use IO.getFile() to open a file in the windows global temp folder?

See also Code snippets: File I/O on developer.mozilla.org. It answers your question (Matthew is right, it's "TmpD"), and provides many other file-related examples.
[edit] Oh, and does IO actually work for you? I thought it was unavailable. [edit2] I added a warning at the top of the pages I could find, that mention it.

The keys are described here.
I believe you want TmpD, which is listed here

// Writing stackoverflow.txt to TEMP dir
const { OS } = Cu.import("resource://gre/modules/osfile.jsm", {})
const path = OS.Path.join(OS.Constants.Path.tmpDir, "stackoverflow.txt")
OS.File.writeAtomic(path, "Hello, StackOverflow!", {
encoding: "utf-8",
tmpPath: "stackoverflow.txt.tmp", // it's not necessary but I'd recommend to use it
}).then(() => console.log(path, "has been written"))
// C:\Users\traxium\AppData\Local\Temp\stackoverflow.txt has been written
// Reading stackoverflow.txt from TEMP dir
const { OS } = Cu.import("resource://gre/modules/osfile.jsm", {})
const path = OS.Path.join(OS.Constants.Path.tmpDir, "stackoverflow.txt")
OS.File.read(path, { encoding: "utf-8" }).then(txt => console.log(txt))
// "Hello, StackOverflow!"

Related

Get the name of file(s) within last directory and the full directory path using Swift

I am trying to obtain the name of a file (JSON format but saved without an extension) within the last directory of a given path. Each file is saved with its own unique subpath inside the app's data container.
I also need to get the full path of the file, including the filename.
From what I've read, I believe it is better to use URLs to do this rather than using string paths.
I have tried the following code:
do {
let enumerator = FileManager.default.enumerator(at: filePath, includingPropertiesForKeys: nil)
while let element = enumerator?.nextObject() as? URL {
var nexObject = element.lastPathComponent
print(nextObject)
}
} catch let error {
print(error.localizedDescription)
}
This does seem to iterate through each level of the path until the end. Great, but what is the best way to get the full path, including the filename, other than concatenation of each object from the above?
All advice gratiously received. Thanks!
As element is an URL, if you're interested in the full path name rather than the last component, just go for:
var nextObject = element.absoluteURL // instead of .lastPathComponent
or just
var nextObject = element.path // or even relativePath
Thank you, #Christophe (+1)
I've also since spotted that the documentation for enumerator(at:includingPropertiesForKeys:options:errorHandler:) provides a nice example, which can be modifed for my purposes by using additional resource keys (e.g. name, path, etc.).

Require json file dynamically in react-native (from thousands of files)

I googled so far and tried to find out the solution but not yet.
I know require() works only with static path, so I want alternative ways to solve my problem. I found this answer here but it doesnt make sense for thousands of resources.
Please advise me the best approach to handle such case.
Background
I have thousand of json files that containing app data, and declared all the file path dynamically like below:
export var SRC_PATH = {
bible_version_inv: {
"kjv-ot": "data/bibles/Bible_KJV_OT_%s.txt",
"kjv-nt": "data/bibles/Bible_KJV_NT_%s.txt",
"lct-ot": "data/bibles/Bible_LCT_OT_%s.txt",
"lct-nt": "data/bibles/Bible_LCT_NT_%s.txt",
"leb": "data/bibles/leb_%s.txt",
"net": "data/bibles/net_%s.txt",
"bhs": "data/bibles/bhs_%s.txt",
"n1904": "data/bibles/na_%s.txt",
.....
"esv": "data/bibles/esv_%s.txt",
.....
},
....
As you can see, file path contains '%s' and that should be replace with right string depends on what the user selected.
For example if user select the bible (abbreviation: "kjv-ot") and the chapter 1 then the file named "data/bibles/Bible_KJV_OT_01.txt" should be imported.
I'm not good enough in react-native, just wondering if there is other alternative way to handle those thousands of resource files and require only one at a time by dynamically following the user's selection.
Any suggestions please.
Instead of exporting a flat file, you could export a function that took a parameter which would help build out the paths like this:
// fileInclude.js
export const generateSourcePath = (sub) => {
return {
bible_version_inv: {
"kjv-ot": `data/bibles/Bible_KJV_OT_${sub}.txt`
}
}
}
//usingFile.js
const generation = require('./fileInclude.js');
const myFile = generation.generateSourcePath('mySub');
const requiredFile = require(myFile);
then you would import (or require) this item into your project, execute generateSourcePath('mysub') to get all your paths.

Can't get Dart asset_pack package example code to run correctly

So I am trying to make a game in Dart and I decided to check out this asset_pack package. I tried to test the example code (below) but asset.imported prints null..why is this?
I did create /web/test/foo.txt in my project folder and put a little bit of text in it but still I get null printed.
main() {
// Construct a new AssetManager.
AssetManager assets = new AssetManager();
// Register the 'test' pack. No url is needed so the empty string suffices.
AssetPack testPack = assets.registerPack('test', '');
// Register asset 'foo' and load it's contents from 'foo.txt'.
// The asset type is 'text' and there are no arguments for the loader
// or importer.
Future<Asset> futureAsset = testPack.loadAndRegisterAsset('foo', 'foo.txt',
'text', {}, {})
futureAsset.then((asset) {
// Print the contents of foo.txt.
print(asset.imported);
});
}
I think your file has to be in the web/ folder or you have to load test/foo.txt. If you run the example you should see an error that the file was not found in the developer console. This code seems to work:
Future<Asset> futureAsset = testPack.loadAndRegisterAsset('foo', 'text',
'test/foo.txt', {}, {})

What's the recommended way to copy multiple dotfiles with yeoman?

I am building a yeoman generator for a fairly typical node app:
/
|--package.json
|--.gitignore
|--.travis.yml
|--README.md
|--app/
|--index.js
|--models
|--views
|--controllers
In the templates folder of my yeoman generator, I have to rename the dotfiles (and the package.json) to prevent them from being processed as part of the generator:
templates/
|--_package.json
|--_gitignore
|--_travis.yml
|--README.md
|--app/
|--index.js
|--models
|--views
|--controllers
I see a lot of generators that copy dotfiles individually manually:
this.copy('_package.json', 'package.json')
this.copy('_gitignore', '.gitignore')
this.copy('_gitattributes', '.gitattributes')
I think it's a pain to manually change my generator code when I add new template files. I would like to automatically copy all files in the /templates folder, and rename the ones that are prefixed with _.
What's the best way to do this?
If I were to describe my intention in imaginary regex, this is what it would look like:
this.copy(/^_(.*)/, '.$1')
ths.copy(/^[^_]/)
EDIT
This is the best I can manage:
this.expandFiles('**', { cwd: this.sourceRoot() }).map(function() {
this.copy file, file.replace(/^_/, '.')
}, this);
I found this question through Google as I was looking for the solution, and then I figured it out myself.
Using the new fs API, you can use globs!
// Copy all non-dotfiles
this.fs.copy(
this.templatePath('static/**/*'),
this.destinationRoot()
);
// Copy all dotfiles
this.fs.copy(
this.templatePath('static/.*'),
this.destinationRoot()
);
Adding to #callumacrae 's answer: you can also define dot: true in the globOptions of copy(). That way a /** glob will include dotfiles. Example:
this.fs.copy(
this.templatePath('files/**'),
this.destinationPath('client'),
{ globOptions: { dot: true } }
);
A list of available Glob options can be found in the README of node-glob.
Just got this working for me: the globOptions needs to be in the fifth argument:
this.fs.copyTpl(
this.templatePath('sometemplate/**/*'),
this.destinationPath(this.destinationRoot()),
null,
null,
{ globOptions: { dot: true } }
);
If you don't want to use templates that starts with a dot, you can use the dive module to achieve something identical:
var templatePath = this.templatePath('static-dotfiles');
var destinationRoot = this.destinationRoot();
dive(templatePath, {all: true}, function (err, file, stat) {
if (err) throw err;
this.fs.copy(
file,
(destinationRoot + path.sep + path.relative(templatePath, file))
.replace(path.sep + '_', path.sep + '.')
);
}.bind(this));
where static-dotfiles is the name of your template folder for dotfiles where _ replaces . in filenames (ex: _gitignore).
Don't forget to add a requirement to dive at the top of your generator with
var dive = require('dive');
Of course, this also works for copyTpl.
Note that all subparts of paths that starts with a _ will be replaced by a . (ex: static-dotfiles/_config/_gitignore will be generated as .config/.gitignore)

How to avoid "Path Manipulation" error in Fortify? [duplicate]

I have path Manipulation problem. The following code is placed in Page_load method of ASPx page.
String rName = Request.QueryString["reportName"];
string path = "C:\\hari" + rName;
if (File.Exists(path))
{
File.Delete(path);
}
But Fortify scan report for the above sample code shows ‘Path Manipulation’ issue as high
Need help to modify above code so that it can pass fortify scan
Jackson is right, this is a direct File Path Manipulation vulnerability that can be fixed through indirect selection.
From your known directory, list all the files. Use the value coming from your own directory list, not the user-supplied value.
String rName = Request.QueryString["reportName"];
String knownPath = "C:\\hari";
DirectoryInfo di = new DirectoryInfo(knownPath);
FileInfo[] files = di.GetFiles(rName);
if (files.length > 0)
{
files[0].Delete();
}
I think the problem is that someone could spoof a request with reportName = "..\\Windows\\Something important" which is clearly a security flaw. You need to change your code so that it doesn't read a partial filename from the request query string.

Resources