Copy zip file and unzip using puppet in windows 10 - puppet

I'm trying to copy one file using puppet in windows.
Code is as below :-
file { 'D:\mycopy\folder\filename':
ensure => present,
mode => '0660',
source => "d:\temp\test.zip",
}
It is giving me below error
Cannot use opaque urls' file:\d: est\test.zip
I want to unzip also after copy. Thanks.

Puppet recognizes both backslashes (\) and forward slashes (/) as path separators in Windows file names. Although Windows shells want the former, the latter is a lot safer to use in Puppet manifests.
In this case, the two appearances of \t in your double-quoted path string will be interpreted as escape sequences representing the tab character. You could solve that issue in this case by doubling the backslashes, by switching to a single-quoted string, or by switching to forward slashes. As a matter of style, I would both switch to single quotes (because there are no variable interpolations or single quotes in the string content) and switch to forward slashes:
However, the actual diagnostic about opaque URLs is a clue to another issue: Puppet accepts URLs as values of File's source parameter, and it is interpreting your absolute filename as a URL with scheme 'd'. To work around this, you should express the source via a file: URL:
source => 'file://d:/temp/test.zip'

Related

Bash: How to delete a multi-line string which may contain special characters from a string/file

I'm writing a script that at one point needs to remove a literal string (no regex matching needed) from a file. Both the string and file path are in script variables.
The problem is that the string is multi-line, and may also contain special characters. Additionally, not all lines in the string are unique in the file (but the string as a whole is), so I cannot go line by line to delete each individual one from the file.
For example, when I echo "$stringToDelete" from my script I get something like:
values(
/the/#quick/[]/"fox",
//jumped/over/the,
//lazy/{},
)
I've tried some approaches using sed and awk but any attempt fails with issues around either the newlines or the special characters. I've seen some answers invoking perl but couldn't get that to work.
Also I can easily read the full file's content into a variable, so the solution doesn't need to edit the file directly - a way to remove the stringToDelete from another string var is fine too.

Why does Rust output escape backslashes on paths in windows? What is best way to remove the extra backslashes from output?

