c++ copying directory into an another directory (linux cp command) - c++

I'm trying to implement cp -r command so that when user types in cp -r dir dir1, dir gets copied and pasted inside dir1. Below is what i have so far and it does copy the files and directories inside the directory, but it doesn't copy directory itself. For example, when there is a file1 and a directory 'a'inside dir then it will only copy and paste file1 and 'a' inside of dir1, but not the directory dir itself.
Any suggestions?

You start your algorithm one step too deep into the directory you are copying: dir_entry = readdir(dir) reads all the entrys inside your source directory and therefore the source directory itself is not copied.
The function is fine, you just need one extra step before you call it.
Instead of calling dirCopy("a", "b") you need to start by executing
mkdir("b/a", convertMode("a"));
and then
dirCopy("a", "b/a");
So you are going to need code that extracts the last filename part from your path so you can then append it to newPath. If you need assistance for that you can look at this question: Get a file name from a path

Related

Is there a way to copy a directory full of files and change their file extentions in a Dockerfile

Problem
I want to copy all the files in a directory over to my container, changing the extension.
Setup
Lets say I want my local directory src/* copied into the container at /work/
src/ contains a bunch of files with extensions like .txt.
I want to COPY them over and change their extensions to .textfile.
Currently I am doing
COPY src/file1.txt /work/file1.textfile
COPY src/file2.txt /work/file2.textfile
COPY src/file3.txt /work/file3.textfile
...
This works file but create a bunch of layers in my image and I'd like to just have it done in one step.
What I've tried
COPY src/*.text /work/*.textfile
This doesn't work.
Work around
I installed util-linux and used the rename command like so:
COPY src/*.txt /work/
RUN rename '.txt' '.textfile' /work
But this is undesirable as the command comes as a package.
Question
Is there a way to do this simply as a dockerfile command?

How to use the path with the space in docker file

I want to write a cmd in docker file to copy the file at the destination C:\windows\Program Files. I am having an issue due to space in program files. I am able to copy the file to different location. Any suggestion will be appreciated.
I am getting below error:
Step 4 : COPY "C:\docker\prerequisites\MicrosoftSDKs" "C:\Program Files
(x86)\MicrosoftSDKs"
Forbidden path outside the build context: C:\docker\prerequisites\MicrosoftSDKs
()
Use the JSON form, you have to use double backslashes inside the braces
FROM microsoft/windowsservercore
COPY ["C:\\docker\\prerequisites\\MicrosoftSDKs", "C:\\Program Files (x86)\\MicrosoftSDKs"]
You can also use slash:
COPY ["C:/Program Files/nodejs", "/windows/system32"]
workaround:
write the script with the spaces into a file, and in the docker file, run the script by calling the file.
in a file named myscript.bat write
COPY "C:\docker\prerequisites\MicrosoftSDKs" "C:\Program Files (x86)\MicrosoftSDKs"
and in the DockerFile
RUN myscript.bat
You can use ARG:
ARG sourc = "C:\docker\prerequisites\MicrosoftSDKs"
ARG destination = "C:\Program Files (x86)\MicrosoftSDKs"
COPY ${sourc} ${destination}
Enclosing the path in quotes like this should help if there are spaces:
"C:\windows\Program Files"

Can we mkdir + rename and remain atomic?

I am designing a build system that needs to be careful about manipulating the filesystem in an atomic manner.
I have run into a situation where I have a temporary directory that contains files and now I want to move it to a more permanent home. I know how to rename atomically, but this does not work if the full destination path does not exist. If I didn't care about being atomic, the answer would be a simple mkdir -p ... && mv .... Is it possible to rename the temporary directory atomically in a way that will create the parent directories? I cannot do so ahead of time, because the parent directory names are computed.
This will be run inside of a Node.js program, so something from the npm ecosystem would be desirable, but it is trivial to call a command line program or C / C++ library to do the work.
Example:
/tmp/mytempbuild - a directory containing files
/myproject/build/dev/0.2.2 - the desired destination, whose parent may not exist
/myproject/build/master - an older, existing directory that must not be changed

How to replace a folder in a pending cl in p4?

I want to replace folder A in P4 by another folder A.
The two folders have different files and sub folders.
I know, we can do it by deleting old folder A then adding new folder A.
But, can I do it with only one step in a pending changelist ?
As following result in that pending cl:
If this file is in old folder, but not in new folder, then it is marked by "delete".
If this file is in new folder, but not in old folder, then it is marked by "add".
If this file is in new folder and also in old folder, then it is marked by "modify".
Thank you
Are both these folders under source control?
That is, are you trying to make //depot/folder/A contain what //depot/other/A_prime contains?
If so, consider using 'p4 copy':
p4 copy //depot/other/A_prime/... //depot/folder/A/...
If the other folder A is just something you have on your hard disk, then consider using 'reconcile':
p4 edit //depot/folder/A/...
rm -r /path/to/depot/folder/A/*
cp -r /path/to/other/folder/A/* /path/to/depot/folder/A
p4 reconcile -aed //depot/folder/A/...
I kind of like the 'p4 copy' approach, myself, so I'd be tempted to check that other folder into Perforce (in a different location in the repository, naturally), so that I could then run 'p4 copy'.

read and write files to different directories Python

I want my code to go into a sub directory, perform some operation and save the output in a file which is one step up, to the main dir.
Main directory ---> sub_directory
I would appreciate solutions which do not require "hardcoding" the path of the main
directory. Is there a way I can directly write my file output to the main dir without
doing a os.chdir() every iteration? Something like just giving the path of the file to read and write?
For eg:
# example
import os
for i in xrange(10):
code to read and operate on some file in this sub dir one by one (ten files)
# write output file to the previous directory
# without hardcoding the path
code to write files to main directory (ten files )
You probably want to check the directory the file is operating within or check the current working directory:
import os
cur_dir= os.getcwd()
top_dir = os.path.dirname(cur_dir)
# perform operations in current directory
# do some stuff in top directory
Assuming you start in the main directory, and you know the (relative) path to the subdirectories, just do
open(os.path.join(subdir, filename))
to access a path in a subdirectory without actually changing the current directory.