Go Lang ioutil.writeFile function making directories and files read only - go

I have created a go program to trace over all the files under a root path and replace a specific source string with a target string after that writhing the updated content into the existing file and for that, I have used filepath.Walk function to trace over the files and ioutil.ReadFile and ioutil.WriteFile to read from and write into the file.
So, the problem is ioutil.WriteFile function is marking the root directory, subdirectories, and files as read-only. I checked online for the solution of this but could not find anything appropriate.
Below is the code snippet of same.
if strings.Contains(data, sourceString) {
data = strings.ReplaceAll(data, sourceString, targetStringArray[index])
ioutil.WriteFile(path, []byte(data), os.FileMode(0655))
}

File mode 0655 is translated to permissions rw- r-x r-x. This combination of permissions is weird, because there is no "x" for the owner, but there are for the group and others. Maybe what you want is 0755 (rwx r-x r-x) for executable files or 0644 (rw- r-- r--) for non-executable files, which are the most used ones.

Related

How to recursively change file permissions only in a ruby script

I have been using the function FileUtils.chmod_R to recursively change files and directories permissions under a given path but now want to change only the file permissions and leave the directories as they are. Looking at the man page for this function I can't see how to do this and I would prefer not to do this with a bash script. Please can someone tell me if this is possible with the FileUtils.chmod_R function or would I have to write additional code to iterate over every file that exist under a given path (recursive) and then FileUtils.chmod it to the desire permission? I am a ruby newbie so please point me someplace if I am asking anything obvious
You could do something like below - this will change permissions of the list of files matched by Dir.glob.
FileUtils.chmod 0400, Dir.glob('/path/to/dir/**/*')
As mentioned in this thread,
Dir.glob("**/*/") # will return list of all directories
Dir.glob("**/*") # will return list of all files

Visual Studio Setup Project - How to Obtain the Directory Path from a File-Search Launch Condition

I am looking for a way to add File(s) to an existing directory that has a random name as part of a Visual Studio Setup Project and I hoped someone might be able to help me solve this puzzle please.
I have been attempting to obtain the discovered path property of the directory using a Launch Condition; Unfortunately this method returns the full file path including the filename, which cannot be used as a directory property.
The directory in question takes the form [AppDataFolder]Company\Product\aaaaaaaaaaaa\
where aaaaaaaaaaaa is a random installation string.
Within the Launch Condition Setup I check for the directory's existence by searching for a file that would appear inside it,
Search Target Machine
(Name): File marker
Filename: sample.txt
Folder: [AppDataFolder]Company\Product\
Property: DIRFILE
Launch Condition
(Name): File marker exists
Condition: DIRFILE
In the Setup Project I add the file I wish to insert, with the details
Condition: DIRFILE
Folder: 'Installation folder'
Then in File System Setup I add a new folder entry for the random directory aaaaaaaaaaaa
(Name): Installation folder
Condition: DIRFILE
DefaultLocation: [DIRFILE]\..\ *Incorrect*
Property [DIRLOCATION]
As you can see the installer detects the existence of the marker file but, instead of placing my file at the same location, when using [DIRFILE] the installer would incorrectly try and insert it INTO the file;
This is because the file path was returned
[AppDataFolder]Company\Product\aaaaaaaaaaaa\sample.txt
where I instead need the directory path
[AppDataFolder]Company\Product\aaaaaaaaaaaa
Therefore I was wondering if it was possible to return the directory the file was found in from Search Target Machine (as opposed to the file location of the file), if I could extract the directory path by performing a string replace of the filename on the file location DIRFILE within the DefaultLocation field in File System Setup, or if perhaps there is even another method I am missing?
I'm also very interested in a simple solution for this, inside the setup project.
The way I did solve it was to install the files to a temporary location and then copy them to the final location in an AfterInstall event handler. Not a very elegant solution! Since it no longer care about the user selected target path I removed that dialog. Also I needed to take special care when uninstalling.
public override void OnAfterInstall(IDictionary savedState)
{
base.OnAfterInstall(savedState);
// Get original file folder
string originDir = Context.Parameters["targetdir"];
// Get new file folder based on the dir of sample.txt
string newDir = Path.GetDirectoryName(Context.Parameters["dirfile"]);
// Application executable file name
// (or loop for all files on the path instead)
string filename = "ApplicationName.exe";
// Move or copy the file
File.Move(Path.Combine(originDir, filename), Path.Combine(newDir, filename)));
}

Copy Files to Directory and Overwrite if theres any Collisions

I am attempting to move files from directory A to directory B. When I move the files into directory B, if there are any collisions then I want the files from A to overwrite the collisions in B.
Will my following code do this?
SetOverwrite ifnewer
CopyFiles \SILENT "Directories/Directory_A/" "Directories/Directory_B"
Note: I am aware of the function MoveFileFolder but I am experiencing buggy behaviour where the files are not being moved when the function is called.
according to the docs, SetOverwrite only works with the File command. if your overwriting criteria is based on the file date, you might want to have a look at GetTime, if it's file existence use IfFileExists

Why are the contents of the subfolders when MQFTE is used for transfer?

Hi when I tried to transfer the contents of a folder ( The folder has several subfolders and few files) using MQFTE ftecreatetransfer command, Not only the few files in the folder but also the contents of the subfolder are transferred to destination. The same subfolders are created in destination and the contents are transferred. Is there a way to avoid the files from subfolders being transferred ?
As per this page in the Infocenter:
When a directory is specified as a source file specification, the
contents of the directory are copied. More precisely, all files in the
directory and in all its subdirectories, including hidden files, are
copied.
However, it looks like they anticipated your question because the page recently added this clarification:
For example, to copy the contents of DIR1 to DIR2 only, specify
fteCreateTransfer ... -dd DIR2 DIR1/*
So instead of specifying the folder, add the wild card to the end and you get just the files in the top level of that folder. (Assuming of course that you do not also use the -r option!)

Ruby (Errno::EACCES) on File.delete

I am trying to delete some XML files after I have finished using them and one of them is giving me this error:
'delete': Permission denied - monthly-builds.xml (Errno::EACCES)
Ruby is claiming that the file is write protected but I set the permissions before I try to delete it.
This is what I am trying to do:
#collect the xml files from the current directory
filenames = Dir.glob("*.xml")
#do stuff to the XML files
finalXML = process_xml_files( filenames )
#clean up directory
filenames.each do |filename|
File.chmod(777, filename) # Full permissions
File.delete(filename)
end
Any ideas?
This:
File.chmod(777, filename)
doesn't do what you think it does. From the fine manual:
Changes permission bits on the named file(s) to the bit pattern represented by mode_int.
Emphasis mine. File modes are generally specified in octal as that nicely separates the bits into the three Unix permission groups (owner, group, other):
File.chmod(0777, filename)
So you're not actually setting the file to full access, you're setting the permission bits to 01411 which comes out like this:
-r----x--t
rather than the
-rwxrwxrwx
that you're expecting. Notice that your (decimal) 777 permission bitmap has removed write permission.
Also, deleting a file requires write access to the directory that the file is in (on Unixish systems at least) so check the permissions on the directory.
And finally, you might want to check the return value from File.chmod:
[...] Returns the number of files processed.
Just because you call doesn't mean that it will succeed.
You may not have access to run chmod. You must own the file to change its permissions.
The file may also be locked by nature of being open in another application. If you're viewing the file in, say, a text editor, you might not be able to delete it.
In my case it was because the file I had been trying to delete--kindle .sdr record--was directory, not file. I need to use this instead:
FileUtils.rm_rf(dirname)

Resources