IDA C++ SDK 9.2
Loading...
Searching...
No Matches
dirtree.hpp
Go to the documentation of this file.
1
25
26#ifndef DIRTREE_HPP
27#define DIRTREE_HPP
28
29//------------------------------------------------------------------------
30typedef qvector<inode_t> inodevec_t; // sequence of inodes
31
36typedef qvector<diridx_t> dirvec_t; // sequence of directory indexes
37
40#define BAD_BLOB_IDX blob_idx_t(-1)
41
42// We use PACKED to save memory, without it we would spend 64 bits instead of
43// 8 bits to store a 1-bit value on ida64.
44#pragma pack(push, 1)
45
47struct PACKED direntry_t
48{
50 bool isdir;
51
52 static const uval_t BADIDX = uval_t(-1);
53 static const uval_t ROOTIDX = 0;
54
55 direntry_t(uval_t i=BADIDX, bool d=false) : idx(i), isdir(d) {}
56 bool valid() const { return idx != BADIDX; }
57
58 bool operator==(const direntry_t &r) const
59 {
60 return idx == r.idx && isdir == r.isdir;
61 }
62 bool operator!=(const direntry_t &r) const
63 {
64 return !(*this == r);
65 }
66 bool operator<(const direntry_t &r) const
67 {
68 if ( isdir != r.isdir )
69 return isdir; // any folder is lesser than any inode
70 return idx < r.idx;
71 }
72};
73#pragma pack(pop)
76
79enum
80{
88};
90
91//------------------------------------------------------------------------
95{
97 enum
98 {
99 DSF_INODE_EA = 0x01, // inode is EA, will be handled during segment moving
100 DSF_PRIVRANGE = 0x02, // inode is tid_t, structure or enum id, will be handled during segment moving
101 DSF_ORDERABLE = 0x04, // items in a folder are ordered by increasing inode # (unless explicitly reordered)
102 };
103 // netnode name to load/save directory tree
104 // If not specified the loading/storing operations are not supported
105 // and the undo functionality is not supported.
107
108 dirspec_t(const char *nm=nullptr, uint32 f=0) : flags(f), id(nm) {}
109
110 virtual ~dirspec_t() {}
111
118 virtual bool get_name(
119 qstring *out,
120 inode_t inode,
121 uint32 name_flags=DTN_FULL_NAME) = 0;
122
127 virtual inode_t get_inode(const char *dirpath, const char *name) = 0;
128
129 // print additional attributes of the entry. for example, is union? is mapped?
130 virtual qstring get_attrs(inode_t inode) const = 0;
131
136 virtual bool rename_inode(inode_t inode, const char *newname) = 0;
137
140 virtual void unlink_inode(inode_t inode) { qnotused(inode); }
141
142 bool is_orderable() const { return (flags & DSF_ORDERABLE) != 0; }
143};
144
145//------------------------------------------------------------------------
148{
150 size_t rank;
151 dirtree_cursor_t(diridx_t _parent=direntry_t::BADIDX, size_t _rank=size_t(-1))
152 : parent(_parent), rank(_rank) {}
153 bool valid() const { return parent != direntry_t::BADIDX || rank == 0; }
154 bool is_root_cursor() const { return parent == direntry_t::BADIDX && rank == 0; }
156
158 {
160 c.set_root_cursor();
161 return c;
162 }
163
165 {
166 if ( parent < r.parent ) return -1;
167 if ( parent > r.parent ) return 1;
168 if ( rank < r.rank ) return -1;
169 if ( rank > r.rank ) return 1;
170 return 0;
171 }
172};
175
176//-------------------------------------------------------------------------
178
179//------------------------------------------------------------------------
186
187//------------------------------------------------------------------------
202
203class dirtree_t;
204class dirtree_impl_t;
205struct segm_move_infos_t;
206
207//-------------------------------------------------------------------------
210{
218 virtual ssize_t visit(
219 const dirtree_cursor_t &c,
220 const direntry_t &de) = 0;
221};
222
223
225//------------------------------------------------------------------------
226// internal functions; use dirtree_t members instead
227#ifndef SWIG
228idaman dirtree_impl_t *ida_export create_dirtree(dirtree_t *dt, dirspec_t *ds);
229idaman void ida_export delete_dirtree(dirtree_impl_t *d);
230idaman bool ida_export load_dirtree(dirtree_impl_t *d);
231idaman bool ida_export save_dirtree(dirtree_impl_t *d);
232void reset_dirtree(dirtree_impl_t *d);
233idaman const char *ida_export dirtree_errstr(dterr_t err);
234idaman bool ida_export dirtree_is_orderable(const dirtree_impl_t *d);
235idaman dterr_t ida_export dirtree_chdir(dirtree_impl_t *d, const char *path);
236idaman void ida_export dirtree_getcwd(qstring *out, const dirtree_impl_t *d);
237idaman void ida_export dirtree_resolve_path(direntry_t *de, const dirtree_impl_t *d, const char *path);
238idaman void ida_export dirtree_resolve_cursor(direntry_t *de, const dirtree_impl_t *d, const dirtree_cursor_t &cursor);
239idaman bool ida_export dirtree_get_entry_name(qstring *out, const dirtree_impl_t *d, const direntry_t &de, uint32 name_flags);
240idaman void ida_export dirtree_get_entry_attrs(qstring *out, const dirtree_impl_t *d, const direntry_t &de);
241idaman bool ida_export dirtree_is_dir_ordered(const dirtree_impl_t *d, diridx_t diridx);
242idaman bool ida_export dirtree_set_natural_order(dirtree_impl_t *d, diridx_t diridx, bool enable);
243idaman ssize_t ida_export dirtree_get_dir_size(dirtree_impl_t *d, diridx_t diridx);
244idaman bool ida_export dirtree_findfirst(dirtree_impl_t *d, dirtree_iterator_t *ff, const char *pattern);
245idaman bool ida_export dirtree_findnext(dirtree_impl_t *d, dirtree_iterator_t *ff);
246idaman bool ida_export dirtree_get_abspath_by_cursor(qstring *out, const dirtree_impl_t *d, const dirtree_cursor_t &cursor, uint32 name_flags);
247idaman bool ida_export dirtree_get_abspath_by_relpath(qstring *out, const dirtree_impl_t *d, const char *relpath);
248idaman dterr_t ida_export dirtree_mkdir(dirtree_impl_t *d, const char *path);
249idaman dterr_t ida_export dirtree_rmdir(dirtree_impl_t *d, const char *path);
250idaman dterr_t ida_export dirtree_link(dirtree_impl_t *d, const char *path, bool do_link);
251idaman dterr_t ida_export dirtree_link_inode(dirtree_impl_t *d, inode_t inode, bool do_link);
252idaman dterr_t ida_export dirtree_rename(dirtree_impl_t *d, const char *from, const char *to);
253idaman ssize_t ida_export dirtree_get_rank(const dirtree_impl_t *d, diridx_t diridx, const direntry_t &de);
254idaman dterr_t ida_export dirtree_change_rank(dirtree_impl_t *d, const char *path, ssize_t rank_delta);
255idaman void ida_export dirtree_get_parent_cursor(dirtree_cursor_t *out, const dirtree_impl_t *d, const dirtree_cursor_t &cursor);
256idaman void ida_export notify_dirtree(dirtree_impl_t *d, bool added, inode_t inode);
257idaman const char *ida_export dirtree_get_id(const dirtree_impl_t *d);
258idaman void ida_export dirtree_set_id(dirtree_impl_t *d, const char *nm);
259idaman const char *ida_export dirtree_get_nodename(const dirtree_impl_t *d); // compat
260idaman void ida_export dirtree_set_nodename(dirtree_impl_t *d, const char *nm);// compat
261idaman ssize_t ida_export dirtree_traverse(dirtree_impl_t *d, dirtree_visitor_t &v);
262idaman dterr_t ida_export dirtree_find_entry(dirtree_cursor_t *out, const dirtree_t *_dt, const direntry_t &_de);
263idaman dirtree_t *ida_export dirtree_new_shadow_dirtree(dirtree_impl_t *d);
264#endif // SWIG
265
266
267
268
270
271//------------------------------------------------------------------------
276{
277 dirtree_impl_t *d;
278
279
280public:
281 //lint -sem(dirtree_t::dirtree_t, custodial(1))
282 dirtree_t(dirspec_t *ds) { d = create_dirtree(this, ds); }
283 ~dirtree_t() { delete_dirtree(d); }
284
286 static const char *errstr(dterr_t err) { return dirtree_errstr(err); }
287
290 bool is_orderable() const { return dirtree_is_orderable(d); }
291
295 dterr_t chdir(const char *path) { return dirtree_chdir(d, path); }
296
300 {
301 qstring out;
302 dirtree_getcwd(&out, d);
303 return out;
304 }
305
312 qstring get_abspath(const dirtree_cursor_t &cursor, uint32 name_flags=DTN_FULL_NAME) const
313 {
314 qstring out;
315 dirtree_get_abspath_by_cursor(&out, d, cursor, name_flags);
316 return out;
317 }
318
324 qstring get_abspath(const char *relpath) const
325 {
326 qstring out;
327 dirtree_get_abspath_by_relpath(&out, d, relpath);
328 return out;
329 }
330
337 {
338 direntry_t de;
339 dirtree_resolve_cursor(&de, d, cursor);
340 return de;
341 }
342
346 direntry_t resolve_path(const char *path) const
347 {
348 direntry_t de;
349 dirtree_resolve_path(&de, d, path);
350 return de;
351 }
352
353 static bool isdir(const direntry_t &de) { return de.valid() && de.isdir; }
354 static bool isfile(const direntry_t &de) { return de.valid() && !de.isdir; }
355
359 bool isdir(const char *path) const
360 {
361 direntry_t de = resolve_path(path);
362 return isdir(de);
363 }
364
368 bool isfile(const char *path) const
369 {
370 direntry_t de = resolve_path(path);
371 return isfile(de);
372 }
373
380 const direntry_t &de,
381 uint32 name_flags=DTN_FULL_NAME) const
382 {
383 qstring out;
384 dirtree_get_entry_name(&out, d, de, name_flags);
385 return out;
386 }
387
390 bool is_dir_ordered(diridx_t diridx) const { return dirtree_is_dir_ordered(d, diridx); }
391
400 bool set_natural_order(diridx_t diridx, bool enable) const { return dirtree_set_natural_order(d, diridx, enable); }
401
406 ssize_t get_dir_size(diridx_t diridx) const { return dirtree_get_dir_size(d, diridx); }
407
412 {
413 qstring out;
414 dirtree_get_entry_attrs(&out, d, de);
415 return out;
416 }
417
422 bool findfirst(dirtree_iterator_t *ff, const char *pattern) const
423 {
424 return dirtree_findfirst(d, ff, pattern);
425 }
426
431 {
432 return dirtree_findnext(d, ff);
433 }
434
438 dterr_t mkdir(const char *path) { return dirtree_mkdir(d, path); }
439
443 dterr_t rmdir(const char *path) { return dirtree_rmdir(d, path); }
444
448 dterr_t link(const char *path) { return dirtree_link(d, path, true); }
449
453 dterr_t unlink(const char *path) { return dirtree_link(d, path, false); }
454
458 dterr_t link(inode_t inode) { return dirtree_link_inode(d, inode, true); }
459
463 dterr_t unlink(inode_t inode) { return dirtree_link_inode(d, inode, false); }
464
470 dterr_t rename(const char *from, const char *to)
471 {
472 return dirtree_rename(d, from, to);
473 }
474
480 ssize_t get_rank(diridx_t diridx, const direntry_t &de) const
481 {
482 return dirtree_get_rank(d, diridx, de);
483 }
484
492 dterr_t change_rank(const char *path, ssize_t rank_delta)
493 {
494 return dirtree_change_rank(d, path, rank_delta);
495 }
496
501 {
502 dirtree_cursor_t parent;
503 dirtree_get_parent_cursor(&parent, d, cursor);
504 return parent;
505 }
506
513 bool load()
514 {
515 return load_dirtree(d);
516 }
517
521 bool save() const
522 {
523 return save_dirtree(d);
524 }
525
527 const char *get_id() const
528 {
529 return dirtree_get_id(d);
530 }
531
532 void set_id(const char *nm)
533 {
534 return dirtree_set_id(d, nm);
535 }
536
540 void notify_dirtree(bool added, inode_t inode)
541 {
542 ::notify_dirtree(d, added, inode);
543 }
544
554 {
555 return dirtree_traverse(d, v);
556 }
557
562 {
564 dirtree_find_entry(&c, this, de);
565 return c;
566 }
567
568};
569
583
584
585#endif // define DIRTREE_HPP
Directory tree.
Definition dirtree.hpp:276
dterr_t link(inode_t inode)
Add an inode into the current directory.
Definition dirtree.hpp:458
bool isdir(const char *path) const
Is a directory?
Definition dirtree.hpp:359
bool isfile(const char *path) const
Is a file?
Definition dirtree.hpp:368
bool is_dir_ordered(diridx_t diridx) const
Is dir ordered?
Definition dirtree.hpp:390
direntry_t resolve_cursor(const dirtree_cursor_t &cursor) const
Resolve cursor.
Definition dirtree.hpp:336
const char * get_id() const
netnode name
Definition dirtree.hpp:527
dterr_t chdir(const char *path)
Change current directory.
Definition dirtree.hpp:295
dterr_t link(const char *path)
Add a file item into a directory.
Definition dirtree.hpp:448
~dirtree_t()
Definition dirtree.hpp:283
static bool isfile(const direntry_t &de)
Definition dirtree.hpp:354
dterr_t rename(const char *from, const char *to)
Rename a directory entry.
Definition dirtree.hpp:470
qstring get_abspath(const dirtree_cursor_t &cursor, uint32 name_flags=DTN_FULL_NAME) const
Get absolute path pointed by the cursor.
Definition dirtree.hpp:312
void set_id(const char *nm)
Definition dirtree.hpp:532
dirtree_t(dirspec_t *ds)
Definition dirtree.hpp:282
direntry_t resolve_path(const char *path) const
Resolve path.
Definition dirtree.hpp:346
dterr_t change_rank(const char *path, ssize_t rank_delta)
Change ordering rank of an item.
Definition dirtree.hpp:492
bool set_natural_order(diridx_t diridx, bool enable) const
Enable/disable natural inode order in a directory.
Definition dirtree.hpp:400
ssize_t traverse(dirtree_visitor_t &v) const
Traverse dirtree, and be notified at each entry If the the visitor returns anything other than 0,...
Definition dirtree.hpp:553
dterr_t mkdir(const char *path)
Create a directory.
Definition dirtree.hpp:438
qstring get_entry_attrs(const direntry_t &de) const
Get entry attributes.
Definition dirtree.hpp:411
qstring get_entry_name(const direntry_t &de, uint32 name_flags=DTN_FULL_NAME) const
Get entry name.
Definition dirtree.hpp:379
bool is_orderable() const
Is dirtree orderable?
Definition dirtree.hpp:290
ssize_t get_rank(diridx_t diridx, const direntry_t &de) const
Get ordering rank of an item.
Definition dirtree.hpp:480
bool findfirst(dirtree_iterator_t *ff, const char *pattern) const
Start iterating over files in a directory.
Definition dirtree.hpp:422
dterr_t rmdir(const char *path)
Remove a directory.
Definition dirtree.hpp:443
qstring get_abspath(const char *relpath) const
Construct an absolute path from the specified relative path.
Definition dirtree.hpp:324
bool findnext(dirtree_iterator_t *ff) const
Continue iterating over files in a directory.
Definition dirtree.hpp:430
dirtree_cursor_t find_entry(const direntry_t &de) const
Find the cursor corresponding to an entry of a directory.
Definition dirtree.hpp:561
void notify_dirtree(bool added, inode_t inode)
Notify dirtree about a change of an inode.
Definition dirtree.hpp:540
dterr_t unlink(const char *path)
Remove a file item from a directory.
Definition dirtree.hpp:453
static bool isdir(const direntry_t &de)
Definition dirtree.hpp:353
ssize_t get_dir_size(diridx_t diridx) const
Get dir size.
Definition dirtree.hpp:406
bool load()
Load the tree structure from the netnode.
Definition dirtree.hpp:513
qstring getcwd() const
Get current directory.
Definition dirtree.hpp:299
static const char * errstr(dterr_t err)
Get textual representation of the error code.
Definition dirtree.hpp:286
dterr_t unlink(inode_t inode)
Remove an inode from the current directory.
Definition dirtree.hpp:463
dirtree_cursor_t get_parent_cursor(const dirtree_cursor_t &cursor) const
Get parent cursor.
Definition dirtree.hpp:500
bool save() const
Save the tree structure to the netnode.
Definition dirtree.hpp:521
Reimplementation of vector class from STL.
Definition pro.h:2250
dirtree_id_t
Built-in dirtree specializations:
Definition dirtree.hpp:572
@ DIRTREE_FUNCS
Definition dirtree.hpp:574
@ DIRTREE_LTYPES_BOOKMARKS
Definition dirtree.hpp:579
@ DIRTREE_IDAPLACE_BOOKMARKS
Definition dirtree.hpp:577
@ DIRTREE_IMPORTS
Definition dirtree.hpp:576
@ DIRTREE_LOCAL_TYPES
Definition dirtree.hpp:573
@ DIRTREE_NAMES
Definition dirtree.hpp:575
@ DIRTREE_BPTS
Definition dirtree.hpp:578
@ DIRTREE_END
Definition dirtree.hpp:580
idaman dirtree_t *ida_export get_std_dirtree(dirtree_id_t id)
qvector< inode_t > inodevec_t
Definition dirtree.hpp:30
qvector< direntry_t > direntry_vec_t
Definition dirtree.hpp:75
uval_t diridx_t
Directory indexes are simple numbers like 0,1,2,3... They are independent of inode numbers.
Definition dirtree.hpp:35
dterr_t
Directory tree: error codes.
Definition dirtree.hpp:190
@ DTE_NOT_DIRECTORY
item is not a directory
Definition dirtree.hpp:194
@ DTE_CANT_RENAME
failed to rename an item
Definition dirtree.hpp:197
@ DTE_OK
ok
Definition dirtree.hpp:191
@ DTE_MAX_DIR
maximum directory count achieved
Definition dirtree.hpp:199
@ DTE_NOT_FOUND
item not found
Definition dirtree.hpp:193
@ DTE_OWN_CHILD
moving inside subdirectory of itself
Definition dirtree.hpp:198
@ DTE_LAST
Definition dirtree.hpp:200
@ DTE_NOT_EMPTY
directory is not empty
Definition dirtree.hpp:195
@ DTE_BAD_PATH
invalid path
Definition dirtree.hpp:196
@ DTE_ALREADY_EXISTS
item already exists
Definition dirtree.hpp:192
ea_t blob_idx_t
Blob index, used for storing/restoring dirtree_t information.
Definition dirtree.hpp:39
qvector< dirtree_cursor_t > dirtree_cursor_vec_t
Definition dirtree.hpp:174
qvector< diridx_t > dirvec_t
Definition dirtree.hpp:36
DECLARE_TYPE_AS_MOVABLE(direntry_t)
@ DTN_FULL_NAME
use long form of the entry name.
Definition dirtree.hpp:81
@ DTN_DISPLAY_NAME
use short, displayable form of the entry name.
Definition dirtree.hpp:83
uval_t uval_t
Definition kernwin.hpp:1878
unsigned int uint32
unsigned 32 bit value
Definition pro.h:348
uint64 ea_t
Definition pro.h:421
uval_t inode_t
The inode_t type is the specialization specific inode number.
Definition pro.h:464
ptrdiff_t ssize_t
Signed size_t - used to check for size overflows when the counter becomes negative.
Definition pro.h:381
_qstring< char > qstring
regular string
Definition pro.h:3694
Directory entry: either a file or directory.
Definition dirtree.hpp:48
bool operator!=(const direntry_t &r) const
Definition dirtree.hpp:62
bool isdir
is 'idx' a diridx_t, or an inode_t
Definition dirtree.hpp:50
bool operator<(const direntry_t &r) const
Definition dirtree.hpp:66
bool valid() const
Definition dirtree.hpp:56
static const uval_t ROOTIDX
Definition dirtree.hpp:53
uval_t idx
diridx_t or inode_t
Definition dirtree.hpp:49
static const uval_t BADIDX
Definition dirtree.hpp:52
direntry_t(uval_t i=BADIDX, bool d=false)
Definition dirtree.hpp:55
bool operator==(const direntry_t &r) const
Definition dirtree.hpp:58
Directory tree specialization.
Definition dirtree.hpp:95
uint32 flags
Definition dirtree.hpp:96
@ DSF_ORDERABLE
Definition dirtree.hpp:101
@ DSF_PRIVRANGE
Definition dirtree.hpp:100
@ DSF_INODE_EA
Definition dirtree.hpp:99
dirspec_t(const char *nm=nullptr, uint32 f=0)
Definition dirtree.hpp:108
virtual bool rename_inode(inode_t inode, const char *newname)=0
rename the entry
virtual bool get_name(qstring *out, inode_t inode, uint32 name_flags=DTN_FULL_NAME)=0
get the entry name.
virtual qstring get_attrs(inode_t inode) const =0
virtual ~dirspec_t()
Definition dirtree.hpp:110
bool is_orderable() const
Definition dirtree.hpp:142
virtual inode_t get_inode(const char *dirpath, const char *name)=0
get the entry inode in the specified directory
virtual void unlink_inode(inode_t inode)
event: unlinked an inode
Definition dirtree.hpp:140
qstring id
Definition dirtree.hpp:106
Position in the directory tree.
Definition dirtree.hpp:148
dirtree_cursor_t(diridx_t _parent=direntry_t::BADIDX, size_t _rank=size_t(-1))
Definition dirtree.hpp:151
bool valid() const
Definition dirtree.hpp:153
void set_root_cursor(void)
Definition dirtree.hpp:155
DECLARE_COMPARISONS(dirtree_cursor_t)
Definition dirtree.hpp:164
size_t rank
the index into the parent directory
Definition dirtree.hpp:150
bool is_root_cursor() const
Definition dirtree.hpp:154
diridx_t parent
the parent directory
Definition dirtree.hpp:149
static dirtree_cursor_t root_cursor()
Definition dirtree.hpp:157
Helper class to iterate over files.
Definition dirtree.hpp:182
dirtree_cursor_t cursor
Definition dirtree.hpp:184
qstring pattern
Definition dirtree.hpp:183
Definition dirtree.hpp:177
A visitor, for use with dirtree_t::traverse.
Definition dirtree.hpp:210
virtual ssize_t visit(const dirtree_cursor_t &c, const direntry_t &de)=0
Will be called for each entry in the dirtree_t If something other than 0 is returned,...
virtual ~dirtree_visitor_t()
Definition dirtree.hpp:211
Definition moves.hpp:60