how to get directory name by inode value in c? - c

i am tring to add a new feature to bash
and the job need a func to get the directory name from the given inode value
so how to got this work? i mean if there's some builtin func in bash's source code
if not , any help will be accept

This is impossible without searching through the filesystem for a dir that contains a file with the given inode. Filenames are named references to inodes with no backlinks.

List the directory and get the inode of .. then list that and find the name of the file there that has the inode you are looking for.

Related

How do I list all files in a directory for a directory name stored in a tensor?

I have a placeholder which will be fed by a string value. The string value represents a path to some directory name. In that directory there are different ".jpg" files. How can I ask TensorFlow to list all of those files?
In other words, I want to add a node to my computation graph which will result in listing the directory, if I run it through my session. Is there a way to do that?
You can do this either using or not TensorFlow. I will only show you how you can list all files in a certain directory using TensorFlow, using tf.gfile.ListDirectory. You don't even need placeholders (in this case).
import tensorflow as tf
dir_path = "."
print(tf.gfile.ListDirectory(dir_path))

Trying to loop through Directory to list all the files using lua

I am new to lua to trying to understand and put pieces to together and looking out for some help.
I have gone through the existing articles on lua file looping but unable to get the desired output.
Question - I have a folder with files, Folder path - "D:\Test_Files\Outbound\Client\final"
Files in the folder with extension - .txt
Trying to :
Get the count of files in the folder(in this case "final" folder).
Read every file, building a loop something similar to this:
list = {}
for i=0,(#Totalfilecount) do
local fr = io.open('D:\Test_Files\Outbound\Client\final\'..filename.,'rb')
local f = fr.read('*.txt')
Customfunction(f) -- Passing file content to customfunction to apply business logic.
end
Questions :
How to get file count from a directory?
How to read the directory to check if the files with "*.txt" exist?
How to use table list to store each file name and read through the loop?
How to read each file via loop and pass the value to function "Customfunction(f)"?
Code is expected to run on windows. Please share suggestions in pure lua without using external file system functions such as 'lfs' as we do not like to import external functions.
Any Suggestions/help will be greatly appreciated!
You can't (at least shouldn't) do this without extensions to Lua. To accomplish this, you have to download LuaFileSystem library. You can do it using LuaRocks:
$ luarocks install luafilesystem
Use library as such:
require "lfs"
function dirtree(dir)
assert(dir and dir ~= "", "Please pass directory parameter")
if string.sub(dir, -1) == "/" then
dir=string.sub(dir, 1, -2)
end
local function yieldtree(dir)
for entry in lfs.dir(dir) do
if entry ~= "." and entry ~= ".." then
entry=dir.."/"..entry
local attr=lfs.attributes(entry)
coroutine.yield(entry,attr)
if attr.mode == "directory" then
yieldtree(entry)
end
end
end
end
return coroutine.wrap(function() yieldtree(dir) end)
end
An example use of code above:
for filename, attr in dirtree("D:\Test_Files\Outbound\Client\final") do
print(attr.mode, filename)
end
You have to check does extension equal to txt. To read file extension use this snippet:
function GetFileExtension(path)
return path:match("^.+(%..+)$")
end
So, to answer your question(s), you can get amount of files in directory just by counting elements in array returned in dirtree. To answer second question, just use code from the post. Table that you want is returned by dirtree(), but you may want to extract only .txt files from it. To read a file, just check other SO answers. You've got given name (in array), so use it.
EDIT: You can parse result of dir and ls command to get directory listing, but you shouldnt. Althrough this way you wouldn't need to install any libraries, your code is going to be heavily OS-depedent.
Adding libraries to your code isn't so bad. Hacking things is worse.
(Not sure file extension extracting function is going to work. I didn't make dirtree code used in this post, it belongs to David Kastrup)

Are all file names valid directory names?

I need to create a directory for a file. The easiest path would be to create a directory with the same name as the file, and put the file there.
Is there any case where a valid file name would be an invalid directory name?
The directory will of course be created on the same system as where the file name is valid.
So basically I'm asking if there is any system where there are different limitations on directory names and file names.
As the number of characters in a path is limited on some filesystems, any file that uses more than half the limit of path characters would cause problems if put into a directory of the same name.

How to list a directory recursively

I'm looking to list and store the contents of a directory in a struct using C on Windows.
I got a problem with stat(), I don't really understand this line
if (statut.st_mode & S_IFDIR)
So I want to understand how it works for checking if it's a directory or a file?
stat() retrieves a block of information describing the specified file. Directories are also files. A directory can be thought of as a file that contains other files.
So, in the file's st_mode, you can see whether the current file is actually a directory by checking for the presence of the S_IFDIR bit.

getting folder content C language

How can i get the paths of a folder and its content. Say i have folder named MyFolder as
/tmp/MyFolder/ where it has subfolders SubFolder1, SubFolder2... and some files
You may take a look at the opendir() family functions.
A more efficient way than {open,read,close}dir() is Linux' getdirentries() function. See getdirentries(3) for details.
Use dirent and dir structures. You can use opendir(), readdir() etc for manipulation.
readdir() will give one name at a time and you can keep calling it iteratively.

Resources