Background
Writing a program that watches a directory and subdirs and alerts user when file(s) in the path are created, updated, edited. Code is shared on github.
I'm new to Rust (about 1 week of experience).
Make Output Path Easy to Use
I want user to be able to easily copy the output path (see pic below) and copy / paste it for use.
When I run the program on Linux it works great with directory separators (/ - forward slash) and the user can easily copy the path and use it.
Problem : Windows Platform Uses Backslashes
The problem is that on Windows platform paths are separated by backslashes (which are also escape chars).
Double-Backslashes
When the program outputs the path on Windows it always shows double backslashes and the path isn't easily copy-able.
Here's a snapshot of the output:
As an example I am using the following output:
println!("{:?}", Path::new("c:\\windows\\"));
The output is from the Path::new() method itself, but it always outputs the path string with the double backslashes.
Can you help me find a better way to format the output so that it has single backslashes like a normal path (which excludes the escape backslashes)?
EDIT
Someone mentioned trying raw string input so I tried the following:
println!("{:?}", Path::new(r"c:\windows\"));
However, the Path::new method still outputs double backslashes.
The Problem is that you are using the Debug formatting to print the path, this causes characters that would need to be escaped in a string literal to be escaped in the output.
Instead you should use the normal Display formatting, though as Path does not directly implement Display you will need to call display on the path to display it.
println!("{}", Path::new("c:\\windows\\").display());
Note that Path::display is not loss-less in case the path contains non-unicode data.

Is there an alternative for the slash in a path?

I have an application which correctly escapes slashes ("/) in file names to avoid path traversal attacks.
The secret file has this path:
/tmp/secret.txt
I want to access this file by uploading a file with a special crafted file name (something like \/tmp\/secret.txt)
Is there any alternative syntax without the slashes which I can use so that Linux will read this file?
(I'm aware of URL encoding but as the escaping is done in the backend this has no use for me.)
No. The / is not allowed in a filename, no matter if it's escaped as \/ or not.
It is one out of only two characters that are not allowed in filenames, the other being \0.
This means that you obviously could use _tmp_secret.txt or -tmp-secret.txt, or replace the / in the path with any other character that you wish, to create a filename with a path "encoded into it". But in doing so, you can not encode pathnames that includes the chosen delimiter character in one or several of its path components and expect to decode it into the original pathname.
This is, by the way, how OpenBSD's ports system encodes filenames for patches to software. In (for example) /usr/ports/shells/fish/patches we find files with names like
patch-share_tools_create_manpage_completions_py
which comes from the pathname of a particular file in the fish shell source distribution (probably share/tools/create_manpage_completions.py). These pathnames are however never parsed, and the encoding is only there to create unique and somewhat intelligible filenames for the patches themselves. The real paths are included in the patch files.

How to rename a folder that contains smart quotes

I have a folder that was created automatically. The user unintentionally provided smart (curly) quotes as part of the name, and the process that sanitizes the inputs did not catch these. As a result, the folder name contains the smart quotes. For example:
this-is-my-folder’s-name-“Bob”
I'm now trying to rename/remove said folder on the command line, and none of the standard tricks for dealing with files/folders with special characters (enclosing in quotes, escaping the characters, trying to rename it by inode, etc.) are working. All result in:
mv: cannot move this-is-my-folder’s-name-“Bob” to this-is-my-folders-name-BOB: No such file or directory
Can anyone provide some advice as to how I can achieve this?
To get the name in a format you can copy-and-paste into your shell:
printf '%q\n' this*
...will print out the filename in a manner the shell will accept as valid input. This might look something like:
$'this-is-my-folder200\231s-name-200\234Bob200\235'
...which you can then use as an argument to mv:
mv $'this-is-my-folder200\231s-name-200\234Bob200\235' this-is-my-folders-name-BOB
Incidentally, if your operating system works the same way mine does (when running the test above), this would explain why using single-character globs such as ? for those characters didn't work: They're actually more than one byte long each!
You can use shell globbing token ? to match any single character, so matching the smart quotes using ? should do:
mv this-is-my-folder?s-name-?Bob? new_name
Here replacing the smart quotes with ? to match the file name.
There are several possibilities.
If an initial substring of the file name ending before the first quote is unique within the directory, then you can use filename completion to help you type an appropriate command. Type "mv" (without the quotes) and the unique initial substring, then press the TAB key to request filename completion. Bash will complete the filename with the correct characters, correctly escaped.
Use a graphical file browser. Then you can select the file to rename by clicking on it. (Details of how to proceed from there depend on the browser.) If you don't have a graphical terminal and can't get one, then you may be able to do the same with a text-mode browser such as Midnight Commander.
A simple glob built with the ? or * wildcard should be able to match the filename
Use a more complex glob to select the filename, and perhaps others with the same problem. Maybe something like *[^a-zA-Z0-9-]* would do. Use a pattern substitution to assign a new name. Something like this:
for f in *[^a-zA-Z0-9-]*; do
mv "$f" "${f//[^a-zA-Z0-9-]/}"
done
The substitution replaces all appearances of a characters that are not decimal digits, appercase or lowercase Latin letters, or hyphens with nothing (i.e. it strips them). Do take care before you use this, though, to make sure you're not going to make more changes than you intend to do.

How to escape colon (:) in $PATH on UNIX?

I need to parse the $PATH environment variable in my application.
So I was wondering what escape characters would be valid in $PATH.
I created a test directory called /bin:d and created a test script called funny inside it. It runs if I call it with an absolute path.
I just can't figure out how to escape : in $PATH I tried escaping the colon with \ and wrapping it into single ' and double " quotes. But always when I run which funny it can't find it.
I'm running CentOS 6.
This is impossible according to the POSIX standard. This is not a function of a specific shell, PATH handling is done within the execvp function in the C library. There is no provision for any kind of quoting.
This is the reason why including certain characters (anything not in the "portable filename character set" - colon is specifically called out as an example.) is strongly recommended against.
From SUSv7:
Since <colon> is a separator in this context, directory names that might be used in PATH should not include a <colon> character.
See also source of GLIBC execvp. We can see it uses the strchrnul and memcpy functions for processing the PATH components, with absolutely no provision for skipping over or unescaping any kind of escape character.
Looking at the function
extract_colon_unit
it seems to me that this is impossible. The : is unconditionally and
inescapably used as the path separator.
Well, this is valid at least for bash. Other shells may vary.
You could try mounting it
mount /bin:d /bind
PATH=/bind
According to http://tldp.org/LDP/abs/html/special-chars.html single quotes should preserve all special characters, so without trying it, I would think that '/bin:d' would work (with)in $PATH.

Resources